Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA 2: Determine an entitys id by looking up JoinColumn - Shared primary key in one-to-one relation
    primarykey
    data
    text
    <p>I have two entity classes Client and Preferences in a bidirectional one-to-one relation. The entities should share the same primary key, which is generated programmatically by java.util.UUID.</p> <p>Here is the minimalistic scenario:</p> <pre class="lang-java prettyprint-override"><code>@Entity public class Client implements Serializable { @Id private String id; @OneToOne(mappedBy = "client", casacade = CascadeType.PERSIST) private Preferences preferences; // ... } @Entity public class Preferences implements Serializable { @Id @OneToOne @JoinColumn private Client client; // ... } </code></pre> <p>Everything works fine but I am disaffected because I can't find a clean way to provide a simple id field/property in Preferences.</p> <p>In the scenario above I am forced to determine it by calling <code>client.getId();</code> all the time which looks quite messed up. So I was trying to figure out some alternatives. Here is what I found out so far:</p> <p>Alternative A: Persist a basic value called id:</p> <pre class="lang-java prettyprint-override"><code>@Entity public class Preferences implements Serializable { @Id @OneToOne @JoinColumn private Client client; private String id; public setClient (Client client) { this.client = client; id = client.getId(); } // ... } </code></pre> <p>The main problem with that solution is that it would store the same value twice...why then share a pk at all?!</p> <p>Alternative B: Transient id:</p> <pre class="lang-java prettyprint-override"><code>@Entity public class Preferences implements Serializable { @Id @OneToOne @JoinColumn private Client client; @Transient private String id; @PostLoad private void postLoad () { id = client.getId(); } public setClient (Client client) { this.client = client; id = client.getId(); } // ... } </code></pre> <p>Okay, here we don't save redundant data but Client must be fetched eagerly erverytime. That's the default behaviour in one-to-one relations but what if it would be better someday to have that client being fetched lazily? I don't like my options being restricted for no reason. </p> <p>Is there a way to read the id given by Client directly out of that JoinColumn or should I use solution A/B?</p> <p>I'm trying to stick to the spec, provider independent hints would be nice.</p> <p>Thank you!</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. 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