Skip to content

Commit 79c0562

Browse files
authored
Merge pull request eugenp#5195 from priyeshmashelkar/master
BAEL-2096 Added classes for article
2 parents dbcbd1f + e146c2a commit 79c0562

5 files changed

Lines changed: 250 additions & 4 deletions

File tree

hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.baeldung.hibernate;
22

3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
import java.net.URL;
6+
import java.util.Properties;
7+
8+
import com.baeldung.hibernate.entities.DeptEmployee;
39
import com.baeldung.hibernate.optimisticlocking.OptimisticLockingCourse;
410
import com.baeldung.hibernate.optimisticlocking.OptimisticLockingStudent;
511
import com.baeldung.hibernate.pessimisticlocking.Individual;
@@ -16,10 +22,30 @@
1622
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
1723
import org.hibernate.service.ServiceRegistry;
1824

19-
import java.io.FileInputStream;
20-
import java.io.IOException;
21-
import java.net.URL;
22-
import java.util.Properties;
25+
import com.baeldung.hibernate.pojo.Course;
26+
import com.baeldung.hibernate.pojo.Employee;
27+
import com.baeldung.hibernate.pojo.EntityDescription;
28+
import com.baeldung.hibernate.pojo.OrderEntry;
29+
import com.baeldung.hibernate.pojo.OrderEntryIdClass;
30+
import com.baeldung.hibernate.pojo.OrderEntryPK;
31+
import com.baeldung.hibernate.pojo.Person;
32+
import com.baeldung.hibernate.pojo.Phone;
33+
import com.baeldung.hibernate.pojo.PointEntity;
34+
import com.baeldung.hibernate.pojo.PolygonEntity;
35+
import com.baeldung.hibernate.pojo.Product;
36+
import com.baeldung.hibernate.pojo.Student;
37+
import com.baeldung.hibernate.pojo.TemporalValues;
38+
import com.baeldung.hibernate.pojo.User;
39+
import com.baeldung.hibernate.pojo.UserProfile;
40+
import com.baeldung.hibernate.pojo.inheritance.Animal;
41+
import com.baeldung.hibernate.pojo.inheritance.Bag;
42+
import com.baeldung.hibernate.pojo.inheritance.Book;
43+
import com.baeldung.hibernate.pojo.inheritance.Car;
44+
import com.baeldung.hibernate.pojo.inheritance.MyEmployee;
45+
import com.baeldung.hibernate.pojo.inheritance.MyProduct;
46+
import com.baeldung.hibernate.pojo.inheritance.Pen;
47+
import com.baeldung.hibernate.pojo.inheritance.Pet;
48+
import com.baeldung.hibernate.pojo.inheritance.Vehicle;
2349

2450
public class HibernateUtil {
2551
private static SessionFactory sessionFactory;
@@ -72,6 +98,8 @@ private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry
7298
metadataSources.addAnnotatedClass(PessimisticLockingCourse.class);
7399
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Customer.class);
74100
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Address.class);
101+
metadataSources.addAnnotatedClass(DeptEmployee.class);
102+
metadataSources.addAnnotatedClass(com.baeldung.hibernate.entities.Department.class);
75103
metadataSources.addAnnotatedClass(OptimisticLockingCourse.class);
76104
metadataSources.addAnnotatedClass(OptimisticLockingStudent.class);
77105

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.hibernate.entities;
2+
3+
import java.util.List;
4+
5+
import javax.persistence.*;
6+
7+
@Entity
8+
public class Department {
9+
@Id
10+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
11+
private long id;
12+
13+
private String name;
14+
15+
@OneToMany(mappedBy="department")
16+
private List<DeptEmployee> employees;
17+
18+
public Department(String name) {
19+
this.name = name;
20+
}
21+
22+
public long getId() {
23+
return id;
24+
}
25+
26+
public void setId(long id) {
27+
this.id = id;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
public List<DeptEmployee> getEmployees() {
39+
return employees;
40+
}
41+
42+
public void setEmployees(List<DeptEmployee> employees) {
43+
this.employees = employees;
44+
}
45+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.hibernate.entities;
2+
3+
import javax.persistence.*;
4+
5+
@Entity
6+
public class DeptEmployee {
7+
@Id
8+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
9+
private long id;
10+
11+
private String employeeNumber;
12+
13+
private String designation;
14+
15+
private String name;
16+
17+
@ManyToOne
18+
private Department department;
19+
20+
public DeptEmployee(String name, String employeeNumber, Department department) {
21+
this.name = name;
22+
this.employeeNumber = employeeNumber;
23+
this.department = department;
24+
}
25+
26+
public long getId() {
27+
return id;
28+
}
29+
30+
public void setId(long id) {
31+
this.id = id;
32+
}
33+
34+
public String getEmployeeNumber() {
35+
return employeeNumber;
36+
}
37+
38+
public void setEmployeeNumber(String employeeNumber) {
39+
this.employeeNumber = employeeNumber;
40+
}
41+
42+
public String getName() {
43+
return name;
44+
}
45+
46+
public void setName(String name) {
47+
this.name = name;
48+
}
49+
50+
public Department getDepartment() {
51+
return department;
52+
}
53+
54+
public void setDepartment(Department department) {
55+
this.department = department;
56+
}
57+
58+
public String getDesignation() {
59+
return designation;
60+
}
61+
62+
public void setDesignation(String designation) {
63+
this.designation = designation;
64+
}
65+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
public class Result {
4+
private String employeeName;
5+
6+
private String departmentName;
7+
8+
public Result(String employeeName, String departmentName) {
9+
this.employeeName = employeeName;
10+
this.departmentName = departmentName;
11+
}
12+
13+
public Result() {
14+
}
15+
16+
public String getEmployeeName() {
17+
return employeeName;
18+
}
19+
20+
public void setEmployeeName(String employeeName) {
21+
this.employeeName = employeeName;
22+
}
23+
24+
public String getDepartmentName() {
25+
return departmentName;
26+
}
27+
28+
public void setDepartmentName(String departmentName) {
29+
this.departmentName = departmentName;
30+
}
31+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.baeldung.hibernate;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.util.List;
7+
8+
import com.baeldung.hibernate.entities.DeptEmployee;
9+
import org.hibernate.Session;
10+
import org.hibernate.Transaction;
11+
import org.hibernate.query.Query;
12+
import org.hibernate.transform.Transformers;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.Test;
15+
16+
import com.baeldung.hibernate.entities.Department;
17+
import com.baeldung.hibernate.pojo.Result;
18+
19+
public class CustomClassIntegrationTest {
20+
21+
private Session session;
22+
23+
private Transaction transaction;
24+
25+
@BeforeEach
26+
public void setUp() throws IOException {
27+
session = HibernateUtil.getSessionFactory().openSession();
28+
transaction = session.beginTransaction();
29+
session.createNativeQuery("delete from manager").executeUpdate();
30+
session.createNativeQuery("delete from department").executeUpdate();
31+
Department department = new Department("Sales");
32+
DeptEmployee employee = new DeptEmployee("John Smith", "001", department);
33+
session.persist(department);
34+
session.persist(employee);
35+
transaction.commit();
36+
transaction = session.beginTransaction();
37+
}
38+
39+
@Test
40+
public void whenAllManagersAreSelected_ThenObjectGraphIsReturned() {
41+
Query<DeptEmployee> query = session.createQuery("from com.baeldung.hibernate.entities.DeptEmployee");
42+
List<DeptEmployee> deptEmployees = query.list();
43+
DeptEmployee deptEmployee = deptEmployees.get(0);
44+
assertEquals("John Smith", deptEmployee.getName());
45+
assertEquals("Sales", deptEmployee.getDepartment().getName());
46+
}
47+
48+
@Test
49+
public void whenIndividualPropertiesAreSelected_ThenObjectArrayIsReturned() {
50+
Query query = session.createQuery("select m.name, m.department.name from com.baeldung.hibernate.entities.DeptEmployee m");
51+
List managers = query.list();
52+
Object[] manager = (Object[]) managers.get(0);
53+
assertEquals("John Smith", manager[0]);
54+
assertEquals("Sales", manager[1]);
55+
}
56+
57+
@Test
58+
public void whenResultConstructorInSelect_ThenListOfResultIsReturned() {
59+
Query<Result> query = session.createQuery("select new com.baeldung.hibernate.pojo.Result(m.name, m.department.name) "
60+
+ "from DeptEmployee m");
61+
List<Result> results = query.list();
62+
Result result = results.get(0);
63+
assertEquals("John Smith", result.getEmployeeName());
64+
assertEquals("Sales", result.getDepartmentName());
65+
}
66+
67+
@Test
68+
public void whenResultTransformerOnQuery_ThenListOfResultIsReturned() {
69+
Query query = session.createQuery("select m.name as employeeName, m.department.name as departmentName "
70+
+ "from com.baeldung.hibernate.entities.DeptEmployee m");
71+
query.setResultTransformer(Transformers.aliasToBean(Result.class));
72+
List<Result> results = query.list();
73+
Result result = results.get(0);
74+
assertEquals("John Smith", result.getEmployeeName());
75+
assertEquals("Sales", result.getDepartmentName());
76+
}
77+
}

0 commit comments

Comments
 (0)