2012-02-24 127 views
1

我在Eclipse的服務器選項卡上運行的Glassfish v3出現以下錯誤。沒有使用glassfish v3 EntityManager錯誤的持久性提供者

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

你必須確保你的persistence.xml是JPA你罐子/在你器物應用服務器的META-INF。 – 2012-02-24 20:37:20

+0

這是在castlerock-> WebContent - > META-INF – Jazzepi 2012-02-24 20:41:00

+0

不知道castlerock,但它應該去任何你的jpa類,或你的jpa代碼的jar META-INF。 – 2012-02-24 20:57:52

回答

1

你應該註釋注入EntityManager

@Stateless 
public class Chatroom 
{ 
    @PersistenceContext(unitName = "chatroom") 
    private EntityManager entityManager; 

還要檢查,存在於你的classpath類org.eclipse.persistence.jpa.PersistenceProvider

相關問題