when save parent, the children do not get saved. URGENT PLEASE.

Discussion in 'Java' started by leela1, Oct 25, 2007.

  1. leela1

    leela1 New Member

    Joined:
    Oct 25, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I have a question and it could be a very simple/silly question for you. Please reply back to my post immediately as soon as possible. Please treat this as very urgent. Thanks a lot for the help in advance.

    When I save a parent, the children do not get saved, unless i save them independently. Is there anyway that i can bypass saving children independently? I just wanted to make one call to save parent which should take care of saving children as well. please guide me. This is a unidirectional one-to-many mapping.


    Code:
    <hibernate-mapping> 
    <class name="Parent" lazy="true" proxy="Parent" table="parent"> 
    <cache usage="read-write"/> 
    <id name="id" access="field" type="long" > 
    <generator class="MyGenerator"></generator> 
    </id> 
    <!-- some properties here --> 
    <set name="children" cascade="all,delete-orphan" 
    access="field" 
    table="child"> 
    <cache usage="read-write"/> 
    <key column="parentid" not-null="true"/> 
    <one-to-many class="Child"/> 
    </set> 
    </class> 
    </hibernate-mapping> 
    
    here is the code :

    Code:
    Session session1 = sf.openSession(); 
    Parent parent1 = (Parent )session1.load(Parent.class, 123); 
    parent parent2 = new Parent(parent1); 
    session1 .flush(); 
    Session session2 = sf.openSession(); 
    session2 .save(parent2); 
    session2 .flush(); 
    
    Please let me know what is wrong here. Thanks a lot.
     
    Last edited by a moderator: Oct 26, 2007

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice