-->
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: One-2-Many relationship transitive persistence with Auto-key
PostPosted: Tue Jul 20, 2004 2:24 pm 
Regular
Regular

Joined: Thu Jul 01, 2004 12:13 am
Posts: 68
Location: San Diego, CA
Simply question: I have a one-to-many set-up between two tables. My parent table has its primary key generated through an Oracle sequence. However, when Hibernate tries to populate the child table, it complains the foreign key is null which is the PK from the parent. I am presuming this is because it hasn't been generated.

I was hoping to push both records to each table per transitive persistence, but I fear now that I will first have to save the parent record and then subsequently add the child record once the parent is persisted. Is this assumption correct?

Here are my relationships:

Child.hbm:

Code:
       <many-to-one
         name="claClmClaim"
         class="ClmClaim"
         not-null="true"
         cascade="none"
      >
         <column name="CLA_CLM_CLAIM_ID"/>
      </many-to-one>


Parent.hbm:

Code:
      
      <id
         name="id"
         type="java.lang.Long"
         column="CLAIM_ID"
      >
         <generator class="sequence">
            <param name="sequence">CLAIM_ID_SEQ</param>
         </generator>
      </id>
...

                <set
         inverse="true"
         name="activityLogSet"
         cascade="all-delete-orphan"
      >
         <key column="CLA_CLM_CLAIM_ID" />
         <one-to-many class="ClmClaimActivityLog" />
      </set>

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 3:47 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
save parent first, it will hit the sequence to fill the id, then do what you want with children

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: Saved...
PostPosted: Tue Jul 20, 2004 4:17 pm 
Regular
Regular

Joined: Thu Jul 01, 2004 12:13 am
Posts: 68
Location: San Diego, CA
anthony wrote:
save parent first, it will hit the sequence to fill the id, then do what you want with children


Yup, that's what I'm doing, but in my value object for the table I have a Set with my children inside it. So when I save it complains. Do you mean to say save the parent with a NULL Set first and then go back and add it? I thought cascade set to "all" is supposed to handle this for me transitively?

Also, when I turn show_sql on, I see the SELECT for the sequence followed by the two inserts for the parent and child table sequentially. Shouldn't Hibernate now know the value for the PK per that SELECT?

TIA,
Lou

Save code as follows:

Code:
         
protected Serializable save(Object obj) throws DaoException {
        Session s = null;
        Monitor mon = MonitorFactory.start(this.getClass().getName() + ":save");
        try {
            s = getSession();
            Serializable rtn = s.save(obj);
            s.flush();
            return rtn;
        } catch (HibernateException e) {
            throw new DaoException(e);
        } finally {
            closeSession(s);
            mon.stop();
        }
    }


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 4:24 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
it have to do with "inverse" attribute more than cascade, read the chapter talking about "parent child relation ship"
one end of the association must manage the link... it is explained

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 4:52 pm 
Regular
Regular

Joined: Thu Jul 01, 2004 12:13 am
Posts: 68
Location: San Diego, CA
Well the inverse setting on the set is either true or false. The reference guide on pp. 49 clearly states that this should be set to "true" when dealing with one-2-many relationships if the key column is set to NOT NULL, which mine is. Nonetheless, I tried false but to no avail.

Looking the examples in this chapter, mine mirrors it with the exception of lazy loading, which I don't want to consider at this time.

Still unclear, whether I need to start off with a NULL Set and then add it after the Parent is created. This doesn't seem right to me.

Lou


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.