当前位置: 代码迷 >> Eclipse >> Hibernate的示范程序无法在数据库中插入记录
  详细解决方案

Hibernate的示范程序无法在数据库中插入记录

热度:69   发布时间:2016-04-23 18:49:35.0
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>
  相关解决方案