简体   繁体   中英

Unable to retrieve EntityManagerFactory for unitName

When I try to reach a specific endpoint I get an exception saying "Unable to retrieve EntityManagerFactory for unitName parent_module "

However, I get this error since I structured the project on modules. When I had everything in the parent module, and had the packaging tag as war, it worked without any errors.

When I structured the project on modules, and made the parent packaging tag as pom, and the child module as war, I started to have the described exception.

Please note that I have the persistence.xml in the right path.

CHILD_MODULE/src/main/resources/META-INF/persistence.xml

anyway, the configuration should be good, as long as it works perfectly if I move the files in the parent module.

The persistence.xml is present in the target folder at the execution time.

I have tried annotating the EntityManager like @PersistenceContext(unitName = "parent_module"), but i get the same exception.

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="parent_module"
                      transaction-type="JTA">
        <jta-data-source>jdbc/postgres.myproject</jta-data-source>
        <mapping-file>META-INF/core-entity-mappings.xml</mapping-file>
    </persistence-unit>
</persistence>
public abstract class AbstractAccessBA {

    @PersistenceContext
    protected EntityManager entityManager;
    /**
     * Flush all changes from the entity manager to the database
     */
    public void flush() {
        entityManager.flush();
    }
}
public class WorkAccessBA extends AbstractAccessBA {

    @Inject
    private WorkMapper workMapper;
    /**
     * Finds all the entries
     * @return a list of workBE
     */
    public List<WorkBE> findAll() {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<WorkBE> cq = cb.createQuery(WorkBE.class);
        Root<WorkBE> rootEntry = cq.from(WorkBE.class);
        CriteriaQuery<WorkBE> all = cq.select(rootEntry);
        TypedQuery<WorkBE> allQuery = entityManager.createQuery(all);
        return allQuery.getResultList();
    }

It should return a list of all the result founded(Again, this works if I move all the files in the parent module), but when I move the files(configuration in the child module, which has a packaging tag "war", it returns me the error described.

"Unable to retrieve EntityManagerFactory for unitName parent_module "

Found the answer myself. The problem was in glassfish/domains/nameOftheDomain/applications

There was the parent_module SNAPSHOT that I used to deploy it as war before structuring the application with parent-children modules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM