Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: EJB3.0 equivalent of CascadeType.SAVE_UPDATE
PostPosted: Sat Aug 19, 2006 8:32 pm 
Newbie

Joined: Sat Aug 19, 2006 8:09 pm
Posts: 1
Hi,
I have a question about the difference between org.hibernate.annotations.CascadeType.SAVE_UPDATE and javax.persistence.CascadeType.PERSIST. I've understood from messages on this forum that session.persist(obj) is more or less the same thing as session.saveOrUpdate(obj), however, they seem to differ in one extremely important detail:

I've set up a class Person that has a property of class PersonType with a ManyToOne mapping (using @ManyToOne), the idea being that more than one person can be of a particular type. In a test, I tried to instantiate several of these classes like so:
Code:
PersonType type = new PersonType("Member");
Person person1 = new Person("Bill", "Joy", "billa@joy.com", type);
Person person2 = new Person("Bill", "Joy", "billb@joy.com", type);
Person person3 = new Person("Bill", "Joy", "billc@joy.com", type);

dao.savePerson(person1);
dao.savePerson(person2);
dao.savePerson(person3);


The important part is that the same PersonType object is used on all the objects. When I use the following code in conjunction with session.saveOrUpdate(obj) everything works.
Code:
@ManyToOne
@Cascade(CascadeType.SAVE_UPDATE)
@JoinColumn(name = "type_fk", nullable = false)
public PersonType getType() {
  return personType;
}


However, when I use the following code in conjunction with session.persist(obj) I get a PersistenObjectException: detached entity passed to persist
Code:
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "type_fk", nullable = false)
public PersonType getType() {
  return personType;
}


The detached object is the PersonType object in Person, which is detached from the session after I saved it the first time, and then cannot be used for the second save operation.

What my question comes down to is this: why does the persist method, under similar cascading circumstances, fail to reattache the object automatically whereas the saveOrUpdate does achieve this? For objects without dependent persisted objects persist() seems to work like saveOrUpdate(), but this seems like a pretty big (and inconvenient difference). Is there any way to achieve the saveOrUpdate() functionality with just EJB3 annotations?

I'm using hibernate version 3.2.0.CR2 and annotations version 3.2.0.CR1.

Regards,
Maarten


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 20, 2006 1:24 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7179
Location: Atlanta, USA
the closest method for saveOrUpdate in EJB 3.0 is merge, not persist

_________________
Emmanuel
Check Hibernate Search in Action out


Top
 Profile  
 
 Post subject: EJB3.0 equivalent of CascadeType.SAVE_UPDATE
PostPosted: Wed Sep 27, 2006 4:11 am 
Newbie

Joined: Mon Mar 13, 2006 3:12 pm
Posts: 2
emmanuel wrote:
the closest method for saveOrUpdate in EJB 3.0 is merge, not persist


Hi Emmanuel,

could your please render more precisely the difference between saveOrUpdate and merge?

Regards,
Christoph


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 11:23 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7179
Location: Atlanta, USA
http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#objectstate-saveorupdate

_________________
Emmanuel
Check Hibernate Search in Action out


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 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.