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());
..........