我在使用注释创建持久性上下文时遇到问题。问题出在哪里?
我尝试使用其他版本
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.*;
import java.util.List;
@Stateful
public class UserService {
@PersistenceContext(unitName = "JPAUNIT", type = PersistenceContextType.EXTENDED)
private EntityManager em;
public boolean saveUsr(User user){
em.getTransaction().begin();
try {
em.persist(user);
em.getTransaction().commit();
return true;
}catch (Exception e){
System.out.println("User exists");
return false;
}
}
//other methods
}
persistence.xml
<persistence-unit name="JPAUNIT">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>classes</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/points"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
<property name="hibernate.connection.username" value="postgres"/>
<property name="hibernate.connection.password" value="1234"/>
</properties>
</persistence-unit>
pom.xml
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.2-api</artifactId>
<version>1.0.0.Beta2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.3.7.Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.27</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
StackTrace
准备应用程序时发生异常:java.lang.ClassNotFoundException:org.hibernate.jpa.HibernatePersistenceProvider java.lang.ClassNotFoundException:org.hibernate.jpa.HibernatePersistenceProvider 在org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1621) 在org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1471) 在org.glassfish.persistence.jpa.PersistenceUnitLoader.loadPU(PersistenceUnitLoader.java:164) 在org.glassfish.persistence.jpa.PersistenceUnitLoader。(PersistenceUnitLoader.java:107) 在org.glassfish.persistence.jpa.JPADeployer $ 1.visitPUD(JPADeployer.java:223) 在org.glassfish.persistence.jpa.JPADeployer $ PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:510) 在org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:230)
更新: 试图使用@SteveC所说的其他版本,但是并不能解决这个问题
添加了堆栈跟踪和pom.xml
答案 0 :(得分:0)
谢谢@SteveC
已解决 刚刚使用此依赖项创建了一个新项目
persistence.xml相同
src / main / java / META-INF / persistence.xml
pom.xml
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.3.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>