Jazzepi
Jazzepi

Reputation: 5490

No Persistence provider for EntityManager error using glassfish v3

I'm getting the below error on Glassfish v3 running in the server tab of Eclipse.

javax.servlet.ServletException: javax.persistence.PersistenceException: No Persistence provider for EntityManager named chatroom

When I try to run this code from a @Stateless bean and the @Entity. I've included the bean's code at the very end for reference (though I don't think it's the issue!)

    EntityManagerFactory factory = Persistence.createEntityManagerFactory("chatroom");

Below is my persistence.xml. I've tried to configure this as best as I can, but I'm not really sure what I'm doing. I get that I'm wiring the entity class ChatHistory to the database, but I'm not sure how to check up the connection to the database, and make sure that it's working properly. I have the database running and can see it in the Data Source Explorer. The username and password are correct.

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!";
    }
}

Upvotes: 1

Views: 2422

Answers (1)

Ilya
Ilya

Reputation: 29721

You should inject EntityManager with annotation

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

also check, that class org.eclipse.persistence.jpa.PersistenceProvider present in your classpath

Upvotes: 1

Related Questions