1 package ejava.examples.orm.rel;
2
3 import javax.persistence.EntityManager;
4
5 import javax.persistence.EntityManagerFactory;
6 import javax.persistence.EntityTransaction;
7 import javax.persistence.Persistence;
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.junit.After;
12 import org.junit.AfterClass;
13 import org.junit.Before;
14 import org.junit.BeforeClass;
15
16
17
18
19
20
21
22
23
24
25 public abstract class DemoBase {
26 protected static Logger logBase = LoggerFactory.getLogger(DemoBase.class);
27 protected Logger logger = LoggerFactory.getLogger(getClass());
28 private static final String PERSISTENCE_UNIT = "ormRelations";
29
30 protected static EntityManagerFactory emf;
31 protected EntityManager em;
32
33 @BeforeClass
34 public static void setUpDownShared() throws Exception {
35 logBase.debug("*** setUpDownShared() ***");
36 emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
37 }
38
39 @Before
40 public void setUpBase() throws Exception {
41 em = emf.createEntityManager();
42 precleanup();
43 em.getTransaction().begin();
44 }
45
46 @After
47 public void tearDownBase() throws Exception {
48 EntityTransaction tx = em.getTransaction();
49 if (tx.isActive()) {
50 if (tx.getRollbackOnly() == true) { tx.rollback(); }
51 else { tx.commit(); }
52 }
53 postcleanup();
54 em.close();
55 }
56
57 @AfterClass
58 public static void tearDownBaseClass() throws Exception {
59 logBase.debug("*** tearDownBaseClass() ***");
60 if (emf != null) {
61 emf.close();
62 emf = null;
63 }
64 }
65
66 protected void precleanup() throws Exception {}
67 protected void postcleanup() throws Exception {}
68 }