Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I see two problems with using one-to-one references:</p> <ol> <li>Parent has zero or one child1, and zero or one child2. My understanding is that you use one to one when it's ALWAYS one.</li> <li>Your IDs are getting really complicated. The idea with one-to-one is that the ID's are the same between the two entities, and you've got Parent with a two-part primary key, and Child with a different four part primary key.</li> </ol> <p>The other addendum to 2. is that if you're using 1-1, then the ID of parent and child should be the same. But if the parent has two children, they can't both have the same ID! So there's a real data modeling problem. Also, the fact that child type is part of the key is also a bad smell, cause I expect child type is part of your business logic, </p> <p>I think what you want is what Hibernate calls a 'unidirectional one-to-one association on a foreign key'. The <a href="http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#assoc-unidirectional-121" rel="nofollow">Hibernate ORM manual</a> shows how to do this in XML, here's an <a href="http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-one-to-one-using-annotations-1.html" rel="nofollow">example</a> with Annotations, although without the unique property set.</p> <p>I'm going to assume there's no outside business reason why your primary keys have to be the way that they are, and suggest that you</p> <ol> <li>Change Parent and Child to each have to have a single field primary key, using @Id and @GeneratedValue(strategy=GenerationType.AUTO).</li> <li>Change Parent to have the references to child1 and child2 be @ManyToOne(unique=true)</li> <li>If you want, change Child as detailed in the Hibernate manual so that it has a reference to the Parent.</li> </ol> <p>You will lose the 'feature' that parents and children have the same ID, but gain massive simplicity in your object ID scheme, making database operations faster and the code simpler to read.</p> <p>If there is an outside reason that you haven't specified as to why Parent and/or Child need multi-part primary keys, this answer will need some modification.</p> <p>I also second the point that @barsju made about using inheritance, or even entirely independent classes, to do the two Child types. If you use ID's the way I lay out here, you can make Parent have references to the concrete subtypes, and the query will work fine.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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