Close

JPA - @JoinColumn with @OneToOne Example

This example shows how to use @JoinColumn to specify a foreign-key column name for the table of the source entity of a one-to-one relationship.

Example Project

Dependencies and Technologies Used:

  • h2 1.4.193: H2 Database Engine.
  • hibernate-core 5.2.8.Final: The core O/RM functionality as provided by Hibernate.
    Implements javax.persistence:javax.persistence-api version 2.1
  • JDK 1.8
  • Maven 3.3.9

jpa-one-to-one-with-join-column-example Select All Download
  • jpa-one-to-one-with-join-column-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • EntityA.java
          • resources
            • META-INF

    Here's the output of MainExample.java

    Output

    --------
    SHOW TABLES
    [ENTITYA, PUBLIC]
    [ENTITYB, PUBLIC]
    --------
    SHOW COLUMNS from EntityA
    [MYIDA, INTEGER(10), NO, PRI, NULL]
    [MY_JOIN_COLUMN, INTEGER(10), YES, , NULL]
    --------
    SHOW COLUMNS from EntityB
    [MYIDB, INTEGER(10), NO, PRI, NULL]
    [STRINGB, VARCHAR(255), YES, , NULL]

    See also

    JPA - OneToOne relationship