• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The link table of ManyToMany annotation is not updated in Hibernate

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just saved the hair on my head!!!

That was exactly my problem, thanks for posting your solution!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic