Difference Between @JoinColumn and @PrimaryKeyJoinColumn in JPA (#14171)

* primarykeyjoincolum annotaion in jpa

* indent Departmnet Person and primaryKeyJoinColumn

* indent Department Perspn and PrimaryKeyJoinColumnIntegrationTest classes

* format properly

* format to 2-space continuation identation

* Revert "format to 2-space continuation identation"

This reverts commit 49630409ee296c07cfd494e46f9dbba75c9a49b0.

* add 2-space indent on line continuations.
This commit is contained in:
Seremba Patrick 2023-07-08 06:56:26 +03:00 committed by GitHub
parent 2546a2e218
commit 5f025f6dd1
3 changed files with 133 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package com.baeldung.hibernate.primarykeyjoincolumn;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "department")
public class Department {
@Id
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.hibernate.primarykeyjoincolumn;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import jakarta.persistence.PrimaryKeyJoinColumn;
import jakarta.persistence.Table;
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToOne
@PrimaryKeyJoinColumn
private Department department;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.hibernate.primarykeyjoincolumn;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
public class PrimaryKeyJoinColumnIntegrationTest {
private static EntityManagerFactory emf;
private EntityManager em;
@Before
public void setup() {
emf = Persistence.createEntityManagerFactory("com.baeldung.movie_catalog");
em = emf.createEntityManager();
em.getTransaction()
.begin();
Department department = new Department();
department.setName("IT");
em.persist(department);
Person person = new Person();
person.setName("John Doe");
person.setDepartment(department);
em.persist(person);
em.getTransaction()
.commit();
}
@After
public void teardown() {
em.close();
emf.close();
}
@Test
public void givenPersonEntity_getDepartment_shouldExist() {
Person person = em.find(Person.class, 1L);
assertNotNull(person);
assertEquals("John Doe", person.getName());
assertNotNull(person.getDepartment());
assertEquals("IT", person.getDepartment()
.getName());
}
}