Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To map one-to-one association using shared primary keys use <code>@PrimaryKeyJoinColumn</code> and <code>@MapsId</code> annotation.</p> <p>Relevant sections of the Hibernate Reference Documentation:</p> <p><a href="http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-association" rel="noreferrer">PrimaryKeyJoinColumn</a> </p> <blockquote> <p>The PrimaryKeyJoinColumn annotation does say that the primary key of the entity is used as the foreign key value to the associated entity.</p> </blockquote> <p><a href="http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-identifier" rel="noreferrer">MapsId</a> </p> <blockquote> <p>The MapsId annotation ask Hibernate to copy the identifier from another associated entity. In the Hibernate jargon, it is known as a foreign generator but the JPA mapping reads better and is encouraged</p> </blockquote> <p><strong><em>Person.java</em></strong></p> <pre><code>@Entity public class Person { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "person_id") private Long id; @OneToOne(cascade = CascadeType.ALL) @PrimaryKeyJoinColumn private VitalStats vitalStats; } </code></pre> <p><strong><em>VitalStats.java</em></strong></p> <pre><code>@Entity public class VitalStats { @Id @Column(name="vitalstats_id") Long id; @MapsId @OneToOne(mappedBy = "vitalStats") @JoinColumn(name = "vitalstats_id") //same name as id @Column private Person person; private String stats; } </code></pre> <p><strong>Person Database Table</strong></p> <pre><code>CREATE TABLE person ( person_id bigint(20) NOT NULL auto_increment, name varchar(255) default NULL, PRIMARY KEY (`person_id`) ) </code></pre> <p><strong>VitalStats Database Table</strong></p> <pre><code>CREATE TABLE vitalstats ( vitalstats_id bigint(20) NOT NULL, stats varchar(255) default NULL, PRIMARY KEY (`vitalstats_id`) ) </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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