两张表MEDLINE_USER,MEDLINE_USER_INFO一对一的关系,配置如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2010-6-30 16:53:04 by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping>
<class name="org.scbit.lsbi.biomedab.pojo.User" table="MEDLINE_USER" schema="LSBI">
<id name="id" type="integer">
<column name="ID" precision="10" scale="0" />
<generator class="sequence">
<param name="sequence">SQ_MEDLINE_USER</param>
</generator>
</id>
<property name="name" type="string">
<column name="NAME" length="50" not-null="true" />
</property>
<property name="password" type="string">
<column name="PASSWORD" length="32" not-null="true" />
</property>
<one-to-one name="userInfo" class="org.scbit.lsbi.biomedab.pojo.UserInfo" cascade="all"/>
</class>
</hibernate-mapping>
hibernate-mapping>
<class name="org.scbit.lsbi.biomedab.pojo.UserInfo" table="MEDLINE_USER_INFO" schema="LSBI">
<id name="id" type="integer" unsaved-value="0">
<column name="ID" precision="10" scale="0" />
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<one-to-one name="user" class="org.scbit.lsbi.biomedab.pojo.User" constrained="true" cascade="none"/>
<property name="loginNum" type="int">
<column name="LOGIN_NUM" precision="4" scale="0" not-null="true" />
</property>
<property name="lastLoginTime" type="date">
<column name="LAST_LOGIN_TIME" length="7" not-null="true" />
</property>
<property name="point" type="int">
<column name="POINT" precision="6" scale="0" not-null="true" />
</property>
<property name="activeCode" type="string">
<column name="ACTIVE_CODE" length="20" />
</property>
</class>
</hibernate-mapping>
级联代码如下: User user=new User();
user.setPassword("ddd");
user.setCreatedDate(new Date());
user.setStatus("ss");
UserInfo userInfo = new UserInfo();
userInfo.setLastLoginTime(new Date());
userInfo.setLoginNum(0);
userInfo.setPoint(0);
userInfo.setUser(user);
user.setUserInfo(userInfo);
userManager.saveOrUpdate(user);上面的代码出以下错误:Hibernate: select SQ_MEDLINE_USER.nextval from dual
Hibernate: insert into LSBI.MEDLINE_USER (NAME, PASSWORD, EMAIL, PHONE, ADDRESS, STATUS, CREATED_DATE, ID) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into LSBI.MEDLINE_USER_INFO (LOGIN_NUM, LAST_LOGIN_TIME, POINT, ACTIVE_CODE, ID) values (?, ?, ?, ?, ?)
2010-07-06 19:21:14,181 [ERROR] - ORA-02291: integrity constraint (LSBI.FK_MEDLINE__REFERENCE_MEDLINE_) violated - parent key not found2010-07-06 19:21:14,181 [ERROR] - ORA-02291: integrity constraint (LSBI.FK_MEDLINE__REFERENCE_MEDLINE_) violated - parent key not found2010-07-06 19:21:14,196 [ERROR] - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
后来我又改成非级联方式:先保存user 在保存user_info 有如下问题: user.setPassword(loginPwd);
user.setCreatedDate(new Date());
user.setStatus(UserStatusEnum.AVAILABLE.name());
userManager.saveOrUpdate(user);
//上面的user保存成功,eclispe中debug看到成功后的user的id和数据库中实际保存的ID的值不一样,刚才保存后程序中打出来的id是787,而数据库中却是788.郁闷,每次测试时都不一样。不知什么原因导致。
UserInfo userInfo = new UserInfo();
userInfo.setLastLoginTime(new Date());
userInfo.setLoginNum(0);
userInfo.setPoint(0);
userInfo.setId(user.getId());
userInfo.setUser(user);
user.setUserInfo(userInfo);
userManager.saveOrUpdate(userInfo);
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2010-6-30 16:53:04 by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping>
<class name="org.scbit.lsbi.biomedab.pojo.User" table="MEDLINE_USER" schema="LSBI">
<id name="id" type="integer">
<column name="ID" precision="10" scale="0" />
<generator class="sequence">
<param name="sequence">SQ_MEDLINE_USER</param>
</generator>
</id>
<property name="name" type="string">
<column name="NAME" length="50" not-null="true" />
</property>
<property name="password" type="string">
<column name="PASSWORD" length="32" not-null="true" />
</property>
<one-to-one name="userInfo" class="org.scbit.lsbi.biomedab.pojo.UserInfo" cascade="all"/>
</class>
</hibernate-mapping>
hibernate-mapping>
<class name="org.scbit.lsbi.biomedab.pojo.UserInfo" table="MEDLINE_USER_INFO" schema="LSBI">
<id name="id" type="integer" unsaved-value="0">
<column name="ID" precision="10" scale="0" />
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<one-to-one name="user" class="org.scbit.lsbi.biomedab.pojo.User" constrained="true" cascade="none"/>
<property name="loginNum" type="int">
<column name="LOGIN_NUM" precision="4" scale="0" not-null="true" />
</property>
<property name="lastLoginTime" type="date">
<column name="LAST_LOGIN_TIME" length="7" not-null="true" />
</property>
<property name="point" type="int">
<column name="POINT" precision="6" scale="0" not-null="true" />
</property>
<property name="activeCode" type="string">
<column name="ACTIVE_CODE" length="20" />
</property>
</class>
</hibernate-mapping>
级联代码如下: User user=new User();
user.setPassword("ddd");
user.setCreatedDate(new Date());
user.setStatus("ss");
UserInfo userInfo = new UserInfo();
userInfo.setLastLoginTime(new Date());
userInfo.setLoginNum(0);
userInfo.setPoint(0);
userInfo.setUser(user);
user.setUserInfo(userInfo);
userManager.saveOrUpdate(user);上面的代码出以下错误:Hibernate: select SQ_MEDLINE_USER.nextval from dual
Hibernate: insert into LSBI.MEDLINE_USER (NAME, PASSWORD, EMAIL, PHONE, ADDRESS, STATUS, CREATED_DATE, ID) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into LSBI.MEDLINE_USER_INFO (LOGIN_NUM, LAST_LOGIN_TIME, POINT, ACTIVE_CODE, ID) values (?, ?, ?, ?, ?)
2010-07-06 19:21:14,181 [ERROR] - ORA-02291: integrity constraint (LSBI.FK_MEDLINE__REFERENCE_MEDLINE_) violated - parent key not found2010-07-06 19:21:14,181 [ERROR] - ORA-02291: integrity constraint (LSBI.FK_MEDLINE__REFERENCE_MEDLINE_) violated - parent key not found2010-07-06 19:21:14,196 [ERROR] - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
后来我又改成非级联方式:先保存user 在保存user_info 有如下问题: user.setPassword(loginPwd);
user.setCreatedDate(new Date());
user.setStatus(UserStatusEnum.AVAILABLE.name());
userManager.saveOrUpdate(user);
//上面的user保存成功,eclispe中debug看到成功后的user的id和数据库中实际保存的ID的值不一样,刚才保存后程序中打出来的id是787,而数据库中却是788.郁闷,每次测试时都不一样。不知什么原因导致。
UserInfo userInfo = new UserInfo();
userInfo.setLastLoginTime(new Date());
userInfo.setLoginNum(0);
userInfo.setPoint(0);
userInfo.setId(user.getId());
userInfo.setUser(user);
user.setUserInfo(userInfo);
userManager.saveOrUpdate(userInfo);
在SQL SERVER中,integrity字段是系统自动生成的。不需要赋值。也插不进去值。
这句话的意思是主键违反了integrity 生成方式,插入失败。找不到主键
2010-07-06 19:21:14,196 [ERROR] - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
找不到主键。
sesson跟数据库状态不同步。
保存后程序中打出来的id是787,而数据库中却是788
你插入sequence当ID的时候,感觉你是取了2次。打印了的时候取了一次,当保存到数据库的时候又取了一次。
刚上面的看错了 看成了uniqueidentifier。最近被这搞的头晕。说错了,对不起。
User user=new User();
user.setPassword("ddd");
user.setCreatedDate(new Date());
user.setStatus("ss");
UserInfo userInfo = new UserInfo();
userInfo.setLastLoginTime(new Date());
userInfo.setLoginNum(0);
userInfo.setPoint(0);
userInfo.setUser(user);
userManager.save(userInfo);
实验下!
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="close.hibernate.vo.Emp" table="emp">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="age" type="java.lang.Integer">
<column name="age" not-null="true" />
</property>
<property name="sex" type="java.lang.String">
<column name="sex" not-null="true" />
</property>
<one-to-one name="empinfo" class="close.hibernate.vo.EmpInfo" cascade="all"/>
</class>
</hibernate-mapping>
<?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="close.hibernate.vo.EmpInfo" table="empinfo">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="foreign">
<param name="property">emp</param>
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" not-null="true" />
</property>
<one-to-one name="emp" class="close.hibernate.vo.Emp" />
</class>
</hibernate-mapping>public static void main(String[] args){
Configuration cfg=new Configuration().configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction ta=null;
try{
ta=session.beginTransaction();
Emp emp=new Emp();
emp.setAge(23);
emp.setSex("man");
EmpInfo empinfo=new EmpInfo();
empinfo.setName("closewubq");
empinfo.setEmp(emp);
session.save(empinfo);
ta.commit();
System.out.println("success"); }catch(Exception e){
ta.rollback();
System.out.println(e.getMessage());
}
finally{
session.close();
}
}
问题已解决,原来是我的数据库建表语句是通过powerDesigner生成的,他在建sequence时同给每个sequence建了触发器,导致只要执行了sql.nextval语句,触发器就将sequence加一,以致于保存user对象后对象中的id于数据库中插入值的id不一致,所以当保存userInfo时取得对象中的id在数据库中不存在对应的user,所以外键关系出错。