-->
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: Detached Entity error while saving entity with oneToMany
PostPosted: Thu Feb 12, 2015 5:37 am 
Newbie

Joined: Fri Nov 02, 2012 2:02 am
Posts: 3
have three entities in hierarchy 1)Teacher 2)Student 3)Subject In Teacher Entity there OneToMany relationship to Student class. Student class has OneToMany relationship with Subject class. I have specific situation in which Teacher has 5 students and students have many subjects assigned to them. I am fetching entity of Teacher and fetching list of Student from teacher entity. Out of 5, I am removing one Student entity from list using remove() function. While saving Teacher entity, getting error as java.lang.IllegalArgumentException: Removing a detached instance of StudentSubject Here StudentSubject is child entity of Student.

Code:
public class Teacher {

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="studentCode", orphanRemoval = true)
@org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.SELECT)
@org.hibernate.annotations.BatchSize(size=30)
private List<Student> students = new ArrayList<Student>();

}

public class Student {

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="studentSubjectCode", orphanRemoval = true)
@org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.SELECT)
@org.hibernate.annotations.BatchSize(size=30)
private List<StudentSubject> studentsubjects = new ArrayList<StudentSubject>();

}


public class StudentSubject {
}


Top
 Profile  
 
 Post subject: Re: Detached Entity error while saving entity with oneToMany
PostPosted: Fri Feb 13, 2015 5:28 am 
Newbie

Joined: Fri Feb 13, 2015 4:13 am
Posts: 1
I suspect that you are running your code outside a transaction so your find and delete operations occur in a separate persistence context and the find actually returns a detached instance (so JPA is right and you ARE deleting a detached object).

Wrap your find / delete sequence inside a transaction.


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.