-->
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: Persistence with reachability - Cascade option.
PostPosted: Wed Jun 13, 2007 2:00 pm 
Newbie

Joined: Mon Jun 11, 2007 2:58 pm
Posts: 11
Hi
I have a question regarding persisting entites:

Lets say i have two tables,

TABLE A, TABLE B and the corresponding mapping classes are CLASS A and CLASS B respectively. TABLE B has a Foreign Key which refers to the Primary Key of TABLE A.
And CLASS A has a reference (property) of the CLASS B. And CLASS B has a reference (property) of the CLASS A. So the mapping looks like the following:



Code:
<class name="CLASSA" table="TABLEA">
      <id name="ID" column="ID">
           <generator class="assigned" />
        </id>
                ..................
      ...some other properties....
                .................
      
      <one-to-one name="classB" class="CLASSB" cascade="persist"/>

   </class>

<class name="CLASSB" table="TABLEB">

      <id name="ID" column="ID">
         <generator class="foreign">
            <param name="property">classA</param>
         </generator>
      </id>      
      
      <one-to-one name="classA" class="CLASSA" constrained="true"/>

                ..................
      ...some other properties....
                .................

   </class>



If during persistence, if a save() method is called by passing only an instance of "CLASSA", does hibernate inserts into two tables "TABLEA" and "TABLEB" or we have to call save() method explicitly for two entities i.e., CLASS A and CLASS B.

But based on hibernate documentation for parent/child entities, if cascade="persist" option is used, then hibernate will take care of inserting into two tables. So, does CLASS B needs to extend CLASS A?

Can some body answer this?


Top
 Profile  
 
 Post subject: Re:
PostPosted: Wed Jun 13, 2007 3:36 pm 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
You will need to put a many-to-one with unique=true on one side for this to work.

Code:
<class name="CLASSB" table="TABLEB">

      <id name="ID" column="ID">
         <generator class="foreign">
            <param name="property">classA</param>
         </generator>
      </id>     
     
     <many-to-one name="classA" class="ClassA" unique="true"/>

                ..................
      ...some other properties....
                .................

   </class>



Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 13, 2007 4:32 pm 
Newbie

Joined: Mon Jun 11, 2007 2:58 pm
Posts: 11
Nope, it did not worked....


Top
 Profile  
 
 Post subject: Re:
PostPosted: Thu Jun 14, 2007 3:27 am 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
please use session.persist() instead of save.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 15, 2007 5:08 pm 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
I think we solved your problem. please give appropriate credit.


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.