Close

JPA - @JoinColumn Examples

JPA JAVA EE 


@JoinColumn is used to specify a column for joining an entity association or element collection. This annotation indicates that the enclosing entity is the owner of the relationship and the corresponding table has a foreign key column which references to the table of the non-owning side.


Using @JoinColumn with @OneToOne

package com.logicbig.example;

import javax.persistence.*;

@Entity
public class EntityA {
@Id
@GeneratedValue
private int myIdA;
@OneToOne
@JoinColumn(name = "MY_JOIN_COLUMN")
private EntityB entityB;

public EntityB getEntityB() {
return entityB;
}

public void setEntityB(EntityB entityB) {
this.entityB = entityB;
}
}
package com.logicbig.example;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class EntityB {
@Id
@GeneratedValue
private int myIdB;
private String stringB;

public String getStringB() {
return stringB;
}

public void setStringB(String stringB) {
this.stringB = stringB;
}
}

src/main/resources/META-INF/persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

<persistence-unit name="test1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.logicbig.example.EntityA</class>
<class>com.logicbig.example.EntityB</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/>
</properties>
</persistence-unit>
</persistence>

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.logicbig.example</groupId>
<artifactId>jpa-one-to-one-with-join-column-example</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.8.Final</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.logicbig.example;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.List;

public class ExampleMain {

public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("test1");
try {
EntityManager em = emf.createEntityManager();
nativeQuery(em, "SHOW TABLES");
nativeQuery(em, "SHOW COLUMNS from EntityA");
nativeQuery(em, "SHOW COLUMNS from EntityB");
emf.close();
} finally {
emf.close();
}
}

public static void nativeQuery(EntityManager em, String s) {
System.out.println("--------\n" + s);
Query query = em.createNativeQuery(s);
List list = query.getResultList();
for (Object o : list) {
System.out.println(Arrays.toString((Object[]) o));
}
}
}

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]
Original PostDownload Project Browser 




@Entity
public class EntityA {
@Id
@GeneratedValue
private int myIdA;
private String strA;
@OneToMany
@JoinColumn(name = "MY_FK_COL")
private List<EntityB> entityBList;
.............
}

@Entity
public class EntityB {
@Id
@GeneratedValue
private int myIdB;
private String strB;
.............
}
Original Post




import javax.persistence.*;
import java.util.List;

@Entity
public class EntityA {
@Id
@GeneratedValue
private int myIdA;
@OneToMany
@JoinTable(name = "MY_JOIN_TABLE",
joinColumns = {@JoinColumn(name = "MY_ENTITY_A_FK")},
inverseJoinColumns = {@JoinColumn(name = "MY_ENTITY_B_FK")}
)
private List<EntityB> entityBList;

public List<EntityB> getEntityBList() {
return entityBList;
}

public void setEntityBList(List<EntityB> entityBList) {
this.entityBList = entityBList;
}

@Override
public String toString() {
return "EntityA{" +
"myIdA=" + myIdA +
", entityBList=" + entityBList +
'}';
}
}
Original Post




Foreign key mapping strategy:

@Entity
public class Employee {
@Id
@GeneratedValue
private long id;
private String name;
@OneToMany
@JoinColumn(name = "EMPLOYEE_FK")
@MapKeyColumn(name = "TASK_DATE", nullable = true)
private Map<Date, Task> tasks;
.............
}

@Entity
public class Task {
@Id
@GeneratedValue
private long id;
private String name;
private String description;
.............
}
Original Post




See Also