Note that there are some explanatory texts on larger screens.

plurals
  1. POMany-to-many relationship - hibernate
    primarykey
    data
    text
    <p>I am using hibernate-jpa-2.0-api 1.0.0.Final and hibernate 3.5.5-Final.</p> <p>I have User and Role entities each having many-to-many relationships to each other(bidirectional). I have the following requirements below: </p> <p><strong>1-</strong> If the deleteRole(role) method is called and this role having users then hibernate should throw exception otherwise the role can be deleted successfully.<br> <strong>2-</strong> If the deleteUser(user) method is called user can be deleted successfully, and related records deleted from the relationship table automatically but its role should not be deleted.<br> <strong>3-</strong> Furhermore; if I create new Role and add some users into its set and finally persist the Role, its users should be persisted without an extra persist operations. </p> <p>Let's say <strong>Role</strong> is <strong>Test1</strong> and the <strong>User</strong> is <strong>Test2</strong>. The entities:</p> <p><em><strong>Test1(Role)</em></strong> </p> <pre><code>import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import com.ttnet.model.BaseModel; @Entity public class Test1 extends BaseModel { @ManyToMany(targetEntity = Test2.class, cascade = { CascadeType.ALL }) @JoinTable(name = "test1_test2", joinColumns = { @JoinColumn(name = "id1", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "id2", nullable = false, updatable = false) }) private Set&lt;Test2&gt; test2s = new HashSet&lt;Test2&gt;(0); public Set&lt;Test2&gt; getTest2s() { return test2s; } public void setTest2s(Set&lt;Test2&gt; test2s) { this.test2s = test2s; } } </code></pre> <p><em><strong>Test2(User)</em></strong> </p> <pre><code>import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.ManyToMany; import com.ttnet.model.BaseModel; @Entity public class Test2 extends BaseModel { @ManyToMany(targetEntity=Test1.class, mappedBy = "test2s") private Set&lt;Test1&gt; test1s = new HashSet&lt;Test1&gt;(0); public Set&lt;Test1&gt; getTest1s() { return test1s; } public void setTest1s(Set&lt;Test1&gt; test1s) { this.test1s = test1s; } } </code></pre> <p>TestCode </p> <pre><code> Test1Bo test1Bo = (Test1Bo) context.getBean("test1Bo"); Test2Bo test2Bo = (Test2Bo) context.getBean("test2Bo"); Test1 t1 = new Test1(); Test2 t21 = new Test2(); Test2 t22 = new Test2(); Set&lt;Test2&gt; t2Set = t1.getTest2s(); t2Set.add(t21); t2Set.add(t22); test1Bo.addTest1(t1); </code></pre> <p>This code successfully adds expected entires to all 3 tables. However, at first I tried only the Cascade.PERSIST, and Cascade.MERGE and it gives unsaved object exception. Then I tried ALL, and it completed with a success. Interesting thing is that, I added all javax Cascade operations I mean individualy but no success same exception thrown(unsaved obj...). Now, the problem is that whenever I delete the t1 it deletes t21 and t22 also and this violates the req.1. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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