From 4798036089d1db858d60a045e8fa4ebd84070ed4 Mon Sep 17 00:00:00 2001 From: Priyesh Mashelkar Date: Tue, 4 Sep 2018 00:01:25 +0100 Subject: [PATCH] Added entities and basic select --- .../hibernate/entities/Department.java | 16 +++++++ .../baeldung/hibernate/entities/Employee.java | 47 +++++++++++++++++++ .../hibernate/CustomClassIntegrationTest.java | 36 ++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/entities/Department.java create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/entities/Employee.java create mode 100644 hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/entities/Department.java b/hibernate5/src/main/java/com/baeldung/hibernate/entities/Department.java new file mode 100644 index 0000000000..12a9bda5f9 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/entities/Department.java @@ -0,0 +1,16 @@ +package com.baeldung.hibernate.entities; + +import java.util.List; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity +public class Department { + @Id + long id; + String name; + @OneToMany(mappedBy="department") + List employees; +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/entities/Employee.java b/hibernate5/src/main/java/com/baeldung/hibernate/entities/Employee.java new file mode 100644 index 0000000000..8b5c554c44 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/entities/Employee.java @@ -0,0 +1,47 @@ +package com.baeldung.hibernate.entities; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToOne; + +@Entity +public class Employee { + @Id + long id; + String employeeNumber; + String name; + String designation; + @ManyToOne + Department department; + + public long getId() { + return id; + } + public void setId(long id) { + this.id = id; + } + public String getEmployeeNumber() { + return employeeNumber; + } + public void setEmployeeNumber(String employeeNumber) { + this.employeeNumber = employeeNumber; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getDesignation() { + return designation; + } + public void setDesignation(String designation) { + this.designation = designation; + } + public Department getDepartment() { + return department; + } + public void setDepartment(Department department) { + this.department = department; + } +} diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java b/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java new file mode 100644 index 0000000000..8b2fd4854d --- /dev/null +++ b/hibernate5/src/test/java/com/baeldung/hibernate/CustomClassIntegrationTest.java @@ -0,0 +1,36 @@ +package com.baeldung.hibernate; + +import static org.junit.jupiter.api.Assertions.*; + +import java.io.IOException; +import java.util.List; +import java.util.TimeZone; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.query.Query; +import org.junit.Before; +import org.junit.jupiter.api.Test; + +class CustomClassIntegrationTest { + + private Session session; + + private Transaction transaction; + + @Before + public void setUp() throws IOException { + session = HibernateUtil.getSessionFactory().openSession(); + transaction = session.beginTransaction(); + session.createNativeQuery("delete from employee").executeUpdate(); + session.createNativeQuery("delete from department").executeUpdate(); + transaction.commit(); + } + + @Test + void whenAllEmployeesSelected_ThenObjectGraphReturned() { + Query query = session.createQuery("from employee"); + List employees = query.list(); + } + +}