初学者刚接触hibernate,按书本上的例子,用hibernate创建个表并插入条记录,问题是每次运行不插入新记录而是更新了原来的记录,表里永远只有第一条记录,这是哪里的原因?看书上描述和网上查询好像是能执行插入的啊
配置文件<?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>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:1433/Northwind</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">sa</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="Test.hbm.xml"/>
</session-factory>
</hibernate-configuration>Test.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 package="db">
<class name="Test" table="t_test">
<id name="id" unsaved-value="null">
<generator class="identity"/>
</id>
<property name="title"/>
</class>
</hibernate-mapping>Test类package db;
public class Test {
private int id;
private String title;
public void setId(int id){
this.id=id;
}
public int getId(){
return (this.id);
}
public void setTitle(String title){
this.title=title;
}
public String getTitle(){
return (this.title);
}
public void test(){
}
} Configuration conf=new Configuration().configure();
SessionFactory sf=conf.buildSessionFactory();
Session sess=sf.openSession();
Transaction tx=sess.beginTransaction();
Test t=new Test();
t.setTitle("hello");
sess.save(t);
tx.commit();
sess.close();
配置文件<?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>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:1433/Northwind</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">sa</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="Test.hbm.xml"/>
</session-factory>
</hibernate-configuration>Test.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 package="db">
<class name="Test" table="t_test">
<id name="id" unsaved-value="null">
<generator class="identity"/>
</id>
<property name="title"/>
</class>
</hibernate-mapping>Test类package db;
public class Test {
private int id;
private String title;
public void setId(int id){
this.id=id;
}
public int getId(){
return (this.id);
}
public void setTitle(String title){
this.title=title;
}
public String getTitle(){
return (this.title);
}
public void test(){
}
} Configuration conf=new Configuration().configure();
SessionFactory sf=conf.buildSessionFactory();
Session sess=sf.openSession();
Transaction tx=sess.beginTransaction();
Test t=new Test();
t.setTitle("hello");
sess.save(t);
tx.commit();
sess.close();
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货