Added entities and basic select

This commit is contained in:
Priyesh Mashelkar 2018-09-04 00:01:25 +01:00
parent a3f6ad6d88
commit 4798036089
3 changed files with 99 additions and 0 deletions

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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();
}
}