如何修复“无法为unitName null检索EntityManagerFactory”

时间:2019-04-24 10:15:05

标签: sqlite jpa glassfish persistence.xml java-ee-8

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

src / main / 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 / classes / 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>

1 个答案:

答案 0 :(得分:0)

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

@EJB
VarvRepositoryJPQL varvRepository;

然后调用它。