| .. | .. |
|---|
| 1 | +/* |
|---|
| 2 | +* Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved. |
|---|
| 3 | +*/ |
|---|
| 1 | 4 | package net.curisit.securis.ioc; |
|---|
| 2 | 5 | |
|---|
| 3 | 6 | import jakarta.enterprise.context.ApplicationScoped; |
|---|
| .. | .. |
|---|
| 8 | 11 | import org.apache.logging.log4j.LogManager; |
|---|
| 9 | 12 | import org.apache.logging.log4j.Logger; |
|---|
| 10 | 13 | |
|---|
| 14 | +/** |
|---|
| 15 | +* EntityManagerProvider |
|---|
| 16 | +* <p> |
|---|
| 17 | +* Simple provider for JPA {@link EntityManager} instances using the |
|---|
| 18 | +* persistence unit "localdb". Creates an {@link EntityManagerFactory} |
|---|
| 19 | +* once per application and returns a fresh {@link EntityManager} per call. |
|---|
| 20 | +* |
|---|
| 21 | +* Note: |
|---|
| 22 | +* - Callers are responsible for closing the obtained EntityManager. |
|---|
| 23 | +* |
|---|
| 24 | +* @author JRA |
|---|
| 25 | +* Last reviewed by JRA on Oct 5, 2025. |
|---|
| 26 | +*/ |
|---|
| 11 | 27 | @ApplicationScoped |
|---|
| 12 | 28 | public class EntityManagerProvider { |
|---|
| 13 | 29 | |
|---|
| 14 | 30 | @SuppressWarnings("unused") |
|---|
| 15 | | - private static final Logger log = LogManager.getLogger(EntityManagerProvider.class); |
|---|
| 31 | + private static final Logger log = LogManager.getLogger(EntityManagerProvider.class); |
|---|
| 16 | 32 | |
|---|
| 33 | + /** |
|---|
| 34 | + * entityManagerFactory<p> |
|---|
| 35 | + * Application-wide EMF built from persistence.xml PU "localdb". |
|---|
| 36 | + */ |
|---|
| 17 | 37 | private final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("localdb"); |
|---|
| 18 | 38 | |
|---|
| 39 | + /** |
|---|
| 40 | + * getEntityManager<p> |
|---|
| 41 | + * Create a new {@link EntityManager}. |
|---|
| 42 | + * |
|---|
| 43 | + * @return a new EntityManager; caller must close it |
|---|
| 44 | + */ |
|---|
| 19 | 45 | public EntityManager getEntityManager() { |
|---|
| 20 | 46 | return entityManagerFactory.createEntityManager(); |
|---|
| 21 | 47 | } |
|---|
| 22 | | - |
|---|
| 23 | 48 | } |
|---|