EJB抽象实体导致java.lang.NoClassDefFoundError?

发布于 2024-12-04 14:07:11 字数 7698 浏览 1 评论 0原文

在我的 ejb 项目中,我定义了一个名为 DataObjectEntity.java 的抽象实体类,并且让所有实体都扩展该类。目的是可以重用公共字段并且更容易地记录系统中的活动。

@Entity
@Inheritance(strategy= InheritanceType.TABLE_PER_CLASS)
public abstract class DataObjectEntity implements Serializable {
  private static final long serialVersionUID = 1L;
  @Transient
  protected Logger log = Logger.getLogger(getClass());

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  private PersonEntity createdBy;
  @Temporal(javax.persistence.TemporalType.DATE)
  private Date createdAt;
  private PersonEntity lastModifiedBy;
  @Temporal(javax.persistence.TemporalType.DATE)
  private Date lastModifiedAt;
  private boolean archived;

  public DataObjectEntity() {
    createdAt = new Date();
    lastModifiedAt = new Date();
    archived = false;
  }

  public DataObjectEntity(PersonEntity createdBy) {
    this();
    this.createdBy = createdBy;
    this.lastModifiedBy = createdBy;       
  }

  public void modified(PersonEntity modifiedBy){
    this.lastModifiedBy = modifiedBy;
    this.lastModifiedAt = new Date();
    log.info("Object " + id + " modified by " + modifiedBy.getName());
  }

  public void created(PersonEntity createdBy){
    this.createdBy = createdBy;
    this.lastModifiedBy = createdBy;
    log.info("Object " + id + " created by " + createdBy.getName());
  }

  //getters and setters
}

然后我使用 netbeans 的内置功能从实体类生成 JSF 页面。一切都很好。我可以很好地运行该项目。

但是添加新实体后,我在尝试部署时收到了超类实体的 NoClassDefFoundError 错误。新实体非常简单,具有使用预先存在的实体类之一定义的 1-1 关系。我尝试了我能想到的一切:清理和构建、重新启动 glassfish、删除所有表、为新实体类生成 JSF(尽管我不需要它)...但没有任何效果。

最终我删除了新实体,只在现有的支持 bean 上留下了一些更改。并且错误仍然存​​在。我什至检查了一个新的工作副本,以将相同的更改应用于支持 bean - 结果发现该副本部署得很好。我的一位队友在尝试添加新实体时遇到了完全相同的错误。知道可能是什么原因吗?

WARNING: entity/DataObjectEntity_
java.lang.NoClassDefFoundError: entity/DataObjectEntity_
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at org.glassfish.web.loader.WebappClassLoader.findClass(WebappClassLoader.java:927)
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1486)
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1369)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at org.eclipse.persistence.internal.helper.ConversionManager.convertObjectToClass(ConversionManager.java:438)
at org.eclipse.persistence.internal.helper.ConversionManager.convertObject(ConversionManager.java:141)
at org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform.convertObject(DatasourcePlatform.java:160)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.initializeCanonicalMetamodel(EntityManagerSetupImpl.java:2552)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.getMetamodel(EntityManagerSetupImpl.java:2531)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:484)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:188)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:277)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:290)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:268)
at org.glassfish.persistence.jpa.PersistenceUnitLoader.doJava2DB(PersistenceUnitLoader.java:373)
at org.glassfish.persistence.jpa.JPADeployer$2.visitPUD(JPADeployer.java:435)
at org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:483)
at org.glassfish.persistence.jpa.JPADeployer.iterateInitializedPUsAtApplicationPrepare(JPADeployer.java:465)
at org.glassfish.persistence.jpa.JPADeployer.event(JPADeployer.java:386)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:128)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:453)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:382)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:355)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:370)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1064)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:96)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1244)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1232)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:459)
at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:209)
at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:168)
at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:238)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.ClassNotFoundException: entity.DataObjectEntity_
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1519)
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1369)
... 53 more

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="MyProject-warPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/myprojectdb</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    </properties>
  </persistence-unit>
</persistence>

更新: 我的队友发现了一种非常奇怪但很有前途的方法来解决此问题:将报告 NoClassDefFoundError 的实体 java 文件重命名为 .class。等待glassfish编译并报错。然后将其更改回 .java。等待 glassfish 再次编译,然后清理并构建、部署。每次都有效。

In my ejb project I defined an abstract entity class called DataObjectEntity.java, and have all entities extend this class. The purpose is such that common fields can be reused and logging of activities in the system can be easier.

@Entity
@Inheritance(strategy= InheritanceType.TABLE_PER_CLASS)
public abstract class DataObjectEntity implements Serializable {
  private static final long serialVersionUID = 1L;
  @Transient
  protected Logger log = Logger.getLogger(getClass());

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  private PersonEntity createdBy;
  @Temporal(javax.persistence.TemporalType.DATE)
  private Date createdAt;
  private PersonEntity lastModifiedBy;
  @Temporal(javax.persistence.TemporalType.DATE)
  private Date lastModifiedAt;
  private boolean archived;

  public DataObjectEntity() {
    createdAt = new Date();
    lastModifiedAt = new Date();
    archived = false;
  }

  public DataObjectEntity(PersonEntity createdBy) {
    this();
    this.createdBy = createdBy;
    this.lastModifiedBy = createdBy;       
  }

  public void modified(PersonEntity modifiedBy){
    this.lastModifiedBy = modifiedBy;
    this.lastModifiedAt = new Date();
    log.info("Object " + id + " modified by " + modifiedBy.getName());
  }

  public void created(PersonEntity createdBy){
    this.createdBy = createdBy;
    this.lastModifiedBy = createdBy;
    log.info("Object " + id + " created by " + createdBy.getName());
  }

  //getters and setters
}

Then I used netbeans' built-in feature to generate JSF pages from entity classes. Everything was fine. I could run the project just fine.

But after adding a new entity, I got this NoClassDefFoundError for the super class entity when trying to deploy. The new entity was really simple with a 1-1 relationship defined with one of the pre-existing entity class. I tried everything I could think of: clean&build, restarting glassfish, dropping all tables, generating JSF for the new entity class though I don't need it... But nothing worked.

Eventually I deleted my new entity and only left some changes on the existing backing beans. And the error still persists. I even checked out a fresh working copy to apply the same changes to the backing beans - turned out that copy deploys fine. One of my teammates is getting exactly the same error when he tries to add a new entity. Any idea what might be the cause?

WARNING: entity/DataObjectEntity_
java.lang.NoClassDefFoundError: entity/DataObjectEntity_
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at org.glassfish.web.loader.WebappClassLoader.findClass(WebappClassLoader.java:927)
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1486)
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1369)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at org.eclipse.persistence.internal.helper.ConversionManager.convertObjectToClass(ConversionManager.java:438)
at org.eclipse.persistence.internal.helper.ConversionManager.convertObject(ConversionManager.java:141)
at org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform.convertObject(DatasourcePlatform.java:160)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.initializeCanonicalMetamodel(EntityManagerSetupImpl.java:2552)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.getMetamodel(EntityManagerSetupImpl.java:2531)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:484)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:188)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:277)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:290)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:268)
at org.glassfish.persistence.jpa.PersistenceUnitLoader.doJava2DB(PersistenceUnitLoader.java:373)
at org.glassfish.persistence.jpa.JPADeployer$2.visitPUD(JPADeployer.java:435)
at org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:483)
at org.glassfish.persistence.jpa.JPADeployer.iterateInitializedPUsAtApplicationPrepare(JPADeployer.java:465)
at org.glassfish.persistence.jpa.JPADeployer.event(JPADeployer.java:386)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:128)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:453)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:382)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:355)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:370)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1064)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:96)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1244)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1232)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:459)
at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:209)
at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:168)
at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:238)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.ClassNotFoundException: entity.DataObjectEntity_
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1519)
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1369)
... 53 more

The persistence.xml file:

<?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="MyProject-warPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/myprojectdb</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    </properties>
  </persistence-unit>
</persistence>

Update: My teammate found a very strange but promising way to fix this: rename the entity java file that reports the NoClassDefFoundError to .class. Wait for glassfish to compile and report errors. Then change it back to .java. Wait for glassfish to compile again then clean and build, deploy. Works every time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻梦旅人 2024-12-11 14:07:11

例外情况是类实体/DataObjectEntity_ 不是 DataObjectEntity,请注意“_”。

这是一个 JPA2 元模型类,仅用于 Criteria API 的类型安全版本。这不是必需的,也不应该引起任何问题。您的应用程序仍然可以成功运行吗?

这些模型类是可选的,通常不需要,因此缺少它们不会导致任何问题。它们仅在编译时由 IDE 或必须连接到构建过程的 EclipseLinkk 元模型生成器生成。

您进行更改来解决问题是没有意义的,看起来您的 IDE 可能刚刚遇到了一些编译问题。

The exception say the class entity/DataObjectEntity_ not DataObjectEntity, note the "_".

This is a JPA2 metamodel class that is only used for the type safe version of the Criteria API. It is not required, and should not cause any issues. Does you application still run successfully?

These model classes are optional and normally not needed, so missing them should not be causing any issue. They are generated only at compile time, either by your IDE, or by the EclipseLinkk meta-model generator that has to be hooked up to your build process.

You change to resolve the issue make no sense, seems like you may have just had some compile issue with your IDE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文