my dog learned polymorphism*
The moose likes Object Relational Mapping and the fly likes The link table of ManyToMany annotation is not updated in Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "The link table of ManyToMany annotation is not updated in Hibernate" Watch "The link table of ManyToMany annotation is not updated in Hibernate" New topic
Author

The link table of ManyToMany annotation is not updated in Hibernate

Anibal Gimenez Canicoba
Greenhorn

Joined: Oct 31, 2004
Posts: 19
As the subject says, I've a got problem when I try to save an entity with @ManyToMany annotation. The main problem is that the save operation is not saving the link Table(but makes an insert in both entities<cascade=CascadeType.All> .
To figure out the the problem, there are the beans with them annotations.


Assignment.java:

package test.bean.school;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

@Entity
public class Assignment {

private int id;
private String description;
private Set<Student> students = new HashSet<Student>();

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

@Id
@GeneratedValue
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@ManyToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name="Assignment_Student",
joinColumns={@JoinColumn(name="students_id")},
inverseJoinColumns={@JoinColumn(name="assignments_id")})
public Set<Student> getStudents() {
return students;
}

public void setStudents(Set<Student> students) {
this.students = students;
}

public int hashCode(){
return this.description.hashCode();
}

public boolean equals(Object object){
if (object instanceof Assignment){
return this.description.equals(((Assignment)object).getDescription());
}
return false;
}
}

Student.java :

package test.bean.school;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

@Entity
public class Student {
private int id;
private String name;
private Set<Assignment> assignments = new HashSet<Assignment>();

@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER , mappedBy="students")
@JoinTable( name="Assignment_Student",
joinColumns={@JoinColumn(name="assignments_id")},
inverseJoinColumns={@JoinColumn(name="students_id")} )
public Set<Assignment> getAssignments() {
return assignments;
}

public void setAssignments(Set<Assignment> assignments) {
this.assignments = assignments;
}

@Id
@GeneratedValue
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int hashCode(){
return this.name.hashCode();
}

public boolean equals(Object object){
if (object instanceof Student){
return this.name.equals(((Student)object).getName());
}
return false;
}
}


... compiles fine and without exceptions at runtime, but, when I try to get the beans from the database, the set attribute in each bean is empty.

Please, help me!!!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Have you set both sides of the relationship. Meaning both Collections are populated?

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Anibal Gimenez Canicoba
Greenhorn

Joined: Oct 31, 2004
Posts: 19
Yes the @ManyToOne annotation is setted in both beans, but Student is the owner.
Does it your question?
Anibal Gimenez Canicoba
Greenhorn

Joined: Oct 31, 2004
Posts: 19
Sorry, but now, I figured out what you trying to ask me, again, the answer is yes. After get the student or assignment bean the set attribute that has a reference to the other bean should be filled and not return empty.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Are you allowing the tables to be generated by your mapping?

I suggest this little test, change your mapping to the following. Also change the Set to a List/ArrayList instead, just as a test.





I highly recommend allowing the default lazy fetching, and only eager fetch in a query, so it is use case driven. There will be times when you want just students or just assignments, and if you set it to eager fetching you will fetch more data than you need and take a performance hit.

Mark
Anibal Gimenez Canicoba
Greenhorn

Joined: Oct 31, 2004
Posts: 19
Thanks for your help!!!

The problem was that hibernate makes the inserts to links table at commit time!!! I was closing the session without commit!!!

sounds stupid, I've known, but maybe I could help to anyone with that information.
A Aasdf
Greenhorn

Joined: Apr 12, 2014
Posts: 1
You just saved the hair on my head!!!

That was exactly my problem, thanks for posting your solution!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The link table of ManyToMany annotation is not updated in Hibernate
 
Similar Threads
I need help with Hibernate
EJB3.0 example
How to solve this exception? and what the mistake I have done in my code??
AbstractWizardFormController: teasing command object
Apache Derby Sql Statement's