This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The link table of ManyToMany annotation is not updated in Hibernate

 
Anibal Gimenez Canicoba
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you set both sides of the relationship. Meaning both Collections are populated?

Mark
 
Anibal Gimenez Canicoba
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the @ManyToOne annotation is setted in both beans, but Student is the owner.
Does it your question?
 
Anibal Gimenez Canicoba
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic