Я использую Glassfish 5.0 с веб-приложением Java, встроенным в платформу Maven. Я настроил пул подключений, и он подключается при пинге.
Я поместил файл 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: невозможно получить EntityManagerFactory для unitName null»
Я проверил, что файл 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>