-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Saving two new objects is causing exception
PostPosted: Wed Jun 22, 2005 8:30 pm 
Beginner
Beginner

Joined: Fri Oct 15, 2004 9:58 pm
Posts: 25
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


Top
 Profile  
 
 Post subject: Re: Saving two new objects is causing exception
PostPosted: Thu Jun 23, 2005 5:25 am 
Newbie

Joined: Mon Jun 20, 2005 6:20 am
Posts: 7
Hi,

Your code looks like this:

Person p = new PersonImpl();
p.setIdentifier(1);
Family f = new FamilyImpl();
f.setIdentifier(2);
sessionFactory.openSession();
session.save(p);
session.save(f);

It should look like this:

Family f = new FamilyImpl();
f.setIdentifier(2);
Person p = new PersonImpl();
p.setIdentifier(1);
p.setFamily(f);

sessionFactory.openSession();
session.save(p);

There is a rule which says that as long af an object is reachable from the object which is about to be saved it will be saved too. I'm not sure if that is true when cascade isn't set. You'll have to try the code above out


Top
 Profile  
 
 Post subject: Got it..
PostPosted: Thu Jun 23, 2005 1:40 pm 
Beginner
Beginner

Joined: Fri Oct 15, 2004 9:58 pm
Posts: 25
I forgot about the save-update cascade, and was thinking if there is any way to do it without it. I don't think it is possible to do.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 2:04 pm 
Regular
Regular

Joined: Thu Apr 21, 2005 9:05 am
Posts: 50
Location: Boston, U.S
Without cascade attribute being set, it will not save the data
in the child table.

So cascade is required.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 3:25 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
mahikty wrote:
Without cascade attribute being set, it will not save the data
in the child table.

So cascade is required.


Or you could just save the Family first, then the Person.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.