我在Eclipse的服务器选项卡中运行的Glassfish v3上遇到以下错误。
javax.servlet.ServletException: javax.persistence.PersistenceException: No Persistence provider for EntityManager named chatroom
当我尝试从@Stateless bean和@Entity运行此代码时。我已经将bean的代码包含在最后作为参考(虽然我不认为这是问题!)
EntityManagerFactory factory = Persistence.createEntityManagerFactory("chatroom");
下面是我的persistence.xml。我尽力配置这个,但我不确定我在做什么。我知道我将实体类ChatHistory连接到数据库,但我不确定如何检查与数据库的连接,并确保它正常工作。我运行了数据库,可以在Data Source Explorer中看到它。用户名和密码是正确的。
http://oi44.tinypic.com/5nyq9.jpg
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="chatroom" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.janp.castlerock.ChatHistory</class>
<properties>
<property name="eclipselink.target-database" value="DERBY"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/MyDB;create=true"/>
<property name="javax.persistence.jdbc.user" value="username"/>
<property name="javax.persistence.jdbc.password" value="password"/>
</properties>
</persistence-unit>
</persistence>
ChatHistory.java
package org.janp.castlerock;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
@Entity
public class ChatHistory {
private int id;
private List<String> messages;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@NotNull
public List<String> getMessages() {
return messages;
}
public void setMessages(List<String> messages) {
this.messages = messages;
}
}
Chatroom.java
package org.janp.castlerock;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("/chatrooms")
@Stateless
public class Chatroom {
ChatHistory history;
@EJB
ConverterBean converterBean;
@GET
@Produces("text/plain")
public String getClichedMessage() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("chatroom");
EntityManager em = factory.createEntityManager();
Query q = em.createQuery("select t from Todo t");
List<ChatHistory> messages = q.getResultList();
for (ChatHistory element : messages) {
System.out.println(element);
}
System.out.println("Size: " + messages.size());
ArrayList<String> temp = new ArrayList<String>();
temp.add("HELLO WORLD");
temp.add("I AM FINE");
em.getTransaction().begin();
ChatHistory messagehistory = new ChatHistory();
messagehistory.setMessages(temp);
em.persist(messagehistory);
em.getTransaction().commit();
em.close();
messages = q.getResultList();
for (ChatHistory element : messages) {
System.out.println(element);
}
System.out.println("Size: " + messages.size());
return "SUCCESS!:" + messages;
// System.out.println("blahblha");
// converterBean.dollarToYen(new BigDecimal(5.0));
// return "Success!";
}
}
答案 0 :(得分:1)
您应该注入EntityManager
注释
@Stateless
public class Chatroom
{
@PersistenceContext(unitName = "chatroom")
private EntityManager entityManager;
还要检查类路径中存在的类org.eclipse.persistence.jpa.PersistenceProvider