Adds Employee, Project and ProjectAssignment

parent 7d9d95d3
package dgpena.siexample.persistence; package dgpena.siexample.persistence;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity @Entity
public class Department { public class Department {
...@@ -14,6 +19,9 @@ public class Department { ...@@ -14,6 +19,9 @@ public class Department {
private String name; private String name;
@OneToMany(mappedBy="department")
private Set<Employee> employees = new HashSet<Employee>();
public int getId() { public int getId() {
return id; return id;
} }
...@@ -25,4 +33,24 @@ public class Department { ...@@ -25,4 +33,24 @@ public class Department {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public Set<Employee> getEmployees() {
return Collections.unmodifiableSet(employees);
}
public void addEmployee(Employee e) {
e.setDepartment(this);
}
public void removeEmployee(Employee e) {
e.setDepartment(null);
}
void internalAddEmployee(Employee e) {
this.employees.add(e);
}
void internalRemoveEmployee(Employee e) {
this.employees.remove(e);
}
} }
...@@ -21,6 +21,10 @@ public class Departments { ...@@ -21,6 +21,10 @@ public class Departments {
} }
public void deleteDepartment(Department d) { public void deleteDepartment(Department d) {
// remove first my employees
for (Employee e: d.getEmployees()) {
e.setDepartment(null);
}
em.remove(d); em.remove(d);
} }
......
package dgpena.siexample.persistence;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@ManyToOne
private Department department;
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
if (this.department != null) {
this.department.internalRemoveEmployee(this);
}
this.department = department;
if (this.department != null) {
department.internalAddEmployee(this);
}
}
}
package dgpena.siexample.persistence;
import javax.persistence.EntityManager;
public class Employees {
private EntityManager em;
public Employees(EntityManager em) {
this.em = em;
}
public void addNewEmployee(Employee e) {
this.em.persist(e);
}
}
package dgpena.siexample.persistence;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Project {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@OneToMany(mappedBy="project")
private Set<ProjectAssignment> projectAssignments = new HashSet<>();
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Employee> getEmployees() {
Set<Employee> employees = new HashSet<>();
for (ProjectAssignment pa : projectAssignments) {
employees.add(pa.getEmployee());
}
return employees;
}
void internalRemoveProjectAssignment(ProjectAssignment projectAssignment) {
this.projectAssignments.remove(projectAssignment);
}
void internalAddProjectAssignment(ProjectAssignment projectAssignment) {
this.projectAssignments.add(projectAssignment);
}
}
package dgpena.siexample.persistence;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.ManyToOne;
@Entity
@IdClass(ProjectAssignment.ProjectAssignmentId.class)
public class ProjectAssignment {
@Id
@ManyToOne
private Project project;
@Id
@ManyToOne
private Employee employee;
private Date startDate;
public Project getProject() {
return project;
}
public Employee getEmployee() {
return employee;
}
public void setProject(Project project) {
if (this.project != null) {
project.internalRemoveProjectAssignment(this);
}
this.project = project;
if (this.project != null) {
this.project.internalAddProjectAssignment(this);
}
this.project = project;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public static class ProjectAssignmentId implements Serializable {
private int project;
private int employee;
public ProjectAssignmentId() {}
public ProjectAssignmentId(int project, int employee) {
super();
this.project = project;
this.employee = employee;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + employee;
result = prime * result + project;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ProjectAssignmentId other = (ProjectAssignmentId) obj;
if (employee != other.employee)
return false;
if (project != other.project)
return false;
return true;
}
}
}
...@@ -4,7 +4,9 @@ ...@@ -4,7 +4,9 @@
version="2.0"> version="2.0">
<persistence-unit name="si-database" transaction-type="RESOURCE_LOCAL"> <persistence-unit name="si-database" transaction-type="RESOURCE_LOCAL">
<class>dgpena.siexample.persistence.Department</class> <class>dgpena.siexample.persistence.Department</class>
<class>dgpena.siexample.persistence.Employee</class>
<class>dgpena.siexample.persistence.Project</class>
<class>dgpena.siexample.persistence.ProjectAssignment</class>
<properties> <properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="siuser" /> <property name="javax.persistence.jdbc.user" value="siuser" />
......
...@@ -64,6 +64,28 @@ public class DepartmentsTest extends SQLBasedTest { ...@@ -64,6 +64,28 @@ public class DepartmentsTest extends SQLBasedTest {
} }
@Test
public void testFindByIdWithEmployees() throws SQLException {
// insert a department previously with JDBC
Statement statement = jdbcConnection.createStatement();
statement.executeUpdate("INSERT INTO Department(name) values('finanzas')", Statement.RETURN_GENERATED_KEYS);
int deptId = getLastInsertedId(statement);
// insert an employee in this department previously with JDBC
statement = jdbcConnection.createStatement();
statement.executeUpdate("INSERT INTO Employee(name, department_id) values('pepe', "+deptId+")", Statement.RETURN_GENERATED_KEYS);
int empId = getLastInsertedId(statement);
EntityManager em = emf.createEntityManager();
Departments depts = new Departments(em);
Department d = depts.findById(deptId);
Set<Employee> departmentEmployees = d.getEmployees();
assertEquals(1, departmentEmployees.size());
assertEquals(empId, departmentEmployees.iterator().next().getId());
}
@Test @Test
public void testUpdateDepartment() throws SQLException { public void testUpdateDepartment() throws SQLException {
// insert a department previously with JDBC // insert a department previously with JDBC
...@@ -109,12 +131,44 @@ public class DepartmentsTest extends SQLBasedTest { ...@@ -109,12 +131,44 @@ public class DepartmentsTest extends SQLBasedTest {
assertEquals(0, rs.getInt("total")); assertEquals(0, rs.getInt("total"));
} }
@Test
public void testDeleteDepartmentWithEmployees() throws SQLException {
// insert a department previously with JDBC
Statement statement = jdbcConnection.createStatement();
statement.executeUpdate("INSERT INTO Department(name) values('finanzas')", Statement.RETURN_GENERATED_KEYS);
int deptId = getLastInsertedId(statement);
// insert an employee in this department previously with JDBC
statement = jdbcConnection.createStatement();
statement.executeUpdate("INSERT INTO Employee(name, department_id) values('pepe', "+deptId+")", Statement.RETURN_GENERATED_KEYS);
int empId = getLastInsertedId(statement);
EntityManager em = emf.createEntityManager();
Departments depts = new Departments(em);
Department d = depts.findById(deptId);
em.getTransaction().begin();
depts.deleteDepartment(d);
em.getTransaction().commit();
// check in the DB using JDBC
statement = jdbcConnection.createStatement();
ResultSet rs = statement.executeQuery("SELECT COUNT(*) as total FROM Department d where d.id = "+deptId);
rs.next();
assertEquals(0, rs.getInt("total"));
}
@Test @Test
public void testFindAllDepartments() throws SQLException { public void testFindAllDepartments() throws SQLException {
// insert a department previously with JDBC // insert a department previously with JDBC
Statement statement = jdbcConnection.createStatement(); Statement statement = jdbcConnection.createStatement();
statement.executeUpdate("UPDATE Employee set department_id = NULL WHERE department_id IS NOT NULL", Statement.RETURN_GENERATED_KEYS);
statement.executeUpdate("DELETE FROM Department", Statement.RETURN_GENERATED_KEYS); statement.executeUpdate("DELETE FROM Department", Statement.RETURN_GENERATED_KEYS);
statement = jdbcConnection.createStatement(); statement = jdbcConnection.createStatement();
statement.executeUpdate("INSERT INTO Department(name) values('dept-1')", Statement.RETURN_GENERATED_KEYS); statement.executeUpdate("INSERT INTO Department(name) values('dept-1')", Statement.RETURN_GENERATED_KEYS);
......
package dgpena.siexample.persistence;
import static org.junit.Assert.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.BeforeClass;
import org.junit.Test;
public class EmployeesTest extends SQLBasedTest {
private static EntityManagerFactory emf;
@BeforeClass
public static void setUpEntityManagerFactory() {
emf = Persistence.createEntityManagerFactory("si-database");
}
@Test
public void testAddNewEmployee() throws SQLException {
// insert a department previously with JDBC
Statement statement = jdbcConnection.createStatement();
statement.executeUpdate("INSERT INTO Department(name) values('finanzas')", Statement.RETURN_GENERATED_KEYS);
int deptId = getLastInsertedId(statement);
EntityManager em = emf.createEntityManager();
Departments depts = new Departments(em);
Department dept = depts.findById(deptId);
Employee e = new Employee();
e.setName("pepe");
e.setDepartment(dept);
Employees employees = new Employees(em);
em.getTransaction().begin();
employees.addNewEmployee(e);
// ensure that bi-directional relation is always consistent
assertEquals(1, dept.getEmployees().size());
em.getTransaction().commit();
int employeeId = e.getId();
statement = jdbcConnection.createStatement();
ResultSet res = statement.executeQuery("SELECT * from Employee where id = "+employeeId);
res.next();
assertEquals(deptId, res.getInt("department_id"));
assertEquals("pepe", res.getString("name"));
}
}
...@@ -4,6 +4,10 @@ ...@@ -4,6 +4,10 @@
version="2.0"> version="2.0">
<persistence-unit name="si-database" transaction-type="RESOURCE_LOCAL"> <persistence-unit name="si-database" transaction-type="RESOURCE_LOCAL">
<class>dgpena.siexample.persistence.Department</class> <class>dgpena.siexample.persistence.Department</class>
<class>dgpena.siexample.persistence.Employee</class>
<class>dgpena.siexample.persistence.Project</class>
<class>dgpena.siexample.persistence.ProjectAssignment</class>
<properties> <properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="siuser" /> <property name="javax.persistence.jdbc.user" value="siuser" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment