- 爱易网页
-
Java教程
- hibernate插入新记录会覆盖mysql的旧记录,该怎么处理
日期:2014-05-18 浏览次数:20691 次
hibernate插入新记录会覆盖mysql的旧记录
我做的一个简单的hibernate应用,照着例子做的,结果是每插入一条新记录,就将上一次插入的记录覆盖了.发下附上我的代码,请大家帮忙.
//Mysql DB
DROP TABLE person;
CREATE TABLE person
(
id varchar(32) not null primary key,
name varchar(20) not null,
password varchar(20) not null,
sex varchar(2),
email varchar(30)
);
//hibernate.cfg.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>
<property name= "connection.driver_class "> com.mysql.jdbc.Driver </property>
<property name= "connection.url "> jdbc:mysql://localhost/sunpool </property>
<property name= "connection.username "> root </property>
<property name= "connection.password "> root </property>
<property name= "connection.pool_size "> 10 </property>
<property name= "dialect "> org.hibernate.dialect.MySQL5Dialect </property>
<property name= "current_session_context_class "> thread </property>
<property name= "cache.provider_class "> org.hibernate.cache.NoCacheProvider </property>
<property name= "show_sql "> true </property>
<property name= "hbm2ddl.auto "> create </property>
<mapping resource= "model/service/dao/Person.hbm.xml " />
<mapping resource= "model/service/dto/User.hbm.xml " />
</session-factory>
</hibernate-configuration>
//person.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= "model.service.dao.Person " table= "person ">
<id name= "id " column= "id " type= "string ">
<generator class= "uuid.hex " />
</id>
<property name= "name " column= "name " type= "string " not-null= "true " />
<property name= "password " column= "password " type= "string " not-null= "true " />
<property name= "sex " column= "sex " type= "string " />
<property name= "email " column= "email " type= "string " />
</class>
</hibernate-mapping>
//PersonOperate.java
package model.service.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;