From f4c5f8b8ae250190defaabe7df9c832a3a80659a Mon Sep 17 00:00:00 2001 From: raghav-jha Date: Fri, 30 Nov 2018 11:40:11 +0530 Subject: [PATCH] JPA One-To-One Association Tutorial Issue: BAEL-2394 --- .../hibernate/onetoone/HibernateUtil.java | 38 +++++++++ .../baeldung/hibernate/onetoone/Strategy.java | 25 ++++++ .../onetoone/foreignkeybased/Address.java | 60 ++++++++++++++ .../onetoone/foreignkeybased/User.java | 51 ++++++++++++ .../onetoone/jointablebased/Employee.java | 53 ++++++++++++ .../onetoone/jointablebased/WorkStation.java | 61 ++++++++++++++ .../onetoone/sharedkeybased/Address.java | 60 ++++++++++++++ .../onetoone/sharedkeybased/User.java | 50 ++++++++++++ .../src/main/resources/one-to-one.cfg.xml | 16 ++++ ...ToOneAnnotationFKBasedIntegrationTest.java | 80 +++++++++++++++++++ ...ToOneAnnotationJTBasedIntegrationTest.java | 80 +++++++++++++++++++ ...oOneAnnotationSPKBasedIntegrationTest.java | 79 ++++++++++++++++++ .../src/test/resources/one-to-one.cfg.xml | 16 ++++ 13 files changed, 669 insertions(+) create mode 100644 persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/HibernateUtil.java create mode 100644 persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/Strategy.java create mode 100644 persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/foreignkeybased/Address.java create mode 100644 persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/foreignkeybased/User.java create mode 100644 persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/jointablebased/Employee.java create mode 100644 persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/jointablebased/WorkStation.java create mode 100644 persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/sharedkeybased/Address.java create mode 100644 persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/sharedkeybased/User.java create mode 100644 persistence-modules/spring-hibernate-5/src/main/resources/one-to-one.cfg.xml create mode 100644 persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/onetoone/HibernateOneToOneAnnotationFKBasedIntegrationTest.java create mode 100644 persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/onetoone/HibernateOneToOneAnnotationJTBasedIntegrationTest.java create mode 100644 persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/onetoone/HibernateOneToOneAnnotationSPKBasedIntegrationTest.java create mode 100644 persistence-modules/spring-hibernate-5/src/test/resources/one-to-one.cfg.xml diff --git a/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/HibernateUtil.java b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/HibernateUtil.java new file mode 100644 index 0000000000..899fcde947 --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/HibernateUtil.java @@ -0,0 +1,38 @@ +package com.baeldung.hibernate.onetoone; + +import org.hibernate.SessionFactory; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.Configuration; +import org.hibernate.service.ServiceRegistry; + +public class HibernateUtil { + private static SessionFactory sessionFactory; + + private static SessionFactory buildSessionFactory(Strategy strategy) { + try { + // Create the SessionFactory from hibernate-annotation.cfg.xml + Configuration configuration = new Configuration(); + + for (Class entityClass : strategy.getEntityClasses()) { + configuration.addAnnotatedClass(entityClass); + } + configuration.configure("one-to-one.cfg.xml"); + + ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()) + .build(); + + SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); + + return sessionFactory; + } catch (Throwable ex) { + ex.printStackTrace(); + throw new ExceptionInInitializerError(ex); + } + } + + public static SessionFactory getSessionFactory(Strategy strategy) { + if (sessionFactory == null) + sessionFactory = buildSessionFactory(strategy); + return sessionFactory; + } +} diff --git a/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/Strategy.java b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/Strategy.java new file mode 100644 index 0000000000..3180566ca5 --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/Strategy.java @@ -0,0 +1,25 @@ +package com.baeldung.hibernate.onetoone; + + +import java.util.Arrays; +import java.util.List; + +public enum Strategy { + //See that the classes belongs to different packages + FOREIGN_KEY(Arrays.asList(com.baeldung.hibernate.onetoone.foreignkeybased.User.class, + com.baeldung.hibernate.onetoone.foreignkeybased.Address.class)), + SHARED_PRIMARY_KEY(Arrays.asList(com.baeldung.hibernate.onetoone.sharedkeybased.User.class, + com.baeldung.hibernate.onetoone.sharedkeybased.Address.class)), + JOIN_TABLE_BASED(Arrays.asList(com.baeldung.hibernate.onetoone.jointablebased.Employee.class, + com.baeldung.hibernate.onetoone.jointablebased.WorkStation.class)); + + private List> entityClasses; + + Strategy(List> entityClasses) { + this.entityClasses = entityClasses; + } + + public List> getEntityClasses() { + return entityClasses; + } +} diff --git a/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/foreignkeybased/Address.java b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/foreignkeybased/Address.java new file mode 100644 index 0000000000..e05eb46030 --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/foreignkeybased/Address.java @@ -0,0 +1,60 @@ +package com.baeldung.hibernate.onetoone.foreignkeybased; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "address") +public class Address { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "id") + private Long id; + + @Column(name = "street") + private String street; + + @Column(name = "city") + private String city; + + @OneToOne(mappedBy = "address") + private User user; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getStreet() { + return street; + } + + public void setStreet(String street) { + this.street = street; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } +} diff --git a/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/foreignkeybased/User.java b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/foreignkeybased/User.java new file mode 100644 index 0000000000..56b108d0d1 --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/foreignkeybased/User.java @@ -0,0 +1,51 @@ +package com.baeldung.hibernate.onetoone.foreignkeybased; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "users") +public class User { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "id") + private Long id; + + @Column(name = "username") + private String userName; + + @OneToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "address_id", referencedColumnName = "id") + private Address address; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } +} diff --git a/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/jointablebased/Employee.java b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/jointablebased/Employee.java new file mode 100644 index 0000000000..a0bc101b9f --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/jointablebased/Employee.java @@ -0,0 +1,53 @@ +package com.baeldung.hibernate.onetoone.jointablebased; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "employee") +public class Employee { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "id") + private Long id; + + @Column(name = "ename") + private String name; + + @OneToOne(cascade = CascadeType.ALL) + @JoinTable(name = "emp_workstation", joinColumns = {@JoinColumn(name = "employee_id", referencedColumnName = "id")}, + inverseJoinColumns = {@JoinColumn(name = "workstation_id", referencedColumnName = "id")}) + private WorkStation workStation; + + 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 WorkStation getWorkStation() { + return workStation; + } + + public void setWorkStation(WorkStation workStation) { + this.workStation = workStation; + } +} diff --git a/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/jointablebased/WorkStation.java b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/jointablebased/WorkStation.java new file mode 100644 index 0000000000..f530611f6e --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/jointablebased/WorkStation.java @@ -0,0 +1,61 @@ +package com.baeldung.hibernate.onetoone.jointablebased; + + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "workstation") +public class WorkStation { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "id") + private Long id; + + @Column(name = "workstation_number") + private Integer workstationNumber; + + @Column(name = "floor") + private String floor; + + @OneToOne(mappedBy = "workStation") + private Employee employee; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Integer getWorkstationNumber() { + return workstationNumber; + } + + public void setWorkstationNumber(Integer workstationNumber) { + this.workstationNumber = workstationNumber; + } + + public String getFloor() { + return floor; + } + + public void setFloor(String floor) { + this.floor = floor; + } + + public Employee getEmployee() { + return employee; + } + + public void setEmployee(Employee employee) { + this.employee = employee; + } +} \ No newline at end of file diff --git a/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/sharedkeybased/Address.java b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/sharedkeybased/Address.java new file mode 100644 index 0000000000..927516f6bb --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/sharedkeybased/Address.java @@ -0,0 +1,60 @@ +package com.baeldung.hibernate.onetoone.sharedkeybased; + + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.MapsId; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "address") +public class Address { + + @Id + @Column(name = "id") + private Long id; + + @Column(name = "street") + private String street; + + @Column(name = "city") + private String city; + + @OneToOne + @MapsId + private User user; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getStreet() { + return street; + } + + public void setStreet(String street) { + this.street = street; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } +} diff --git a/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/sharedkeybased/User.java b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/sharedkeybased/User.java new file mode 100644 index 0000000000..fa00db1271 --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/main/java/com/baeldung/hibernate/onetoone/sharedkeybased/User.java @@ -0,0 +1,50 @@ +package com.baeldung.hibernate.onetoone.sharedkeybased; + + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "users") +public class User { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "id") + private Long id; + + @Column(name = "username") + private String userName; + + @OneToOne(mappedBy = "user", cascade = CascadeType.ALL) + private Address address; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } +} diff --git a/persistence-modules/spring-hibernate-5/src/main/resources/one-to-one.cfg.xml b/persistence-modules/spring-hibernate-5/src/main/resources/one-to-one.cfg.xml new file mode 100644 index 0000000000..7522036acb --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/main/resources/one-to-one.cfg.xml @@ -0,0 +1,16 @@ + + + + + org.h2.Driver + + jdbc:h2:mem:spring_hibernate_one_to_one + sa + org.hibernate.dialect.H2Dialect + thread + true + create-drop + + \ No newline at end of file diff --git a/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/onetoone/HibernateOneToOneAnnotationFKBasedIntegrationTest.java b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/onetoone/HibernateOneToOneAnnotationFKBasedIntegrationTest.java new file mode 100644 index 0000000000..475c93f6ff --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/onetoone/HibernateOneToOneAnnotationFKBasedIntegrationTest.java @@ -0,0 +1,80 @@ +package com.baeldung.hibernate.onetoone; + +import com.baeldung.hibernate.onetoone.foreignkeybased.Address; +import com.baeldung.hibernate.onetoone.foreignkeybased.User; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class HibernateOneToOneAnnotationFKBasedIntegrationTest { + + private static SessionFactory sessionFactory; + + private Session session; + + @BeforeClass + public static void beforeTests() { + sessionFactory = HibernateUtil.getSessionFactory(Strategy.FOREIGN_KEY); + } + + @Before + public void setUp() { + session = sessionFactory.openSession(); + session.beginTransaction(); + } + + @Test + public void givenData_whenInsert_thenCreates1to1relationship() { + User user = new User(); + user.setUserName("alice@baeldung.com"); + + Address address = new Address(); + address.setStreet("FK Street"); + address.setCity("FK City"); + + address.setUser(user); + user.setAddress(address); + + //Address entry will automatically be created by hibernate, since cascade type is specified as ALL + session.persist(user); + session.getTransaction().commit(); + + assert1to1InsertedData(); + } + + private void assert1to1InsertedData() { + @SuppressWarnings("unchecked") + List userList = session.createQuery("FROM User").list(); + + assertNotNull(userList); + assertEquals(1, userList.size()); + + User user = userList.get(0); + assertEquals("alice@baeldung.com", user.getUserName()); + + Address address = user.getAddress(); + assertNotNull(address); + assertEquals("FK Street", address.getStreet()); + assertEquals("FK City", address.getCity()); + + } + + @After + public void tearDown() { + session.close(); + } + + @AfterClass + public static void afterTests() { + sessionFactory.close(); + } +} diff --git a/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/onetoone/HibernateOneToOneAnnotationJTBasedIntegrationTest.java b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/onetoone/HibernateOneToOneAnnotationJTBasedIntegrationTest.java new file mode 100644 index 0000000000..df4cd4d137 --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/onetoone/HibernateOneToOneAnnotationJTBasedIntegrationTest.java @@ -0,0 +1,80 @@ +package com.baeldung.hibernate.onetoone; + +import com.baeldung.hibernate.onetoone.jointablebased.Employee; +import com.baeldung.hibernate.onetoone.jointablebased.WorkStation; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class HibernateOneToOneAnnotationJTBasedIntegrationTest { + + private static SessionFactory sessionFactory; + + private Session session; + + @BeforeClass + public static void beforeTests() { + sessionFactory = HibernateUtil.getSessionFactory(Strategy.JOIN_TABLE_BASED); + } + + @Before + public void setUp() { + session = sessionFactory.openSession(); + session.beginTransaction(); + } + + @Test + public void givenData_whenInsert_thenCreates1to1relationship() { + Employee employee = new Employee(); + employee.setName("bob@baeldung.com"); + + WorkStation workStation = new WorkStation(); + workStation.setWorkstationNumber(626); + workStation.setFloor("Sixth Floor"); + + + employee.setWorkStation(workStation); + workStation.setEmployee(employee); + + session.persist(employee); + session.getTransaction().commit(); + + assert1to1InsertedData(); + } + + private void assert1to1InsertedData() { + @SuppressWarnings("unchecked") + List employeeList = session.createQuery("FROM Employee").list(); + assertNotNull(employeeList); + assertEquals(1, employeeList.size()); + + Employee employee = employeeList.get(0); + assertEquals("bob@baeldung.com", employee.getName()); + + WorkStation workStation = employee.getWorkStation(); + + assertNotNull(workStation); + assertEquals((long) 626, (long) workStation.getWorkstationNumber()); + assertEquals("Sixth Floor", workStation.getFloor()); + + } + + @After + public void tearDown() { + session.close(); + } + + @AfterClass + public static void afterTests() { + sessionFactory.close(); + } +} diff --git a/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/onetoone/HibernateOneToOneAnnotationSPKBasedIntegrationTest.java b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/onetoone/HibernateOneToOneAnnotationSPKBasedIntegrationTest.java new file mode 100644 index 0000000000..7931a8e3fe --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/onetoone/HibernateOneToOneAnnotationSPKBasedIntegrationTest.java @@ -0,0 +1,79 @@ +package com.baeldung.hibernate.onetoone; + +import com.baeldung.hibernate.onetoone.sharedkeybased.Address; +import com.baeldung.hibernate.onetoone.sharedkeybased.User; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class HibernateOneToOneAnnotationSPKBasedIntegrationTest { + + private static SessionFactory sessionFactory; + + private Session session; + + @BeforeClass + public static void beforeTests() { + sessionFactory = HibernateUtil.getSessionFactory(Strategy.SHARED_PRIMARY_KEY); + } + + @Before + public void setUp() { + session = sessionFactory.openSession(); + session.beginTransaction(); + } + + @Test + public void givenData_whenInsert_thenCreates1to1relationship() { + User user = new User(); + user.setUserName("alice@baeldung.com"); + + Address address = new Address(); + address.setStreet("SPK Street"); + address.setCity("SPK City"); + + address.setUser(user); + user.setAddress(address); + + //Address entry will automatically be created by hibernate, since cascade type is specified as ALL + session.persist(user); + session.getTransaction().commit(); + + assert1to1InsertedData(); + } + + + private void assert1to1InsertedData() { + @SuppressWarnings("unchecked") + List userList = session.createQuery("FROM User").list(); + assertNotNull(userList); + assertEquals(1, userList.size()); + + User user = userList.get(0); + assertEquals("alice@baeldung.com", user.getUserName()); + + Address address = user.getAddress(); + assertNotNull(address); + assertEquals("SPK Street", address.getStreet()); + assertEquals("SPK City", address.getCity()); + } + + @After + public void tearDown() { + session.close(); + } + + @AfterClass + public static void afterTests() { + sessionFactory.close(); + } +} diff --git a/persistence-modules/spring-hibernate-5/src/test/resources/one-to-one.cfg.xml b/persistence-modules/spring-hibernate-5/src/test/resources/one-to-one.cfg.xml new file mode 100644 index 0000000000..60417e312d --- /dev/null +++ b/persistence-modules/spring-hibernate-5/src/test/resources/one-to-one.cfg.xml @@ -0,0 +1,16 @@ + + + + + org.h2.Driver + + jdbc:h2:mem:spring_hibernate_one_to_one + sa + org.hibernate.dialect.H2Dialect + thread + true + create-drop + +