今天在做Hibernate中1多1双向关联映射的实例时,
保存时,报下面的错误..
检查了一下..具体Hibernate配置代码是没有问题:
Person_SX_PK_One_to_One.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="pack.java.model.Person_SX_PK_One_to_One" table="ZHT_PERSON_SX_PK_ONE_TO_ONE"> <id name="id" column="ID"> <generator class="identity"></generator> </id> <property name="name" column="NAME"></property> <property name="age" column="AGE"></property> <!-- 1对1,引用address对象, --> <one-to-one name="addressSXPKOneToOne" class="pack.java.model.Address_SX_PK_One_To_One" fetch="select" lazy="false" cascade="all"> </one-to-one> </class> </hibernate-mapping>
Address_SX_PK_One_To_One.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="pack.java.model.Address_SX_PK_One_To_One" table="ZHT_ADDRESS_SX_PK_ONE_TO_ONE"> <id column="ID" name="id"> <!-- generator 主键生成策略,配制成foreign,外键,是引用主表person的对象。 --> <generator class="foreign"> <param name="property">personSXPKOneToOne</param> </generator> </id> <property name="addressDetail" column="ADDRESSDETAIL"></property> <!-- 表示在address表中,存在外键约束,外键参考相关表person对象。 --> <one-to-one name="personSXPKOneToOne" constrained="true"></one-to-one> </class> </hibernate-mapping>
然后写了一个test类,测试时,
给person设置属性值并且给peson设置address对象,然后保存person. 现在就发生错误了.
原因是因为没有给address对象设置person对象. 所以报如下错误,。原因在于address引用了person表的主键,如果不设置.则会引用一个null值..不能进save操作,所以,需要注意。
错误信息:
Exception in thread "main" org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property: personSXPKOneToOne
at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:44)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:99)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:334)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at pack.java.test.HibernateDemoXS_PK_OneToOne.savePerson(HibernateDemoXS_PK_OneToOne.java:31)
at pack.java.test.HibernateDemoXS_PK_OneToOne.main(HibernateDemoXS_PK_OneToOne.java:15)