如何修复'无法检索unitName null的EntityManagerFactory'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何修复'无法检索unitName null的EntityManagerFactory'相关的知识,希望对你有一定的参考价值。

我正在使用Glassfish 5.0和Maven框架中构建的Java Web App。我已经设置了连接池,并且在ping它时它已连接。我已将persistent.xml文件放入

的src /主/ JAVA / META-INF

配置:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 
xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="ramsoPU" transaction-type="JTA">
        <jta-data-source>jdbc/ramsovarvet</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="javax.persistence.schema-generation.database.action" value="create"/>

        </properties>
    </persistence-unit>
</persistence>

但是,当尝试实例化EntityManager时,我收到错误消息'由以下引起:java.lang.IllegalStateException:无法检索unitName null的EntityManagerFactory'

我已检查peristent.xml是否在war文件的类路径中。它在:

WEB-INF /班/ META-INF / persistence.xml中

JNDI名称是正确的,因为我在服务器上下文中设置它。

下面是我使用持久单元实例化EM的代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.city.douglas.varv.Job.dao;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import uk.ac.city.douglas.varv.Account.domain.Customer;
import uk.ac.city.douglas.varv.Job.domain.Boat;
import uk.ac.city.douglas.varv.Job.domain.BoatVariant;
import uk.ac.city.douglas.varv.Job.domain.BoatVariantEngine;

import uk.ac.city.douglas.varv.Job.domain.Engine;
import uk.ac.city.douglas.varv.Job.domain.Storage;

/**
 *
 * @author douglaslandvik
 */
@Stateless
public class VarvRepositoryJPQL implements VarvRepository {

    @PersistenceContext(unitName="ramsoPU")
    private EntityManager em;


    @Override
    public List<Customer> getAllCustomers(){

        TypedQuery query = em.createQuery("SELECT c FROM Customer AS c",Customer.class);
        return query.getResultList();
    }

    @Override
    public List<Boat> getAllBoats() {
        TypedQuery query = em.createQuery("SELECT b FROM Boat AS b ORDER by b.brand ASC",Boat.class);

        return query.getResultList();

    }

    @Override
    public void addEmployee() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void saveBoat(Boat boat) {
        em.persist(boat);

    }



    @Override
    public void saveCustomer(Customer customer) {
        em.persist(customer);
    }

    @Override
    public void eraseCustomerById(int id) {
        Customer customer = em.find(Customer.class, id);
        em.remove(customer);
    }

    @Override
    public List<Storage> getAllStorages() {
      TypedQuery query = em.createQuery("SELECT s FROM Storage AS s",Storage.class);
        return query.getResultList();

    }

    @Override
    public List<Engine> getAllEngines() {
         TypedQuery query = em.createQuery("SELECT e FROM Engine AS e",Engine.class);
        return query.getResultList();
    }

    @Override
    public List<BoatVariant> getAllBoatVariants() {
  TypedQuery query = em.createQuery("SELECT b FROM BoatVariant AS b",BoatVariant.class);
        return query.getResultList();    

    }



    @Override
    public void saveBoatVariant(BoatVariant boatVariant) {
    em.persist(boatVariant);    

    }

    @Override
    public List<Boat> getAllBoatsByBrand(String brand) {
        TypedQuery query = em.createQuery("SELECT b FROM Boat AS b WHERE b.brand LIKE :brand ORDER BY b.brand", Boat.class);
        query.setParameter("brand", brand+"%");

        return query.getResultList();


    }

    @Override
    public List<Boat> getAllBoatsByModel(String model) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Boat> getAllBoatsById(int id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<String> getAllBoatBrands() {
           TypedQuery query = em.createQuery("SELECT DISTINCT b.brand  FROM Boat AS b", String.class);
        return query.getResultList();   

    }

    @Override
    public Boat findBoatById(int id) {
        return em.find(Boat.class, id);
    }

    @Override
    public Customer findCustomerById(int id) {
         return em.find(Customer.class, id);
    }

    @Override
    public List<Engine> findEnginesByBrand(String brand) {
         TypedQuery query = em.createQuery("SELECT e FROM Engine AS e WHERE e.brand LIKE :brand ORDER BY e.brand", Engine.class);
        query.setParameter("brand", brand+"%");

        return query.getResultList();

    }

    @Override
    public void addEngine(Engine engine) {
            em.persist(engine);

    }


    @Override
    public Engine findEngineById(int id) {
          return em.find(Engine.class, id);

    }





    @Override
    public void removeEngineById(int id) {
    Engine engine = em.find(Engine.class, id);
        em.remove(engine);
    }

    @Override
    public List<BoatVariant> findAllBoatVariantByCustomerId(int customerId) {
    TypedQuery query = em.createQuery("SELECT bv FROM BoatVariant AS bv WHERE bv.customerId = :customerId ", BoatVariant.class);
        query.setParameter("customerId", customerId);

        return query.getResultList();

    }


    //nya jävlar

    @Override
    public void addBoatVariantEngine(BoatVariantEngine boatVariantEngine) {
        em.persist(boatVariantEngine);

    }

    @Override
    public void removeBoatVariantEngineById(BoatVariantEngine boatVariantEngine)  {
       em.remove(boatVariantEngine);
    }

    @Override
    public List<BoatVariantEngine> findBoatVariantEngineById(int boatId, int customerId, int engineId) {
        TypedQuery query = em.createQuery("SELECT bve FROM BoatVariantEngine AS bve WHERE bve.customerId = :customerId AND bve.boatId = :boatId AND bve.engineId = :engineId ", BoatVariantEngine.class);
        query.setParameter("customerId", customerId);
        query.setParameter("boatId", boatId);
        query.setParameter("engineId", engineId);

        return query.getResultList();

    }

    @Override
    public BoatVariant findAllBoatVariantByCustomerIdAndBoatId(int customerId, int boatId) {
    TypedQuery query = em.createQuery("SELECT bv FROM BoatVariant AS bv WHERE bve.customerId = :customerId AND bve.boatId = :boatId", BoatVariant.class);
        query.setParameter("customerId", customerId);
        query.setParameter("boatId", boatId);


        return (BoatVariant) query.getSingleResult();
    }

    @Override
    public void removeBoatVariant(BoatVariant boatVariant) {
        em.remove(boatVariant);
    }


}

pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>uk.ac.city.douglas</groupId>
    <artifactId>varv</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <name>varv</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
            <version>2.5.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.asayama.gwt.jquery</groupId>
            <artifactId>gwt-jquery</artifactId>
            <version>0.1.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.bluecatcode.mockito</groupId>
            <artifactId>mockito-1.10.19-extended</artifactId>
            <version>1.0.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>varv</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <warName>varv</warName>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
答案

持久性单元文件的名称必须是persistence.xml,但在您的情况下,文件的名称是persistent.xml,这就是容器无法检测持久性单元名称的原因。因此,将持久性文件名更改为persistence.xml。另外,不要忘记使用VarvRepositoryJPQL调用服务类@EJB,例如:

@EJB
VarvRepositoryJPQL varvRepository;

然后打电话给它。

以上是关于如何修复'无法检索unitName null的EntityManagerFactory'的主要内容,如果未能解决你的问题,请参考以下文章

Django模板如何修复错误“无法解析余数:'|'来自'|' ”

如何修复'require_once()无法打开流'

如何修复无法找到参数[project':app']的方法实现()

如何在Express中修复'无法读取属性'地图'未定义'

如何修复 Flutter build_runner 错误:无法预编译 build_runner:build_runner:

如何修复'Microsoft Excel无法打开或保存更多文档'