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
c5dc318b
Commit
c5dc318b
authored
Nov 03, 2017
by
Daniel González Peña
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Department entity and basic tests
parent
340f8035
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
179 additions
and
0 deletions
+179
-0
.gitignore
.gitignore
+5
-0
Department.java
src/main/java/dgpena/siexample/persistence/Department.java
+28
-0
persistence.xml
src/main/resources/META-INF/persistence.xml
+26
-0
DepartmentTest.java
...est/java/dgpena/siexample/persistence/DepartmentTest.java
+63
-0
SQLBasedTest.java
src/test/java/dgpena/siexample/persistence/SQLBasedTest.java
+32
-0
persistence.xml
src/test/resources/META-INF/persistence.xml
+25
-0
No files found.
.gitignore
0 → 100644
View file @
c5dc318b
/target
.classpath
.project
.settings
src/main/java/dgpena/siexample/persistence/Department.java
0 → 100644
View file @
c5dc318b
package
dgpena
.
siexample
.
persistence
;
import
javax.persistence.Entity
;
import
javax.persistence.GeneratedValue
;
import
javax.persistence.GenerationType
;
import
javax.persistence.Id
;
@Entity
public
class
Department
{
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
private
int
id
;
private
String
name
;
public
int
getId
()
{
return
id
;
}
public
String
getName
()
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
}
src/main/resources/META-INF/persistence.xml
0 → 100644
View file @
c5dc318b
<persistence
xmlns=
"http://java.sun.com/xml/ns/persistence"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version=
"2.0"
>
<persistence-unit
name=
"si-database"
transaction-type=
"RESOURCE_LOCAL"
>
<class>
dgpena.siexample.persistence.Department
</class>
<properties>
<property
name=
"javax.persistence.jdbc.driver"
value=
"com.mysql.jdbc.Driver"
/>
<property
name=
"javax.persistence.jdbc.user"
value=
"siuser"
/>
<property
name=
"javax.persistence.jdbc.password"
value=
"sipass"
/>
<property
name=
"javax.persistence.jdbc.url"
value=
"jdbc:mysql://localhost:3306/si"
/>
<property
name=
"hibernate.dialect"
value=
"org.hibernate.dialect.MySQL5InnoDBDialect"
/>
<property
name=
"hibernate.max_fetch_depth"
value=
"3"
/>
<property
name=
"hibernate.hbm2ddl.auto"
value=
"create"
/>
<property
name=
"hibernate.show_sql"
value=
"true"
/>
<property
name=
"hibernate.format_sql"
value=
"true"
/>
<property
name=
"hibernate.use_sql_comments"
value=
"true"
/>
<property
name=
"hibernate.connection.autocommit"
value=
"false"
/>
<!-- alternatively to <class> and <property> declarations, you can use
a regular hibernate.cfg.xml file -->
<!-- property name="hibernate.ejb.cfgfile" value="/org/hibernate/ejb/test/hibernate.cfg.xml"/ -->
</properties>
</persistence-unit>
</persistence>
src/test/java/dgpena/siexample/persistence/DepartmentTest.java
0 → 100644
View file @
c5dc318b
package
dgpena
.
siexample
.
persistence
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
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
DepartmentTest
extends
SQLBasedTest
{
private
static
EntityManagerFactory
emf
;
@BeforeClass
public
static
void
setUpEntityManagerFactory
()
{
emf
=
Persistence
.
createEntityManagerFactory
(
"si-database"
);
}
@Test
public
void
testCreateDepartment
()
throws
Exception
{
EntityManager
em
=
emf
.
createEntityManager
();
Department
d
=
new
Department
();
d
.
setName
(
"finanzas"
);
em
.
getTransaction
().
begin
();
em
.
persist
(
d
);
em
.
getTransaction
().
commit
();
// check in the DB using JDBC
int
deptId
=
d
.
getId
();
Statement
statement
=
jdbcConnection
.
createStatement
();
ResultSet
rs
=
statement
.
executeQuery
(
"SELECT COUNT(*) as total FROM Department d where d.id = "
+
deptId
);
rs
.
next
();
assertEquals
(
1
,
rs
.
getInt
(
"total"
));
}
@Test
public
void
testFindDepartment
()
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
();
Department
d
=
em
.
find
(
Department
.
class
,
deptId
);
assertEquals
(
deptId
,
d
.
getId
());
assertEquals
(
"finanzas"
,
d
.
getName
());
}
}
src/test/java/dgpena/siexample/persistence/SQLBasedTest.java
0 → 100644
View file @
c5dc318b
package
dgpena
.
siexample
.
persistence
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
public
class
SQLBasedTest
{
private
static
final
String
DB_URL
=
"jdbc:mysql://localhost:3306/si"
;
private
static
final
String
DB_USER
=
"siuser"
;
private
static
final
String
DB_PASS
=
"sipass"
;
protected
static
Connection
jdbcConnection
=
createConnection
();
private
static
Connection
createConnection
()
{
try
{
Class
.
forName
(
"com.mysql.jdbc.Driver"
);
return
DriverManager
.
getConnection
(
DB_URL
,
DB_USER
,
DB_PASS
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
}
protected
int
getLastInsertedId
(
Statement
statement
)
throws
SQLException
{
ResultSet
rs
=
statement
.
getGeneratedKeys
();
rs
.
next
();
return
rs
.
getInt
(
1
);
}
}
src/test/resources/META-INF/persistence.xml
0 → 100644
View file @
c5dc318b
<persistence
xmlns=
"http://java.sun.com/xml/ns/persistence"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version=
"2.0"
>
<persistence-unit
name=
"si-database"
transaction-type=
"RESOURCE_LOCAL"
>
<class>
dgpena.siexample.persistence.Department
</class>
<properties>
<property
name=
"javax.persistence.jdbc.driver"
value=
"com.mysql.jdbc.Driver"
/>
<property
name=
"javax.persistence.jdbc.user"
value=
"siuser"
/>
<property
name=
"javax.persistence.jdbc.password"
value=
"sipass"
/>
<property
name=
"javax.persistence.jdbc.url"
value=
"jdbc:mysql://localhost:3306/si"
/>
<property
name=
"hibernate.dialect"
value=
"org.hibernate.dialect.MySQL5InnoDBDialect"
/>
<property
name=
"hibernate.max_fetch_depth"
value=
"3"
/>
<property
name=
"hibernate.hbm2ddl.auto"
value=
"create"
/>
<property
name=
"hibernate.show_sql"
value=
"true"
/>
<property
name=
"hibernate.format_sql"
value=
"true"
/>
<property
name=
"hibernate.use_sql_comments"
value=
"true"
/>
<property
name=
"hibernate.connection.autocommit"
value=
"false"
/>
<!-- alternatively to <class> and <property> declarations, you can use
a regular hibernate.cfg.xml file -->
<!-- property name="hibernate.ejb.cfgfile" value="/org/hibernate/ejb/test/hibernate.cfg.xml"/ -->
</properties>
</persistence-unit>
</persistence>
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