Skip to content
Snippets Groups Projects
Commit 560ae5e8 authored by Goik Martin's avatar Goik Martin
Browse files

Removing a project assignment

parent f835a359
No related branches found
No related tags found
No related merge requests found
......@@ -30,7 +30,32 @@ public class Employee {
String name, email;
@OneToMany(mappedBy="employee", cascade=CascadeType.PERSIST)
@OneToMany(mappedBy="employee", cascade=CascadeType.ALL)
Set<WorksFor> projects = new HashSet<WorksFor>();
public Long getId() { return id;}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Set<WorksFor> getProjects() {
return projects;
}
public void setProjects(Set<WorksFor> projects) {
this.projects = projects;
}
}
......@@ -31,7 +31,7 @@ public class Project {
String name;
@OneToMany(mappedBy="project", cascade=CascadeType.PERSIST)
@OneToMany(mappedBy="project", cascade=CascadeType.ALL)
Set<WorksFor> employees = new HashSet<WorksFor>();
public String getName() {
......
......@@ -5,6 +5,8 @@ import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PostRemove;
import javax.persistence.PreRemove;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
......@@ -40,11 +42,14 @@ public class WorksFor {
}
public void setProject(Project project) {
if (null != this.project && this.project != project ) {
this.project.employees.remove(this);
}
this.project = project;
project.employees.add(this);
if (null == this.project) {
this.project = project;
project.employees.add(this);
} else if (this.project != project) {
this.project.employees.remove(this);
this.project = project;
project.employees.add(this);
}
}
public Employee getEmployee() {
......@@ -67,5 +72,16 @@ public class WorksFor {
this.weekly_hours = weekly_hours;
}
@PreRemove
void preRemove() {
project.getEmployees().remove(this);
employee.getProjects().remove(this);
}
@PostRemove
void postRemove() {
project = null;
employee = null;
weekly_hours = 0;
}
}
package de.hdm_stuttgart.mi.company;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.junit.Test;
import de.hdm_stuttgart.mi.company.model.Employee;
import de.hdm_stuttgart.mi.company.model.Project;
import de.hdm_stuttgart.mi.company.model.WorksFor;
/**
* Assigning a user to a project and releasing the assignment
* in a second transaction.
*
*/
public class DeleteProjectAssignment {
static final EntityManager em;
static {
final EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("create-drop");
em = emFactory.createEntityManager();
}
@Test
public void createDropAssignment() {
Long jimId;
{
final EntityTransaction transaction = em.getTransaction();
transaction.begin();
final Project bridge = new Project("Bridge");
final Employee jim = new Employee("Jom Bone", "bone@graveyard.com");
new WorksFor(bridge, jim, 13);
em.persist(bridge);
transaction.commit();
jimId = jim.getId();
}
{
final EntityTransaction transaction = em.getTransaction();
transaction.begin();
final Employee jim = em.find(Employee.class, jimId);
for (WorksFor wf: jim.getProjects()) {
em.remove(wf);
}
transaction.commit();
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment