From 5f025f6dd179aaeffedf307bcfc2492ac05ac121 Mon Sep 17 00:00:00 2001 From: Seremba Patrick <45463621+Seremba@users.noreply.github.com> Date: Sat, 8 Jul 2023 06:56:26 +0300 Subject: [PATCH] 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. --- .../primarykeyjoincolumn/Department.java | 32 ++++++++++++ .../primarykeyjoincolumn/Person.java | 49 +++++++++++++++++ .../PrimaryKeyJoinColumnIntegrationTest.java | 52 +++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/primarykeyjoincolumn/Department.java create mode 100644 persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/primarykeyjoincolumn/Person.java create mode 100644 persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/primarykeyjoincolumn/PrimaryKeyJoinColumnIntegrationTest.java diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/primarykeyjoincolumn/Department.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/primarykeyjoincolumn/Department.java new file mode 100644 index 0000000000..578cc70e38 --- /dev/null +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/primarykeyjoincolumn/Department.java @@ -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; + } + +} diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/primarykeyjoincolumn/Person.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/primarykeyjoincolumn/Person.java new file mode 100644 index 0000000000..0dfa11541b --- /dev/null +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/primarykeyjoincolumn/Person.java @@ -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; + } + +} diff --git a/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/primarykeyjoincolumn/PrimaryKeyJoinColumnIntegrationTest.java b/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/primarykeyjoincolumn/PrimaryKeyJoinColumnIntegrationTest.java new file mode 100644 index 0000000000..0a595d4052 --- /dev/null +++ b/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/primarykeyjoincolumn/PrimaryKeyJoinColumnIntegrationTest.java @@ -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()); + } +}