我收到此错误:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyUnit
但设置应该是正确的。我不能再了解世界了。
它是一个Maven项目:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MyGroupID</groupId>
<artifactId>MyArtifactID</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>MyProject Maven Webapp</name>
<build>
<finalName>MyProject</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.168</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.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.1.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
</project>
我的persistence.xml位于“src / main / resources / META-INF / persistence.xml”下 可以使用此persistence.xml将数据插入到我的数据库中 所以它绝对存在!
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="MyUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>sample.beans.MyBean</class>
<properties>
<property name="persistence-context.persist-on-commit" value="true"/>
<property name="hibernate.connection.url" value="jdbc:h2:./data/datastore" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"></property>
<property name="hibernate.connection.driver_class" value="org.h2.Driver" />
<property name="hibernate.connection.password" value="admin" />
<property name="hibernate.connection.username" value="admin" />
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
我尝试在JSP中使用它
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:useBean id="my" class="sample.beans.MyBean"></jsp:useBean>
<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
<title>Bean Titles</title>
</head>
<body>
<h2>Bean Titles</h2>
<ul>
<c:forEach var="title" items="${my.titles}">
<li><c:out value="${title}"/></li>
</c:forEach>
</ul>
</body>
</html>
正如我已发布的......如果我在我的glassfish服务器上打开这个jsp端我 得到这个错误:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyUnit
这是我打开实体管理器的类:
public class MyBean implements Serializable {
private List<String> titles = null
public List<String> getTitles() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("MyUnit");
EntityManager em = factory.createEntityManager();
Query query = em.createQuery("SELECT title FROM Bla");
titles = query.getResultList();
return titles;
}
}
我真的不知道这里的问题是什么。我已经在家里的电脑上工作了。 在我的笔记本电脑上(同一个项目!)它不起作用.....