Note that there are some explanatory texts on larger screens.

plurals
  1. PONot able to save ManyToMany self-reference relationship in hibernate
    primarykey
    data
    text
    <p>I have a ManyToMany relationship between <strong>person</strong> that I'm trying to describe with Hibernate annotations. I have also created a test for this but the problem is that the relationship isn't saved. Please help me find where I did wrong!</p> <p><strong>Entity:</strong> </p> <pre><code>@Entity(name = "person") @Table(appliesTo = "person", indexes = { @org.hibernate.annotations.Index(name = "ix_uuid", columnNames = {"uuid"}), @org.hibernate.annotations.Index(name = "ix_facebookId", columnNames = {"facebookId"}) }) public class Person implements Serializable { @Id @GeneratedValue(strategy = AUTO) private Long id; private String uuid; private String firstName; private String lastName; private String facebookId; private String email; @ManyToMany(cascade = CascadeType.ALL) @JoinTable( name = "person_friend", joinColumns = @JoinColumn(name = "person_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "friend_id", referencedColumnName = "id") ) private Set&lt;Person&gt; persons = new HashSet&lt;Person&gt;(); @ManyToMany(mappedBy = "persons", cascade = CascadeType.ALL) private Set&lt;Person&gt; friends = new HashSet&lt;Person&gt;(); public Person(String uuid, String firstName, String lastName, String facebookId, String email) { this.uuid = uuid; this.firstName = firstName; this.lastName = lastName; this.facebookId = facebookId; this.email = email; } public Person() { // Hibernate } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getFacebookId() { return facebookId; } public void setFacebookId(String facebookId) { this.facebookId = facebookId; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public void addFriend(Person person){ if(!getFriends().contains(person)){ getFriends().add(person); person.getPersons().add(this); } } public void becomeFriendOf(Person person) { if(!getPersons().contains(person)){ getPersons().add(person); person.getFriends().add(this); } } public Set&lt;Person&gt; getFriends() { return friends; } public void setFriends(Set&lt;Person&gt; friends) { this.friends = friends; } public Set&lt;Person&gt; getPersons() { return persons; } public void setPersons(Set&lt;Person&gt; persons) { this.persons = persons; } @Override public String toString() { return "Person{" + "id=" + id + ", uuid='" + uuid + '\'' + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", facebookId='" + facebookId + '\'' + ", email='" + email + '\'' + '}'; } } </code></pre> <p><strong>PersonDao:</strong></p> <pre><code>@Repository("personDao") public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao { @Autowired public PersonDaoImpl(SessionFactory sessionFactory) { setSessionFactory(sessionFactory); } @Override public void save(Person person) { getSession().save(person); } @Override public Person getPerson(Long id) { return (Person) getSession().get(Person.class, id); } @Override public void saveFriendship(Friendship friendship) { getSession().save(friendship); } } </code></pre> <p><strong>Test:</strong></p> <pre><code>@Test(groups = {"integration"}) @ContextConfiguration(locations = {"classpath:applicationContext-test.xml" }) public class PersonDaoImplTest extends AbstractTestNGSpringContextTests { @Autowired private PersonDao target; @Test public void loadDatabaseWithSomeInitialValues(){ System.out.println(applicationContext.toString()); Person person = new Person("12345abcde","test","test2","test.test", "test.test@gmail.com"); Person person2 = new Person("4567abcde","fest","fest","fest.fest", "fest.fest@gmail.com"); Person person3 = new Person("89105abcde","best","best","best.best", "best.best@gmail.com"); person.addFriend(person2); person.addFriend(person3); person2.becomeFriendOf(person); person3.becomeFriendOf(person); target.save(person); } } } </code></pre> <p>As stated the persons are saved in the <strong>person</strong> table but not the relationship in the <strong>person_friend</strong> table. Why?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload