-->
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: Help on Joint Tables (Many-to-Many
PostPosted: Wed May 21, 2014 7:53 am 
Newbie

Joined: Wed May 21, 2014 7:50 am
Posts: 1
I hope it is okay to ask for help here :)

would like to ask for help on my hibernate project

I have 3 tables: student, class and student_class
student_class is a joint table for student and class

the following is the setup:

Student.java
@ManyToMany(
targetEntity=Class.class,
cascade={CascadeType.PERSIST, CascadeType.MERGE},
fetch=FetchType.EAGER
)
@JoinTable(
name="student_class",
joinColumns=@JoinColumn(name="student_id"),
inverseJoinColumns=@JoinColumn(name="class_id")
)
@Fetch(value = FetchMode.SUBSELECT)
public Set<Class> classes;

_____
Class.java
@ManyToMany(
cascade = {CascadeType.PERSIST, CascadeType.MERGE},
targetEntity = Student.class
)
@JoinTable(
name="student_class",
joinColumns=@JoinColumn(name="class_id"),
inverseJoinColumns=@JoinColumn(name="student_id")
)
public Set<Student> students;


What I want to do is when the student changes one of its classes the joint table (student_class) should be updated. But this is not happening in my current implementation.

for example
Student A has Math and Science classes. This creates 2 records in the joint table.
Now Student A decides to change Science class to French class. In my assumption, this should delete the A-and-Science record and inserts the A-and-French record in the joint table. But the A-and-Science record is not deleted that results to 3 classes for Student A.

How do I implement the correct mapping in order for A-and-Science to be deleted?

Thanks for the help.
rein


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