This repository was archived by the owner on Oct 21, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 7 files changed +158
-1
lines changed
java/es/msanchez/frameworks/java/spring/boot
test/java/es/msanchez/frameworks/java/spring/boot/hibernate Expand file tree Collapse file tree 7 files changed +158
-1
lines changed Original file line number Diff line number Diff line change 13
13
<maven .shade.version>3.2.1</maven .shade.version>
14
14
15
15
<!-- Normal Dependency Versions -->
16
+ <hibernate .core.version>5.4.3.Final</hibernate .core.version>
17
+ <hibernate .mysql.connector>8.0.16</hibernate .mysql.connector>
18
+
16
19
<spring .version>5.1.6.RELEASE</spring .version>
17
20
<spring-boot .version>2.0.0.RELEASE</spring-boot .version>
18
21
33
36
34
37
<dependencies >
35
38
39
+ <!-- Hibernate -->
40
+ <dependency >
41
+ <groupId >org.hibernate</groupId >
42
+ <artifactId >hibernate-core</artifactId >
43
+ <version >${hibernate.core.version} </version >
44
+ </dependency >
45
+
46
+ <dependency >
47
+ <groupId >mysql</groupId >
48
+ <artifactId >mysql-connector-java</artifactId >
49
+ <version >${hibernate.mysql.connector} </version >
50
+ </dependency >
51
+
52
+
36
53
<!-- Spring Boot -->
37
54
<dependency >
38
55
<groupId >org.springframework.boot</groupId >
Original file line number Diff line number Diff line change
1
+ package es .msanchez .frameworks .java .spring .boot .entity ;
2
+
3
+ import lombok .Data ;
4
+ import lombok .NoArgsConstructor ;
5
+
6
+ import javax .persistence .Entity ;
7
+ import javax .persistence .GeneratedValue ;
8
+ import javax .persistence .GenerationType ;
9
+ import javax .persistence .Id ;
10
+ import javax .persistence .Table ;
11
+
12
+ @ Entity
13
+ @ Table
14
+ @ Data
15
+ @ NoArgsConstructor
16
+ public class Hobby {
17
+
18
+ @ Id
19
+ @ GeneratedValue (strategy = GenerationType .IDENTITY )
20
+ private int id ;
21
+
22
+ private String name ;
23
+
24
+ }
Original file line number Diff line number Diff line change
1
+ package es .msanchez .frameworks .java .spring .boot .entity ;
2
+
3
+ import lombok .Data ;
4
+ import lombok .NoArgsConstructor ;
5
+
6
+ import javax .persistence .Entity ;
7
+ import javax .persistence .GeneratedValue ;
8
+ import javax .persistence .GenerationType ;
9
+ import javax .persistence .Id ;
10
+ import javax .persistence .Table ;
11
+
12
+ @ Table
13
+ @ Entity
14
+ @ Data
15
+ @ NoArgsConstructor
16
+ public class Person {
17
+
18
+ @ Id
19
+ @ GeneratedValue (strategy = GenerationType .IDENTITY )
20
+ private int id ;
21
+
22
+ private String name ;
23
+
24
+ private Integer age ;
25
+
26
+ }
Original file line number Diff line number Diff line change
1
+ package es .msanchez .frameworks .java .spring .boot .hibernate ;
2
+
3
+ import lombok .Getter ;
4
+ import lombok .extern .slf4j .Slf4j ;
5
+ import org .hibernate .SessionFactory ;
6
+ import org .hibernate .cfg .Configuration ;
7
+
8
+ @ Slf4j
9
+ public class HibernateUtil {
10
+
11
+ @ Getter
12
+ private static final SessionFactory SESSION_FACTORY = HibernateUtil .buildSessionFactory ();
13
+
14
+ private static SessionFactory buildSessionFactory () {
15
+ try {
16
+ return new Configuration ().configure ().buildSessionFactory ();
17
+ } catch (final Throwable ex ) {
18
+ log .error ("Failure on ini hibernate session. " , ex );
19
+ throw new ExceptionInInitializerError (ex );
20
+ }
21
+ }
22
+
23
+ public static void shutdown () {
24
+ SESSION_FACTORY .close ();
25
+ }
26
+
27
+ }
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" utf-8" ?>
2
+ <!DOCTYPE hibernate-configuration PUBLIC
3
+ "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4
+ "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5
+ <hibernate-configuration >
6
+ <session-factory >
7
+
8
+ <property name =" connection.url" >jdbc:mysql://localhost:3310/hibernate</property >
9
+ <property name =" connection.username" >hibernate_user</property >
10
+ <property name =" connection.password" >hibernate_pass</property >
11
+ <property name =" connection.driver_class" >com.mysql.jdbc.Driver</property >
12
+ <property name =" dialect" >org.hibernate.dialect.MySQL8Dialect</property >
13
+
14
+ <property name =" show_sql" >true</property >
15
+ <property name =" format_sql" >true</property >
16
+ <property name =" hibernate.hbm2ddl.auto" >update</property >
17
+
18
+ <!-- JDBC connection pool (use the built-in) -->
19
+ <property name =" connection.pool_size" >100</property >
20
+ <property name =" current_session_context_class" >thread</property >
21
+
22
+ <mapping class =" es.msanchez.frameworks.java.spring.boot.entity.Person" />
23
+ <mapping class =" es.msanchez.frameworks.java.spring.boot.entity.Hobby" />
24
+
25
+ </session-factory >
26
+ </hibernate-configuration >
Original file line number Diff line number Diff line change
1
+ package es .msanchez .frameworks .java .spring .boot .hibernate ;
2
+
3
+ import es .msanchez .frameworks .java .spring .boot .entity .Hobby ;
4
+ import org .hibernate .Session ;
5
+ import org .testng .annotations .AfterMethod ;
6
+ import org .testng .annotations .BeforeMethod ;
7
+ import org .testng .annotations .Test ;
8
+
9
+ public class HibernateUtilTest {
10
+
11
+ private Session session ;
12
+
13
+ @ BeforeMethod
14
+ public void setUp () {
15
+ this .session = HibernateUtil .getSESSION_FACTORY ().openSession ();
16
+ }
17
+
18
+ @ AfterMethod
19
+ public void tearDown () {
20
+ HibernateUtil .shutdown ();
21
+ }
22
+
23
+ @ Test
24
+ public void testInsert () {
25
+ // @GIVEN
26
+ session .beginTransaction ();
27
+
28
+ final Hobby hobby = new Hobby ();
29
+ hobby .setName ("Swimming" );
30
+
31
+ // @WHEN
32
+ session .save (hobby );
33
+ session .getTransaction ().commit ();
34
+
35
+ // @THEN
36
+ }
37
+ }
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ services:
9
9
# - ./dbcreation.sql:/tmp/dbcreation.sql
10
10
# - ./import.sh:/tmp/import.sh
11
11
ports :
12
- - " 3306 :3306"
12
+ - " 3310 :3306"
13
13
environment :
14
14
MYSQL_DATABASE : " hibernate"
15
15
MYSQL_USER : " hibernate_user"
You can’t perform that action at this time.
0 commit comments