-->
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: object references an unsaved transient instance
PostPosted: Tue Apr 07, 2009 5:56 am 
Newbie

Joined: Tue Dec 30, 2008 6:24 pm
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.3.1
Mapping documents:
Annotations
Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
ERROR AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: compta.entities.Poste
...
Name and version of the database you are using:
Mysql 5.5
The generated SQL (show_sql=true):
Hibernate: update lignecompta set accountid=?, dtpay=?, libelle=?, pointed=?, posteid=?, valeur=? where id=?
I want to update an Object called lignecompta.
This object contains another object called Poste, link with posteid.
This object is directly got from the DB and put in the lignecompta object.
An when I want to update the "lignecompta" object, I got the error.
The reason is really because I set the Poste object from a previously loaded object. I can "evict" it from the session, but it's not clean.
--------- Annotations for ligne compta ------------
@Entity
@Table (name="lignecompta")
public class LigneCompta implements Serializable{
...
@ManyToOne(optional=true)
@JoinColumn(name="posteid", nullable=true)
public Poste getPoste() {
return poste;
}
}
-------- Thes Poste object is got using: ---------
public List<Poste> getPostes(User u) {
Session session = this.getSession();
//session.beginTransaction();
Criteria crit = session.createCriteria(Poste.class);
crit.add(Restrictions.eq("ownerid", u.getId()));
List<Poste> postes= crit.list();
return postes;
}
-------- Ths lignecompta object is saved using -------
@Transactional // spring transactionnal
public void updateLigne(LigneCompta lc) {
Session session = this.getSession();
session.update(lc); // error is raised here
}

Hope I will get help because I don't see the pb.

Regards

Francillo


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 07, 2009 6:13 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Please modify as mention below and check it.
Code:
--------- Annotations for ligne compta ------------
@Entity
@Table (name="lignecompta")
public class LigneCompta implements Serializable{
...
@ManyToOne(optional=true)
@org.hibernate.annotations.Cascade(CascadeType.save_update) <----add this line
@JoinColumn(name="posteid", nullable=true)
public Poste getPoste() {
return poste;
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 07, 2009 6:37 am 
Newbie

Joined: Tue Dec 30, 2008 6:24 pm
Posts: 5
Thanks for your answer,
It Works.
Unfortunalty, when I do not use a loaded (but existing in DB) "Poste" object (i.e. created with a new), I get:

dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

Do I have to load the object before (because I know the Id), or there is another Cascade option?

To sum up, my poste object comes from:
- a loaded object from DB (solved with the save_update option)
- a newly created object (not in session) ==> Crash

Regards

Francillo


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 07, 2009 7:25 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Hi, Wait
your understanding is wrong

Quote:
To sum up, my poste object comes from:
- a loaded object from DB (solved with the save_update option)
//a loaded object from DB doesn't required save_update option
- a newly created object (not in session) ==> Crash
//Cascading option help up to solve this problem, which you did before.



for more information go to
http://www.hibernate.org/hib_docs/refer ... itive.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 07, 2009 8:18 am 
Newbie

Joined: Tue Dec 30, 2008 6:24 pm
Posts: 5
Hello,
Hum, I confirm the following behaviour in my case:

+-------------------------------+
+ lc=new LigneCompta();
+ lc.set(..);
+ p=new Poste();
+ p.setId(posteId);
+ lc.setPoste(p);
+ LigneComptaDAO.update(lc)
+---------------------------------+
==> With Save_Update: /!\ Constraint violation
==> No save Update: OK

+-------------------------------+
+ lc=new LigneCompta();
+ lc.set(..);
+ p=PosteDAO.get(posteId);
+ lc.setPoste(p);
+ LigneComptaDAO.update(lc)
+--------------------------------+
==> With Save_Update: OK
==> No save Update: /!\ save transient object before flush

In both case, the "Poste" object is known to be in DB.
Note that LigneCompta is alway created from a new.

public Poste PosteDAO.get(long posteId){
Session session = this.getSession();
Criteria crit = session.createCriteria(c);
crit.add(Restrictions.eq("id", posteId));
return (Poste)crit.uniqueResult();
}

@Transactional
public void LigneComptaDAO.update(LigneCompta lc){
Session session = this.getSession();
session.update(lc);
}

Regards.
Francillo


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.