Added entities and basic select
This commit is contained in:
parent
a3f6ad6d88
commit
4798036089
|
@ -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<Employee> employees;
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue