-->
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.  [ 2 posts ] 
Author Message
 Post subject: How can a new child record be added to an existing parent?
PostPosted: Wed Jul 22, 2009 8:04 am 
Newbie

Joined: Mon Jul 13, 2009 7:47 am
Posts: 4
Please no offence if this is a repeat query.

How can a new child record be inserted into a child table
for which a parent is already existing?
(i.e How can I add record no 2 with status of 'Executed'
to child table only?)

My code below does not add a new Child.

Code:
Trade (Parent)           TradeDetails(Child)
------------------------------------------------------
ID                             ID   status  trade_id(Foreign Key
-------------------------------------------------------
1                               1    New          1
                                 2   Executed    1


        Configuration configuration = new Configuration();
        SessionFactory  sessionFactory = configuration.configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
       
        // 1.Parent.
        Trade trade = new Trade();
       
        // Child.
        TradeDetails tDetails = new TradeDetails();
        tDetails.setStatus("New");
        tDetails.setTrade(trade);    // Association
       
        // Create a set of status
        Set setOfStatus = new HashSet();
        setOfStatus.add(tDetails);
       
        // Add to Parent.
        trade.setStatuses(setOfStatus);

        session.save(trade);
        transaction.commit();


        // 2).Add a new Child record to an existing parent id.
     
        trade = (Trade) session.load(Trade.class, 30);
       
        // Child
        TradeDetails tDet = new TradeDetails();
        tDet.setStatus("Executed");
        tDet.setTrade(trade);
       
        setOfStatus.add(tDet);
       
        trade.setStatuses(setOfStatus);
       
        // Save child
        session.saveOrUpdate(tDet);
       
        session.flush();
       

       


Top
 Profile  
 
 Post subject: Re: How can a new child record be added to an existing parent?
PostPosted: Wed Jul 22, 2009 8:43 am 
Newbie

Joined: Mon Jul 13, 2009 7:47 am
Posts: 4
This is now done.
Thanks


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