Note that there are some explanatory texts on larger screens.

plurals
  1. POTwo almost identical OneToOne in Entity, custom join annotation
    text
    copied!<p>I've got Parent - Child relationship OneToOne, but two of them. Annotations are not good but it produces good DB schema, yet code is not working. If I try to save Parent instance, Hibernate at first tries to save child1 and child2 - but it breaks FK defined in Child -> because owner doesn't exist yet in DB...So I need to save Parent and then child1, and child2.</p> <p>If I could do that it doesn't help, because when I try to load Parent, Hibernate will not know which record in Child table belongs to child1 or child2...So in this case I would need to specify one condition in join for child1 something like "where type = 1" and for child2 "where type = 2"...</p> <p>Just to clarify: in <code>Child</code> table there will be zero or one child for one <code>Parent</code> with <code>ChildType.A</code> (always child1) and zero or one child with <code>ChildType.B</code> (always child2).</p> <p>I need to save xml which looks like this:</p> <pre><code>&lt;parent id="" oid=""&gt; &lt;child1 (and other attributes)&gt; &lt;child2 (and other attributes)&gt; &lt;parent&gt; </code></pre> <p>Both <code>child1</code> and <code>child2</code> elements are the same type therefore are type of <code>Child</code> in java classes. Only difference is element name (in java I differentiate them with <code>ChildType</code>). Only identification for children are <code>id</code> and <code>oid</code> attributes from parent. They points to another <code>Parent</code> hence <code>target</code> in <code>Child</code>. </p> <p>I need to change annotations somehow to get this working...Do you guys have some ideas, because I'm really stuck???</p> <p>Parent.java</p> <pre><code>public class Parent { private String oid private Long id; private Child child1; private Child child2; @Id @GeneratedValue(generator = "IdGenerator") @GenericGenerator(name = "IdGenerator", strategy = "com.example.IdGenerator") @Column(name = "id") public Long getId() { return id; } @Id @GeneratedValue(generator = "OidGenerator") @GenericGenerator(name = "OidGenerator", strategy = "com.example.OidGenerator") @Column(name = "oid", nullable = false, updatable = false, length = 36) public String getOid() { return oid; } @OneToOne(optional = true, fetch = FetchType.EAGER) @Cascade({org.hibernate.annotations.CascadeType.ALL}) public Child getChild1() { return child1; } @OneToOne(optional = true, fetch = FetchType.EAGER) @Cascade({org.hibernate.annotations.CascadeType.ALL}) public Child getChild2() { return child2; } } </code></pre> <p>Child.java</p> <pre><code>public class Child { private Parent owner; private String ownerOid; private Long ownerId; private ChildType type; private Parent target; @MapsId("owner") @ManyToOne(fetch = FetchType.LAZY) @PrimaryKeyJoinColumns({ @PrimaryKeyJoinColumn(name = "owner_oid", referencedColumnName = "oid"), @PrimaryKeyJoinColumn(name = "owner_id", referencedColumnName = "id") }) public Parent getOwner() { return owner; } @MapsId("target") @ManyToOne(fetch = FetchType.LAZY) @PrimaryKeyJoinColumns({ @PrimaryKeyJoinColumn(name = "target_oid", referencedColumnName = "oid"), @PrimaryKeyJoinColumn(name = "target_id", referencedColumnName = "id") }) public Parent getTarget() { return target; } @Id @Column(name = "owner_id") public Long getOwnerId() { if (ownerId == null &amp;&amp; owner != null) { ownerId = owner.getId(); } return ownerId; } @Id @Column(name = "owner_oid", length = 36) public String getOwnerOid() { if (ownerOid == null &amp;&amp; owner != null) { ownerOid = owner.getOid(); } return ownerOid; } @Id @Column(name = "target_id") public Long getTargetId() { if (targetId == null &amp;&amp; target != null) { targetId = target.getId(); } return targetId; } @Id @Column(name = "target_oid", length = 36) public String getTargetOid() { if (targetOid == null &amp;&amp; target != null) { targetOid = target.getOid(); } if (targetOid == null) { targetOid = ""; } return targetOid; } @Id @Enumerated(EnumType.ORDINAL) public ChildType getType() { if (type == null) { return ChildType.A; } return type; } } </code></pre> <p>ChildType.java</p> <pre><code>public enum ChildType { A, B; } </code></pre> <p>I also tried to use <code>mappedBy</code> approach <a href="https://stackoverflow.com/questions/9676649/onetoone-custom-join-query">mappedBy approach</a> but there are still problems with loading - I can't tell hibernate which child record belogs to which child class member variable.</p>
 

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