Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6d8b2a5
Showing
10 changed files
with
471 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.iml | ||
target | ||
derby.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>jpa-builder-pattern</groupId> | ||
<artifactId>jpa-builder-pattern</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.inject</groupId> | ||
<artifactId>guice</artifactId> | ||
<version>4.1.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.inject.extensions</groupId> | ||
<artifactId>guice-persist</artifactId> | ||
<version>4.1.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.persistence</groupId> | ||
<artifactId>eclipselink</artifactId> | ||
<version>2.5.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.derby</groupId> | ||
<artifactId>derby</artifactId> | ||
<version>10.12.1.1</version> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-all</artifactId> | ||
<version>1.3</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package entity; | ||
|
||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.MappedSuperclass; | ||
|
||
@MappedSuperclass | ||
class AbstractEntity { | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(final Long id) { | ||
this.id = id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package entity; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.OneToMany; | ||
import java.util.Collection; | ||
|
||
@Entity | ||
public class Dept extends AbstractEntity { | ||
|
||
@Column(nullable = false) | ||
private String name; | ||
@OneToMany(mappedBy = "dept") | ||
private Collection<Employee> employees; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(final String name) { | ||
this.name = name; | ||
} | ||
|
||
public Collection<Employee> getEmployees() { | ||
return employees; | ||
} | ||
|
||
public void setEmployees(final Collection<Employee> employees) { | ||
this.employees = employees; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package entity; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.ManyToMany; | ||
import javax.persistence.ManyToOne; | ||
import javax.persistence.OneToMany; | ||
import java.util.Collection; | ||
|
||
@Entity | ||
public class Employee extends AbstractEntity { | ||
|
||
@Column(nullable = false) | ||
private String name; | ||
@Column(nullable = false, name = "temporary_emp") | ||
private Boolean temporary; | ||
@JoinColumn(nullable = false) | ||
@ManyToOne | ||
private Dept dept; | ||
@ManyToMany | ||
private Collection<Project> projects; | ||
@OneToMany(mappedBy = "employee") | ||
private Collection<Phone> phones; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(final String name) { | ||
this.name = name; | ||
} | ||
|
||
public Boolean getTemporary() { | ||
return temporary; | ||
} | ||
|
||
public void setTemporary(final Boolean temporary) { | ||
this.temporary = temporary; | ||
} | ||
|
||
public Dept getDept() { | ||
return dept; | ||
} | ||
|
||
public void setDept(final Dept dept) { | ||
this.dept = dept; | ||
} | ||
|
||
public Collection<Project> getProjects() { | ||
return projects; | ||
} | ||
|
||
public void setProjects(final Collection<Project> projects) { | ||
this.projects = projects; | ||
} | ||
|
||
public Collection<Phone> getPhones() { | ||
return phones; | ||
} | ||
|
||
public void setPhones(final Collection<Phone> phones) { | ||
this.phones = phones; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package entity; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.ManyToOne; | ||
|
||
@Entity | ||
public class Phone extends AbstractEntity { | ||
|
||
@Column(nullable = false, unique = true) | ||
private String number; | ||
@ManyToOne | ||
@JoinColumn(nullable = false) | ||
private Employee employee; | ||
|
||
public String getNumber() { | ||
return number; | ||
} | ||
|
||
public void setNumber(final String number) { | ||
this.number = number; | ||
} | ||
|
||
public Employee getEmployee() { | ||
return employee; | ||
} | ||
|
||
public void setEmployee(final Employee employee) { | ||
this.employee = employee; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package entity; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.ManyToMany; | ||
import java.util.Collection; | ||
|
||
@Entity | ||
public class Project extends AbstractEntity { | ||
|
||
@Column(nullable = false) | ||
private String name; | ||
@ManyToMany(mappedBy = "projects") | ||
private Collection<Employee> employees; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(final String name) { | ||
this.name = name; | ||
} | ||
|
||
public Collection<Employee> getEmployees() { | ||
return employees; | ||
} | ||
|
||
public void setEmployees(final Collection<Employee> employees) { | ||
this.employees = employees; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package service; | ||
|
||
import entity.Dept; | ||
import entity.Employee; | ||
import entity.Phone; | ||
import entity.Project; | ||
|
||
import javax.persistence.EntityManager; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class EmployeeService { | ||
|
||
private final EntityManager em; | ||
|
||
EmployeeService(final EntityManager em) { | ||
this.em = em; | ||
} | ||
|
||
public long create(long deptId, | ||
String name, | ||
boolean temporary, | ||
Set<Long> projectIds, | ||
Set<String> phoneNumbers) { | ||
|
||
// instantiating and setting attributes of employee | ||
final Employee employee = new Employee(); | ||
employee.setName(name); | ||
employee.setTemporary(temporary); | ||
|
||
// making a relation between employee and dept | ||
final Dept dept = em.find(Dept.class, deptId); | ||
employee.setDept(dept); | ||
dept.getEmployees().add(employee); | ||
|
||
// making relations between employee and projects | ||
employee.setProjects(new HashSet<>()); | ||
for (final Long projectId : projectIds) { | ||
final Project project = em.find(Project.class, projectId); | ||
project.getEmployees().add(employee); | ||
employee.getProjects().add(project); | ||
} | ||
|
||
// creating phones | ||
employee.setPhones(new HashSet<>()); | ||
for (final String phoneNumber : phoneNumbers) { | ||
final Phone phone = new Phone(); | ||
phone.setNumber(phoneNumber); | ||
phone.setEmployee(employee); | ||
employee.getPhones().add(phone); | ||
em.persist(phone); | ||
} | ||
|
||
em.persist(employee); | ||
em.flush(); // making sure a generated id is present | ||
|
||
return employee.getId(); | ||
} | ||
|
||
public Builder builder(long deptId, String name) { | ||
return new Builder(deptId, name); | ||
} | ||
|
||
public final class Builder { // not static | ||
private final long deptId; | ||
private final String name; | ||
private boolean temporary; | ||
private Set<Long> projectIds = new HashSet<>(); | ||
private Set<String> phoneNumbers = new HashSet<>(); | ||
|
||
public Builder(final long deptId, final String name) { | ||
this.deptId = deptId; | ||
this.name = name; | ||
} | ||
|
||
public Builder temporary() { | ||
temporary = true; | ||
return this; | ||
} | ||
|
||
public Builder projectIds(long... ids) { | ||
for (final long id : ids) { | ||
projectIds.add(id); | ||
} | ||
return this; | ||
} | ||
|
||
public Builder phoneNumbers(String... numbers) { | ||
phoneNumbers.addAll(Arrays.asList(numbers)); | ||
return this; | ||
} | ||
|
||
public long build() { | ||
return EmployeeService.this.create(deptId, name, temporary, projectIds, phoneNumbers); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> | ||
<persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL"> | ||
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> | ||
<class>entity.Dept</class> | ||
<class>entity.Employee</class> | ||
<class>entity.Phone</class> | ||
<class>entity.Project</class> | ||
<shared-cache-mode>NONE</shared-cache-mode> | ||
<properties> | ||
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/> | ||
<property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:myDB;create=true"/> | ||
<property name="javax.persistence.jdbc.user" value="app"/> | ||
<property name="javax.persistence.jdbc.password" value="app"/> | ||
|
||
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> | ||
|
||
<property name="eclipselink.logging.level" value="FINE"/> | ||
<property name="eclipselink.logging.parameters" value="true"/> | ||
|
||
<property name="hibernate.show_sql" value="true"/> | ||
</properties> | ||
</persistence-unit> | ||
</persistence> |
Oops, something went wrong.