Unknown entity class error message even though the entity is marked with @Entity annotation

16k views Asked by At

I am building REST web app using Netbean6.9.1 and JPA EclipseLink.

The issue I'm facing is even though my entity class MasatoTable is marked with Entity annotation, I get error:

    (java.lang.IllegalArgumentException: Unknown entity bean class:
     class entity.MasatoTable, please verify that this class
 has been marked with the @Entity annotation.)

The thing is when I restart the GlassFish3 server from NetbeanIDE, it works for a while and somehow at some point, the error start showing up. I used to use Toplink Essential and had no issue but I've changed to EclipseLink and re-defiend persistence.xml (shown below) and this problem started. But I don't see any code error that I can think of..

MasatoTable.java

@Entity
@Table(name = "MasatoTable")
public class MasatoTable implements Serializable {
...continue code...

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
  <persistence-unit name="kojoPU">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <non-jta-data-source>jdbc/koga_kojo</non-jta-data-source>
    <class>entity.MasatoTable</class>
    <properties>
      <property name="eclipselink.logging.logger" value="JavaLogger"/>
      <property name="eclipselink.logging.level.sql" value="FINEST"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://KOGADBSERVER;databaseName=MasatoDB"/>
      <property name="javax.persistence.jdbc.password" value="foobar"/>
      <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
      <property name="javax.persistence.jdbc.user" value="koga"/>
    </properties>
  </persistence-unit>
</persistence>

Looks like same issue but that ticket's solution is to rollback to Toplink from Eclipselink. Has anyone solve the issue without rolling back to toplink?

Unknown entity bean class after hot deploy: netbeans 6.9 + glassfish 2.1 + eclipselink jpa 2.0

UPDATE

As far as my research goes in terms of this case, look like the bug has been addressed and fixed?

https://bugs.eclipse.org/bugs/show_bug.cgi?id=288141

I'm not knowledgeable enough to understand the whole thing but there are few facts I found:

Netbean6.9.1 provides EclipseLink 2.0.1 whereas newest version is 2.2.0

I downloaded newest version and put them into the lib dir then re-deployed but no luck, the issue still persists. Not sure if I did things in wrong way.

UPDATE after James's comment

Below is the class I use to create singleton EntityManagerFactory and how it is being used.

package common;

import java.util.Date;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class EmProvider {

    private static final String DB_PU = "kojoPU"; //工場DB

    public static final boolean DEBUG = false;

    private static final EmProvider singleton = new EmProvider();

    private EntityManagerFactory emf;

    private EmProvider() {}

    public static EmProvider getInstance() {
        return singleton;
    }


    public EntityManagerFactory getEntityManagerFactory() {
        if(emf == null) {
            emf = Persistence.createEntityManagerFactory(DB_PU);
        }
        if(DEBUG) {
            System.out.println("factory created on: " + new Date());
        }
        return emf;
    }

    public void closeEmf() {
        if(emf.isOpen() || emf != null) {
            emf.close();
        }
        emf = null;
        if(DEBUG) {
            System.out.println("EMF closed at: " + new Date());
        }
    }

}//end class

How it is used:

EntityManager em = null;
   Object out = null;
   try {
       em = EmProvider.getInstance().getEntityManagerFactory().createEntityManager();
       Query query = em.createNativeQuery(sql);

       ....some code here
   }
   finally {
       if(em != null) {
           em.close();
       }
   }

By looking at my code, I think... I'm not closing EntityManagerFactory (i'm closing EntityManager in finally though)

I also I found the "unknown entity bean class" error in topic shows up for sure with following action.

  1. Deploy web app through Netbean (This will open the browser)

  2. Change some code in Netbean IDE and save it. Then Netbean will auto-re-deploy as soon as you press the save button (is this called "hot deploy" ???)

  3. Go back to browser and take some action like add some data which involves EntityManager.

Looks like the error occurs because I'm not closing entity manager factory when hot-deploy? I have to investigate.

3

There are 3 answers

1
James On BEST ANSWER

How are you creating your EntityManager/Factory, is it managed or non-managed?

If it is not managed, then I believe that EclipseLink will not be notified in any way of a redeployment, so will still have the old EntityManagerFactory cached as it is never closed. Can you ensure that you close the EntityManagerFactory after the hot deploy?

You can make the persistence unit a managed persistence unit, that may resolve the issue.

Either way, please log a bug for this with details in EclipseLink and Glassfish, as this issue should be tracked.

I think this may be the bug, https://bugs.eclipse.org/bugs/show_bug.cgi?id=326552

Please vote for this bug, and include your information in it.

4
David O'Meara On

The solution here is to use JPA 1.0 if 2.0 is not required.

0
VolatilitySmile On

Today i faced the same proble, but i corrected in persistence.xml

  1. Please check with entity class name and package are correctly defined in persistence.xml.