Joaquín Reñé
2026-03-27 4ee50e257b32f6ec0f72907305d1f2b1212808a4
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
45
46
47
48
/*
* Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
*/
package net.curisit.securis.ioc;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* EntityManagerProvider
* <p>
* Simple provider for JPA {@link EntityManager} instances using the
* persistence unit "localdb". Creates an {@link EntityManagerFactory}
* once per application and returns a fresh {@link EntityManager} per call.
*
* Note:
* - Callers are responsible for closing the obtained EntityManager.
* @author JRA
* Last reviewed by JRA on Oct 5, 2025.
*/
@ApplicationScoped
public class EntityManagerProvider {
    @SuppressWarnings("unused")
    private static final Logger log = LogManager.getLogger(EntityManagerProvider.class);
    /** 
     * entityManagerFactory<p>
     * Application-wide EMF built from persistence.xml PU "localdb". 
     */
    private final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("localdb");
    /**
    * getEntityManager<p>
    * Create a new {@link EntityManager}.
    *
    * @return a new EntityManager; caller must close it
    */
    public EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }
}