-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate/jpa: do I have to "re-attach"
PostPosted: Mon Dec 09, 2013 8:04 pm 
Beginner
Beginner

Joined: Tue May 11, 2004 12:20 am
Posts: 33
Hi,
I'm using JPA with Hibernate implementation (but a solution on classic Hibernate would also be appreciated).
It's Many-to-Many unidirectional relation, say between "Course" and "Student" (code below).
My problem is saving a Student when his Courses list is detached. I'm sure each such course is already saved in the database and has proper ID... so I wanted to just update the JoinTable without wasting performance on checking the existence of each course. I could do it easily with raw JDBC. But if I use Hibernate, it forces me to check and "re-attach" the courses (re-attaching is either through CASCADE, or re-selecting the courses).
Code:
@Entity
public class Student {
   @Id private long id;
   @ManyToMany @JoinTable(...)
   private Set courses;
   // ... more properties, getters, setters...
}
@Entity
public class Course {
   @Id private long id;
   // ... more properties, getters, setters...
}

Now: 
// .. start transaction 1 
Set<Course> detachedCourses= entityManager.find(...); 
// ... end transaction 1, courses are now detached 
 
// start transaction 2 
Student stud=new Student("john doe"); 
s.setCourses(detachedCourses); 
entityManager.persist(stud); 
// end transaction 2 - this would fail unless I "re-attach" the courses


Is there a way to tell Hibernate to "just trust me &update the JoinTable (linking this studentID to those courseIDs), without reattaching/validating each course?

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.