I have simple program,
There are two objects P and F with P having a reference to F. The reference has not-null set to true and cascade to nothing.
I create two objects outside of Hibernate session and then call save on P first and then F. This causes the not-null property references a null or transient value: P.F. The stack trace is shown below.
Seemingly during nullability check, the reference is nulled out because it is transient (it is not yet attached to the session) and so this exception is thrown.
I tried attaching these two objects to the session using lock(P, LockMode.None) but this will not work if P has a collection of newly created objects. Exception is thrown "reassociated object has dirty collection reference (or an array)" from OnLockVisitor:67.
Can any one help please?
Hibernate version: 3.0.1
Mapping documents: <class name="FamilyImpl" table="O_FamilyImpl">
<id name="persistentIdentifier" type="java.lang.Long" column="O_persistentIdentifier" access="property"/>
<property name="Income" column="O_Income" type="java.lang.Integer" not-null="true" unique="false" access="property"/>
<property name="Name" column="O_Name" type="java.lang.String" not-null="true" unique="false" access="property"/>
</class>
<class name="PersonImpl" table="O_PersonImpl">
<id name="persistentIdentifier" type="java.lang.Long" column="O_persistentIdentifier" access="property"/>
<property name="Name" column="O_Name" type="java.lang.String" not-null="true" unique="false" access="property"/>
<many-to-one name="Family" class="FamilyImpl" column="FK_Family_persistentIdentifi" fetch="join" not-null="true" access="property"/>
</class>
Code between sessionFactory.openSession() and session.close():
Person p = new PersonImpl();
p.setIdentifier(1);
Family f = new FamilyImpl();
f.setIdentifier(2);
sessionFactory.openSession();
session.save(p);
session.save(f);
session.flush();
commit();
close();
Full stack trace of any exception that occurs:
org.hibernate.PropertyValueException: not-null property references a null or transient value: Person.family
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:234)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:158)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:445)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:440)
Name and version of the database you are using:
Oracle 1og
The generated SQL (show_sql=true):
17:10:00,176 DEBUG SchemaExport:162 - create table O_FamilyImpl (
O_persistentIdentifier number(19,0) not null,
O_Income number(10,0) not null,
O_Name varchar2(255) not null,
primary key (O_persistentIdentifier)
)
17:10:00,285 DEBUG SchemaExport:162 - create table O_PersonImpl (
O_persistentIdentifier number(19,0) not null,
O_Name varchar2(255) not null,
FK_Family_persistentIdentifi number(19,0) not null,
primary key (O_persistentIdentifier)
)
Debug level Hibernate log excerpt:
17:10:01,191 INFO LogService:206 - Inserting new object PersonImpl[1]
17:10:01,207 DEBUG DefaultSaveOrUpdateEventListener:159 - saving transient instance
17:10:01,207 DEBUG AbstractSaveEventListener:89 - generated identifier: 1, using strategy: org.hibernate.id.Assigned
17:10:01,207 DEBUG AbstractSaveEventListener:132 - saving [PersonImpl#1]
Thanks,
Prasad
|