即使实体使用 @Entity 注释进行标记,仍会出现未知的实体类错误消息
我正在使用 Netbean6.9.1 和 JPA EclipseLink 构建 REST Web 应用程序。
我面临的问题是,即使我的实体类 MasatoTable 标记有实体注释,我也会收到错误:
(java.lang.IllegalArgumentException: Unknown entity bean class:
class entity.MasatoTable, please verify that this class
has been marked with the @Entity annotation.)
问题是,当我从 NetbeanIDE 重新启动 GlassFish3 服务器时,它会工作一段时间,并且在某些时候,错误开始出现。 我曾经使用 Toplink Essential,没有任何问题,但我已更改为 EclipseLink 并重新定义 persistence.xml(如下所示),并且开始出现此问题。但我没有看到任何我能想到的代码错误..
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>
看起来是同样的问题,但该票证的解决方案是从Eclipse 链接。有人在不回滚到toplink的情况下解决了这个问题吗?
未知实体 bean 类热部署后:netbeans 6.9 + glassfish 2.1 + eclipselink jpa 2.0
更新
就我在这种情况下的研究而言,看起来该错误已经得到解决和修复?
https://bugs.eclipse.org/bugs/show_bug.cgi?id= 288141
我的知识不足以理解整个事情,但我发现了一些事实:
Netbean6.9.1提供了EclipseLink 2.0.1,而最新版本是2.2.0
我下载了最新版本并将它们放入lib目录中然后重新部署但没有运气,问题仍然存在。不确定我是否以错误的方式做事。
詹姆斯评论后更新
下面是我用来创建单例 EntityManagerFactory 的类及其使用方式。
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
如何使用:
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();
}
}
通过查看我的代码,我认为... 我没有关闭 EntityManagerFactory (不过我最终还是关闭了 EntityManager)
我也发现通过以下操作,主题中的“未知实体 bean 类”
错误肯定会出现。
通过 Netbean 部署 Web 应用程序(这将打开浏览器)
在 Netbean IDE 中更改一些代码并保存。然后,只要按下保存按钮,Netbean 就会自动重新部署(这就是所谓的“热部署”???)
返回浏览器并执行一些操作,例如添加一些涉及 EntityManager 的数据。
看起来发生错误是因为我在热部署时没有关闭实体管理器工厂?我必须调查一下。
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.
Deploy web app through Netbean (This will open the browser)
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" ???)
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您如何创建 EntityManager/Factory,它是托管的还是非托管的?
如果它不受管理,那么我相信 EclipseLink 不会以任何重新部署的方式收到通知,因此仍然会缓存旧的 EntityManagerFactory,因为它永远不会关闭。能否确保热部署后关闭EntityManagerFactory?
您可以将持久性单元设置为托管持久性单元,这可能会解决该问题。
无论哪种方式,请在 EclipseLink 和 Glassfish 中记录此错误的详细信息,因为应该跟踪此问题。
我认为这可能是错误, https://bugs.eclipse.org/bugs /show_bug.cgi?id=326552
请为此错误投票,并在其中包含您的信息。
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.
解决方案此处< /a> 是如果不需要 2.0,则使用 JPA 1.0。
The solution here is to use JPA 1.0 if 2.0 is not required.
今天我遇到了同样的问题,但我在 persistence.xml 中更正了
Today i faced the same proble, but i corrected in persistence.xml