-->
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 not to save transient objects if it already exists in DB
PostPosted: Tue Feb 03, 2004 4:57 pm 
Beginner
Beginner

Joined: Fri Jan 23, 2004 10:26 am
Posts: 21
There goes another question.
if i do not want to save a transient object, because there is an object caryying same information exists in DB, what should i do? there is an example down,
Code:
Persistent classes:

public class PCard{
    private String id;
    private String serial;
    .....
}

public class PSession {
    private String id;
    private Date startTime;
    private Date endTime;
    private PCard card;
    ......
}


when i create a Psession object including a PCard object, before i want to save the PCard object, i want to check if there is a PCard in the database carrying the same serial number. if it already exist, i would like to put the existing PCard's info to the session and not save a new card for that. how can i do that? there is a code example below but strangely it is producing double identical PCard objects. is there any better way of doing this? if i simply refuse to save transient card object, it is giving an error like "you have to save the transient object before flush bla bla bla"

Code:
..........
      // Open H Session
      Session sess = sf.openSession();
      // Save
      Transaction t = sess.beginTransaction();
      PCard card = m.getSession().getCard();
      if (card != null) {
         String serial = card.getSerial();
         Query q = sess.createQuery("from PCard as card " +
                 "where card.serial=:serial");
         q.setParameter("serial", serial);
         List result = q.list();
         if (result.size() > 0)  {
            // if i do not write code line below, it gives exception.
            m.getSession.setCard((PCard)result.get(0));
            System.out.println("Card already exist");
         }
         else
           sess.save(card);
      }
      sess.save(m.getSession());
..........


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2004 6:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I suggest you simply use session.saveOrUpdate() ...


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.