Hibernate的示例程序无法在数据库中插入记录?
在运行《精通J2EE--Eclipse、Struts、Hibernate及Spring整合应用案例》第三章的Hibernate程序时发现无法向数据库中插入记录。在控制台显示的信息如下:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into hello (name) values (?) select scope_identity()
1.持久化类User.java:
package hibernate;
public class User {
private int id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
2.映射文件hibernate/User.hbm.xml
<?xml version= "1.0 " encoding= "UTF-8 "?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN " "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >
<hibernate-mapping >
<class name= "Hibernate.User " table= "hello ">
<id name= "id " column= "id " type= "int ">
<generator class= "identity "> </generator>
</id>
<property name= "name " column= "name " type= "string "/>
</class>
</hibernate-mapping>
3.配置文件hibernate.hbm.xml
<?xml version= "1.0 " encoding= "UTF-8 "?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN " "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd " >
<hibernate-configuration>
<session-factory>
<!--Database connection settings(数据库连接设置)-->
<property name= "connection.driver_class "> com.microsoft.jdbc.sqlserver.SQLServerDriver </property>
<property name= "connection.url " > jdbc:microsoft:sqlserver://wang:1433;Database=test </property>
<property name= "connection.username "> sa </property>
<property name= "connection.password "> sa </property>
<!--JDBC connection pool (连接池) (use the build-in)-->
<property name= "connection.pool_size "> 1 </property> <!--SQL dialect( SQL 方言 )-->
<property name= "dialect "> org.hibernate.dialect.SQLServerDialect </property>
<!--Enable Hibernate 's automatic session contex management-->
<property name= "current_session_contex_class "> thread </property>
<!--Disable the second-level cache-->
<property name= "cache.provide_class "> org.hibernate.cache.NoCacheProvider </property>
<!--Echo all executed SQL to stdout-->
<property name= "show_sql "> true </property>
<!--Drop and re-create the database schema on startup-->
<property name= "hbm2ddl.auto "> create </property>
<!--映射资源-->
<mapping resource= "hibernate/User.hbm.xml "> </mapping>
</session-factory>
</hibernate-configuration>
4.测试类hibernate/Test.java
package hibernate;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try{
SessionFactory sf=new Configuration().configure().buildSessionFactory();
Session session=sf.openSession();
Transaction tx =session.beginTransaction();
User user = new User();
//user.setPassword( "123 ");//
user.setName( "Hibernate ");
session.save(user);
tx.commit();
session.close();
}catch(HibernateException e){
e.printStackTrace();
}
}
}
我的MSN是:ecjtuwsh@hotmail.com,请各位高手看看我的错在哪里?
[解决办法]
你的错误在哪呢?那个只是警告,你用了Logger 而你也许并没有导入那两个包,就算导入了版本还要正确(具体我也忘了)不过这个没有什么用,在说不影响你的程序所以不用理它,实在不行,就把那部分代码注释掉,我只能说这么多,因为Log4j我没什么研究!