-->
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.  [ 7 posts ] 
Author Message
 Post subject: Cascade save() -> "the given object has a null ident
PostPosted: Fri Mar 17, 2006 2:46 am 
Regular
Regular

Joined: Sat Jan 07, 2006 8:30 pm
Posts: 68
This is the simple Parent/Child problem.
Both Parent and Child have pk as Integer id

I need to create/insert a Parent along with many children in same time

Code:
Parent parent = new Parent("Parent");
parent.addChild( new Child("First") )
parent.addChild( new Child("Second") )
session.save( parent )

where addChild is bi-directional parent is set in child


Parent mapping
Code:
<class name="Parent" table="parent" schema="public">
        <id name="id" type="integer">
            <column name="id" />
            <generator class="sequence">
      <param name="sequence">parent_id_seq</param>
       </generator>
        </id>
...

        <set name="children" table="child" lazy="true" inverse="true" cascade="all" sort="unsorted">
   <key column="id"/>
      <one-to-many class="Child" />
   </set>   
</class>


Child mapping
Code:
<class name="Child" table="child" schema="public">
   <id name="id" type="integer" unsaved-value="null">
       <column name="id" />
       <generator class="sequence">
         <param name="sequence">child_id_seq</param>
      </generator>
   </id>
   ...

   <many-to-one name="parent" class="Parent" column="id" insert="false" update="false" lazy="false"/>

</class>



Well that all to it when I run the code it get the following output

Code:

2006-03-17 00:15:09,140  DEBUG  org.hibernate.impl.SessionImpl.<init>(SessionImpl.java:273) - opened session at timestamp: 4679991743037440
2006-03-17 00:15:11,078  DEBUG  org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:54) - begin
2006-03-17 00:15:11,078  DEBUG  org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:358) - opening JDBC connection
2006-03-17 00:15:11,078  DEBUG  org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:59) - current autocommit status: true
2006-03-17 00:15:11,093  DEBUG  org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:62) - disabling autocommit
2006-03-17 00:15:11,093  DEBUG  org.hibernate.jdbc.JDBCContext.afterTransactionBegin(JDBCContext.java:190) - after transaction begin
2006-03-17 00:15:11,109  DEBUG  org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:161) - saving transient instance
2006-03-17 00:15:11,109  DEBUG  org.hibernate.jdbc.AbstractBatcher.logOpenPreparedStatement(AbstractBatcher.java:311) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2006-03-17 00:15:11,109  DEBUG  org.hibernate.jdbc.AbstractBatcher.log(AbstractBatcher.java:346) - select nextval ('parent_id_seq')
2006-03-17 00:15:11,109  DEBUG  org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:424) - preparing statement
2006-03-17 00:15:11,109  DEBUG  org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:82) - Sequence identifier generated: 186
2006-03-17 00:15:11,109  DEBUG  org.hibernate.jdbc.AbstractBatcher.logClosePreparedStatement(AbstractBatcher.java:319) - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2006-03-17 00:15:11,125  DEBUG  org.hibernate.jdbc.AbstractBatcher.closePreparedStatement(AbstractBatcher.java:470) - closing statement
2006-03-17 00:15:11,125  DEBUG  org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:106) - generated identifier: 186, using strategy: org.hibernate.id.SequenceGenerator
2006-03-17 00:15:11,125  DEBUG  org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:139) - saving [Parent#186]
2006-03-17 00:15:11,125  DEBUG  org.hibernate.engine.Cascade.cascade(Cascade.java:237) - processing cascade ACTION_SAVE_UPDATE for: Parent
2006-03-17 00:15:11,125  DEBUG  org.hibernate.engine.Cascade.cascade(Cascade.java:259) - done processing cascade ACTION_SAVE_UPDATE for: Parent
2006-03-17 00:15:11,125  DEBUG  org.hibernate.event.def.WrapVisitor.processArrayOrNewCollection(WrapVisitor.java:87) - Wrapped collection in role: Parent.children
2006-03-17 00:15:11,156  DEBUG  org.hibernate.engine.Cascade.cascade(Cascade.java:237) - processing cascade ACTION_SAVE_UPDATE for: Parent
2006-03-17 00:15:11,171  DEBUG  org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:285) - cascade ACTION_SAVE_UPDATE for collection: Parent.children
2006-03-17 00:15:11,171  DEBUG  org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:133) - cascading to saveOrUpdate: Child
2006-03-17 00:15:11,171  DEBUG  org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:471) - detached instance of: Child
2006-03-17 00:15:11,171  DEBUG  org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:202) - updating detached instance
                                org.hibernate.TransientObjectException: The given object has a null identifier: Child




I see no error but maybe I got tired to look at this code over and over again.

What do you see ?

Q


Last edited by qmaster on Mon Mar 20, 2006 2:37 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 6:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Read the parent child section of the manual again.
You need to understand what inverse=true means and how this affects the bi-directional relationship.

Hint: as soon as you said inverse=true on the one-to-many side, eg the set, you tell hibernate that the responcible relation updates occur in the other side, eg, the Many-to-one side. Thus you have two problems:
1) insert and update is false on the many-to-one which disables the update so no relationship can be created.
2) You don't set the Many-to-one in you setup code.


Top
 Profile  
 
 Post subject: Re:
PostPosted: Fri Mar 17, 2006 10:37 am 
Regular
Regular

Joined: Sat Jan 07, 2006 8:30 pm
Posts: 68
Forgot to mentioned that I tried also inverse="false" in one-to-many so I'd let Parent to manage the link but no changes.

I'm not sure what David tries to say with
>2) You don't set the Many-to-one in you setup code.

When I do parent.add( new Child() ) it does that does that

Parent has this method

public void add( Child c )
{
children.add( c );
c.setParent( this );
}


Your thoughts are much appreciated!

Q


Top
 Profile  
 
 Post subject: Re:
PostPosted: Fri Mar 17, 2006 10:46 am 
Regular
Regular

Joined: Sat Jan 07, 2006 8:30 pm
Posts: 68
A very strange thing is that unsaved-value="null" is not triggering the sequence call. Most likely is a problem in the relationship but I don't see it...


Top
 Profile  
 
 Post subject: Anyone ?
PostPosted: Sun Mar 19, 2006 11:39 am 
Regular
Regular

Joined: Sat Jan 07, 2006 8:30 pm
Posts: 68
Can anyone tell me what's wrong ?

I tried inverse=true and inverse=false,
also I removed the insert and update attributes in many-to-one. I tried almost everything. Somehow the save(parent) triggers saveOrUpdate !!!!!(???)

Should I first save the parent without the kids and then add the kids ?
As i said, unsaved-value="null" should trigger the insert since the id *are* null when I try to save the parent.

Should I give entirely up this approach and do the composite one ? Why would that work ?

Just your thoughts. I read the documentation over and over again.
This case, where I generate the ids with a sequence are briefly mentioned in the Parent/Child chapter.


Briefly, what I want is to be able to insert/update the kids from parent.
The kid update/insert should not alter the parent in anyway thus initially I've put there insert=false update=false

Anyone ? If you have an example that has been *tested* please let me k now.

TIA,
Q


Top
 Profile  
 
 Post subject: Echo
PostPosted: Mon Mar 20, 2006 2:36 pm 
Regular
Regular

Joined: Sat Jan 07, 2006 8:30 pm
Posts: 68
Anybody here ? Anybody here ?


Top
 Profile  
 
 Post subject: Hibernate session interceptor
PostPosted: Wed Mar 22, 2006 1:24 pm 
Regular
Regular

Joined: Sat Jan 07, 2006 8:30 pm
Posts: 68
I found the answer to my problem.

Wasn't the mapping. If you ever implement a Hibernate Session Interceptor make sure you return null in isTransient() overriden method !

I was returning Boolean.FALSE and that screw the cascading for save-updated even thought the session.save( parent ) worked great!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.