-->
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.  [ 4 posts ] 
Author Message
 Post subject: Bi-directional many-to-many mapping question
PostPosted: Wed Aug 26, 2009 6:55 pm 
Beginner
Beginner

Joined: Sat Dec 03, 2005 11:12 am
Posts: 27
Hi,

I have an many-to-many relationship mapped with annotations
as below:

@Entity
Class User {

// ...some
attributes
private Set<UserGroup> userGroups;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "User",
cascade=CascadeType.ALL)
@org.hibernate.annotations.Cascade(value =
org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
public Set
getUserGroups() { return this.userGroups; }
// getter/setter(s)

}


@Entity
Class Group {
// ...some
attributes
private Set<UserGroup> userGroups;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "Group",
cascade=CascadeType.ALL)
@org.hibernate.annotations.Cascade(value =
org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
public Set
getUserGroups() { return this.userGroups; }
// getter/setter(s)
}


@Entity
Class UserGroup {
// ...some
attributes
private User user;
private Group group;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name =
"userId", nullable = false, insertable = false, updatable =
false)
public User getUser() { return this.user }

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name =
"groupId", nullable = false, insertable = false, updatable =
false)
public Group getGroup() { return this.group }

}

I am updating the UserGroup by doing:

user.getUserGroups().remove(userGroup1);
entityManager.merge(user);

I am getting this exception:

Caused by: org.hibernate.ObjectDeletedException: deleted entity
passed to persist: [UserGroup#<null>]
at
org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:90)
at
org.hibernate.impl.SessionImpl.firePersistOnFlush(SessionImpl.java:644)
at
org.hibernate.impl.SessionImpl.persistOnFlush(SessionImpl.java:636)
at
org.hibernate.engine.CascadingAction$9.cascade(CascadingAction.java:323)
at
org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at
org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at
org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at
org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at
org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at
org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at
org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at
org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at
org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
at
org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
at
org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
at
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at
org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at
org.hibernate.transaction.JTATransaction.commit(JTATransaction.java:135)
at
org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
at
org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:456)


If i remove cascade=CascadeType.ALL from Group entity i
don't get any exception but i want to keep cascade in Group entity.
Is there anything i need to do to get rid of this exception or I should
remove cascade in Group entity?

Thanks,


Top
 Profile  
 
 Post subject: Re: Bi-directional many-to-many mapping question
PostPosted: Thu Aug 27, 2009 7:25 pm 
Beginner
Beginner

Joined: Sat Dec 03, 2005 11:12 am
Posts: 27
Could anyone please let me know


Top
 Profile  
 
 Post subject: Re: Bi-directional many-to-many mapping question
PostPosted: Thu Aug 27, 2009 11:22 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Looks like the entity you are trying to delete has a null id, so Hibernate doesn't know what to delete in the database.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: Bi-directional many-to-many mapping question
PostPosted: Fri Aug 28, 2009 7:42 pm 
Beginner
Beginner

Joined: Sat Dec 03, 2005 11:12 am
Posts: 27
[quote="Cameron McKenzie"]Looks like the entity you are trying to delete has a null id, so Hibernate doesn't know what to delete in the database.[/quote]

I am sure the id is not null. I am doing List<UserGroup> userGroups = group.getUserGroups()

Once i get list of userGroups, depending on certain criteria i am deleting a particular userGroup. So the id is not null.

Thanks


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