-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
44 lines (33 loc) · 1.33 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package demo;
import demo.model.Employee;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class Main {
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("jpa_inheritance_demo");
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
entityManager.getTransaction().begin();
entityManager.persist(new Employee("Worker"));
entityManager.persist(new Employee("Worker"));
entityManager.persist(new Employee("Manager"));
Object employeesCount = entityManager
.createQuery("SELECT COUNT(e) FROM Employee e")
.getSingleResult();
System.out.println("Employees: " + employeesCount);
entityManager.getTransaction().commit();
} catch (Exception e) {
System.err.println(e.toString());
entityManager.getTransaction().rollback();
} finally {
if (entityManagerFactory.isOpen()) {
entityManager.close();
}
if (entityManagerFactory.isOpen()) {
entityManagerFactory.close();
}
}
}
}