Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
si1718-example-project-persistence
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Daniel González Peña
si1718-example-project-persistence
Commits
7025724b
Commit
7025724b
authored
Nov 16, 2017
by
Daniel González Peña
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds Employee, Project and ProjectAssignment
parent
7d9d95d3
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
377 additions
and
1 deletion
+377
-1
Department.java
src/main/java/dgpena/siexample/persistence/Department.java
+28
-0
Departments.java
src/main/java/dgpena/siexample/persistence/Departments.java
+4
-0
Employee.java
src/main/java/dgpena/siexample/persistence/Employee.java
+49
-0
Employees.java
src/main/java/dgpena/siexample/persistence/Employees.java
+17
-0
Project.java
src/main/java/dgpena/siexample/persistence/Project.java
+56
-0
ProjectAssignment.java
.../java/dgpena/siexample/persistence/ProjectAssignment.java
+97
-0
persistence.xml
src/main/resources/META-INF/persistence.xml
+3
-1
DepartmentsTest.java
...st/java/dgpena/siexample/persistence/DepartmentsTest.java
+54
-0
EmployeesTest.java
...test/java/dgpena/siexample/persistence/EmployeesTest.java
+65
-0
persistence.xml
src/test/resources/META-INF/persistence.xml
+4
-0
No files found.
src/main/java/dgpena/siexample/persistence/Department.java
View file @
7025724b
package
dgpena
.
siexample
.
persistence
;
import
java.util.Collections
;
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
Department
{
...
...
@@ -14,6 +19,9 @@ public class Department {
private
String
name
;
@OneToMany
(
mappedBy
=
"department"
)
private
Set
<
Employee
>
employees
=
new
HashSet
<
Employee
>();
public
int
getId
()
{
return
id
;
}
...
...
@@ -25,4 +33,24 @@ public class Department {
public
void
setName
(
String
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
);
}
}
src/main/java/dgpena/siexample/persistence/Departments.java
View file @
7025724b
...
...
@@ -21,6 +21,10 @@ public class Departments {
}
public
void
deleteDepartment
(
Department
d
)
{
// remove first my employees
for
(
Employee
e:
d
.
getEmployees
())
{
e
.
setDepartment
(
null
);
}
em
.
remove
(
d
);
}
...
...
src/main/java/dgpena/siexample/persistence/Employee.java
0 → 100644
View file @
7025724b
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
);
}
}
}
src/main/java/dgpena/siexample/persistence/Employees.java
0 → 100644
View file @
7025724b
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
);
}
}
src/main/java/dgpena/siexample/persistence/Project.java
0 → 100644
View file @
7025724b
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
);
}
}
src/main/java/dgpena/siexample/persistence/ProjectAssignment.java
0 → 100644
View file @
7025724b
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
;
}
}
}
src/main/resources/META-INF/persistence.xml
View file @
7025724b
...
...
@@ -4,7 +4,9 @@
version=
"2.0"
>
<persistence-unit
name=
"si-database"
transaction-type=
"RESOURCE_LOCAL"
>
<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>
<property
name=
"javax.persistence.jdbc.driver"
value=
"com.mysql.jdbc.Driver"
/>
<property
name=
"javax.persistence.jdbc.user"
value=
"siuser"
/>
...
...
src/test/java/dgpena/siexample/persistence/DepartmentsTest.java
View file @
7025724b
...
...
@@ -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
public
void
testUpdateDepartment
()
throws
SQLException
{
// insert a department previously with JDBC
...
...
@@ -109,12 +131,44 @@ public class DepartmentsTest extends SQLBasedTest {
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
public
void
testFindAllDepartments
()
throws
SQLException
{
// insert a department previously with JDBC
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
=
jdbcConnection
.
createStatement
();
statement
.
executeUpdate
(
"INSERT INTO Department(name) values('dept-1')"
,
Statement
.
RETURN_GENERATED_KEYS
);
...
...
src/test/java/dgpena/siexample/persistence/EmployeesTest.java
0 → 100644
View file @
7025724b
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"
));
}
}
src/test/resources/META-INF/persistence.xml
View file @
7025724b
...
...
@@ -4,6 +4,10 @@
version=
"2.0"
>
<persistence-unit
name=
"si-database"
transaction-type=
"RESOURCE_LOCAL"
>
<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>
<property
name=
"javax.persistence.jdbc.driver"
value=
"com.mysql.jdbc.Driver"
/>
<property
name=
"javax.persistence.jdbc.user"
value=
"siuser"
/>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment