Skip to content

Commit 69b8688

Browse files
committed
JPA - Hello, World using Hibernate 5
1 parent b91d716 commit 69b8688

15 files changed

Lines changed: 561 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
30+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
31+
<attributes>
32+
<attribute name="maven.pomderived" value="true"/>
33+
</attributes>
34+
</classpathentry>
35+
<classpathentry kind="output" path="target/classes"/>
36+
</classpath>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>JPAWithHibernate5</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.hibernate.eclipse.console.hibernateBuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.m2e.core.maven2Builder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>org.jboss.tools.jst.web.kb.kbbuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
<buildCommand>
29+
<name>org.jboss.tools.cdi.core.cdibuilder</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
33+
<buildCommand>
34+
<name>org.eclipse.wst.validation.validationbuilder</name>
35+
<arguments>
36+
</arguments>
37+
</buildCommand>
38+
</buildSpec>
39+
<natures>
40+
<nature>org.eclipse.jdt.core.javanature</nature>
41+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
42+
<nature>org.hibernate.eclipse.console.hibernateNature</nature>
43+
<nature>org.jboss.tools.jst.web.kb.kbnature</nature>
44+
<nature>org.jboss.tools.cdi.core.cdinature</nature>
45+
</natures>
46+
</projectDescription>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3+
org.eclipse.jdt.core.compiler.compliance=1.8
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
default.configuration=JPAWithHibernate5
2+
eclipse.preferences.version=1
3+
hibernate3.enabled=true
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.javahelps</groupId>
5+
<artifactId>JPAWithHibernate5</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<dependencies>
9+
<!-- Logback to print the logs generated by Hibernate -->
10+
<dependency>
11+
<groupId>ch.qos.logback</groupId>
12+
<artifactId>logback-classic</artifactId>
13+
<version>1.1.7</version>
14+
</dependency>
15+
16+
<!-- Core of Hibernate -->
17+
<dependency>
18+
<groupId>org.hibernate</groupId>
19+
<artifactId>hibernate-core</artifactId>
20+
<version>5.2.3.Final</version>
21+
</dependency>
22+
23+
<!-- Entity manager which is required for JPA -->
24+
<dependency>
25+
<groupId>org.hibernate</groupId>
26+
<artifactId>hibernate-entitymanager</artifactId>
27+
<version>5.2.3.Final</version>
28+
</dependency>
29+
30+
31+
<!-- JDBC Connector for MySQL -->
32+
<dependency>
33+
<groupId>mysql</groupId>
34+
<artifactId>mysql-connector-java</artifactId>
35+
<version>6.0.4</version>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<plugins>
41+
<!-- This plugin is used to set the Java version 1.8 -->
42+
<plugin>
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-compiler-plugin</artifactId>
45+
<version>3.5.1</version>
46+
<configuration>
47+
<source>1.8</source>
48+
<target>1.8</target>
49+
</configuration>
50+
</plugin>
51+
</plugins>
52+
</build>
53+
</project>
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
package com.javahelps.jpa;
2+
3+
import java.util.List;
4+
5+
import javax.persistence.EntityManager;
6+
import javax.persistence.EntityManagerFactory;
7+
import javax.persistence.EntityTransaction;
8+
import javax.persistence.Persistence;
9+
10+
public class Main {
11+
// Create an EntityManagerFactory when you start the application.
12+
private static final EntityManagerFactory ENTITY_MANAGER_FACTORY = Persistence
13+
.createEntityManagerFactory("JavaHelps");
14+
15+
public static void main(String[] args) {
16+
17+
// Create two Students
18+
create(1, "Alice", 22); // Alice will get an id 1
19+
create(2, "Bob", 20); // Bob will get an id 2
20+
create(3, "Charlie", 25); // Charlie will get an id 3
21+
22+
// Update the age of Bob using the id
23+
upate(2, "Bob", 25);
24+
25+
// Delete the Alice from database
26+
delete(1);
27+
28+
// Print all the Students
29+
List<Student> students = readAll();
30+
if (students != null) {
31+
for (Student stu : students) {
32+
System.out.println(stu);
33+
}
34+
}
35+
36+
// NEVER FORGET TO CLOSE THE ENTITY_MANAGER_FACTORY
37+
ENTITY_MANAGER_FACTORY.close();
38+
}
39+
40+
/**
41+
* Create a new Student.
42+
*
43+
* @param name
44+
* @param age
45+
*/
46+
public static void create(int id, String name, int age) {
47+
// Create an EntityManager
48+
EntityManager manager = ENTITY_MANAGER_FACTORY.createEntityManager();
49+
EntityTransaction transaction = null;
50+
51+
try {
52+
// Get a transaction
53+
transaction = manager.getTransaction();
54+
// Begin the transaction
55+
transaction.begin();
56+
57+
// Create a new Student object
58+
Student stu = new Student();
59+
stu.setId(id);
60+
stu.setName(name);
61+
stu.setAge(age);
62+
63+
// Save the student object
64+
manager.persist(stu);
65+
66+
// Commit the transaction
67+
transaction.commit();
68+
} catch (Exception ex) {
69+
// If there are any exceptions, roll back the changes
70+
if (transaction != null) {
71+
transaction.rollback();
72+
}
73+
// Print the Exception
74+
ex.printStackTrace();
75+
} finally {
76+
// Close the EntityManager
77+
manager.close();
78+
}
79+
}
80+
81+
/**
82+
* Read all the Students.
83+
*
84+
* @return a List of Students
85+
*/
86+
public static List<Student> readAll() {
87+
88+
List<Student> students = null;
89+
90+
// Create an EntityManager
91+
EntityManager manager = ENTITY_MANAGER_FACTORY.createEntityManager();
92+
EntityTransaction transaction = null;
93+
94+
try {
95+
// Get a transaction
96+
transaction = manager.getTransaction();
97+
// Begin the transaction
98+
transaction.begin();
99+
100+
// Get a List of Students
101+
students = manager.createQuery("SELECT s FROM Student s", Student.class).getResultList();
102+
103+
// Commit the transaction
104+
transaction.commit();
105+
} catch (Exception ex) {
106+
// If there are any exceptions, roll back the changes
107+
if (transaction != null) {
108+
transaction.rollback();
109+
}
110+
// Print the Exception
111+
ex.printStackTrace();
112+
} finally {
113+
// Close the EntityManager
114+
manager.close();
115+
}
116+
return students;
117+
}
118+
119+
/**
120+
* Delete the existing Student.
121+
*
122+
* @param id
123+
*/
124+
public static void delete(int id) {
125+
// Create an EntityManager
126+
EntityManager manager = ENTITY_MANAGER_FACTORY.createEntityManager();
127+
EntityTransaction transaction = null;
128+
129+
try {
130+
// Get a transaction
131+
transaction = manager.getTransaction();
132+
// Begin the transaction
133+
transaction.begin();
134+
135+
// Get the Student object
136+
Student stu = manager.find(Student.class, id);
137+
138+
// Delete the student
139+
manager.remove(stu);
140+
141+
// Commit the transaction
142+
transaction.commit();
143+
} catch (Exception ex) {
144+
// If there are any exceptions, roll back the changes
145+
if (transaction != null) {
146+
transaction.rollback();
147+
}
148+
// Print the Exception
149+
ex.printStackTrace();
150+
} finally {
151+
// Close the EntityManager
152+
manager.close();
153+
}
154+
}
155+
156+
/**
157+
* Update the existing Student.
158+
*
159+
* @param id
160+
* @param name
161+
* @param age
162+
*/
163+
public static void upate(int id, String name, int age) {
164+
// Create an EntityManager
165+
EntityManager manager = ENTITY_MANAGER_FACTORY.createEntityManager();
166+
EntityTransaction transaction = null;
167+
168+
try {
169+
// Get a transaction
170+
transaction = manager.getTransaction();
171+
// Begin the transaction
172+
transaction.begin();
173+
174+
// Get the Student object
175+
Student stu = manager.find(Student.class, id);
176+
177+
// Change the values
178+
stu.setName(name);
179+
stu.setAge(age);
180+
181+
// Update the student
182+
manager.persist(stu);
183+
184+
// Commit the transaction
185+
transaction.commit();
186+
} catch (Exception ex) {
187+
// If there are any exceptions, roll back the changes
188+
if (transaction != null) {
189+
transaction.rollback();
190+
}
191+
// Print the Exception
192+
ex.printStackTrace();
193+
} finally {
194+
// Close the EntityManager
195+
manager.close();
196+
}
197+
}
198+
}

0 commit comments

Comments
 (0)