当前位置: 首页 > 面试题库 >

Eclipselink忽略带有Lambda表达式的实体类

谢昊乾
2023-03-14
问题内容

我正在使用EclipseLink(2.5.1,也尝试过2.5.2-M1)构建一个Java SE 8(oracle
1.8.0-b129)应用程序,并且拥有一个Entity类,尽管已正确注释和删除,但EclipeLink只是忽略了它在persistence.xml文件中引用。日志中没有提及该类,没有为它生成任何模式,等等。使用该实体将给出“抽象模式类型未知”错误。

我想我终于找到了原因,并认为我会分享。显然,EclipseLink不喜欢带有lambda表达式的类。这是一个重现该问题的简单类:

@Entity
public class LambdaEntity {

    @Id
    private Integer id;

    public void theLambda() {
        Arrays.asList(1, 2, 3).stream().filter(m -> m == 2);
    }
}

lambda表达式甚至不必使用持久性属性,它在类中的简单存在就足够了。有谁知道是什么原因造成的?我猜想EclipeLink会阻塞生成的字节码,但我感到奇怪的是它默默地忽略了该类。

如果尝试将此实体与其他实体结合使用,则EclipseLink将给出错误消息,指出该实体未在persistence.xml文件中定义。部分堆栈跟踪:

Local Exception Stack: 
Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@3b9a45b3
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [hcm-test] failed.
Internal Exception: Exception [EclipseLink-7250] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException
Exception Description: [class com.senior.hcm.domain.organization.Workstation] uses a non-entity [class com.senior.hcm.domain.auth.UserRole] as target entity in the relationship attribute [field roles].
    at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:127)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:107)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [hcm-test] failed.
Internal Exception: Exception [EclipseLink-7250] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException
Exception Description: [class com.senior.hcm.domain.organization.Workstation] uses a non-entity [class com.senior.hcm.domain.auth.UserRole] as target entity in the relationship attribute [field roles].
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createPredeployFailedPersistenceException(EntityManagerSetupImpl.java:1954)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1945)
    at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPAInitializer.java:98)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:96)
    ... 28 more
Caused by: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [hcm-test] failed.
Internal Exception: Exception [EclipseLink-7250] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException
Exception Description: [class com.senior.hcm.domain.organization.Workstation] uses a non-entity [class com.senior.hcm.domain.auth.UserRole] as target entity in the relationship attribute [field roles].
    at org.eclipse.persistence.exceptions.EntityManagerSetupException.predeployFailed(EntityManagerSetupException.java:230)
    ... 32 more
Caused by: Exception [EclipseLink-7250] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException
Exception Description: [class com.senior.hcm.domain.organization.Workstation] uses a non-entity [class com.senior.hcm.domain.auth.UserRole] as target entity in the relationship attribute [field roles].
    at org.eclipse.persistence.exceptions.ValidationException.nonEntityTargetInRelationship(ValidationException.java:1378)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.RelationshipAccessor.getReferenceDescriptor(RelationshipAccessor.java:553)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.RelationshipAccessor.getOwningMapping(RelationshipAccessor.java:469)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ManyToManyAccessor.process(ManyToManyAccessor.java:149)
    at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processNonOwningRelationshipAccessors(MetadataProject.java:1566)
    at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage3(MetadataProject.java:1855)
    at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:580)
    at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:585)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1869)
    ... 30 more

问题答案:

正如Michael
Jess所建议的(谢谢:)),这是此问题的错误报告的链接。不幸的是,我还没有时间来测试已经完成的工作,但是看起来很有希望。它出现的时间应该不会更长。



 类似资料:
  • 我想使用方法按长度对数组中的字符串元素进行排序。我在这里被告知,可以使用lambda表达式而不是创建实现比较器的新类。用完全相同的方式做了,但它不起作用。

  • 问题内容: 如果lambda表达式不需要参数,因此名称过多,该如何写? 这种方式不能编译: 但这确实做到了: 有没有更好的办法? 问题答案: 既然您提到了这可行 我假设预期的功能接口方法必须接受一个参数。自Java8以来,该标识符是保留关键字。 我只会使用一个一次性的(有效标识符)字符。 甚至 明确地说。 在Java语言规范定义了一个lambda表达式的句法 和 换句话说,您不能省略标识符。 作为

  • 我在第42和43行出现错误:,未处理的异常类型InterruptedException。如果我尝试quickfix,它将创建带有catch异常的try catch,它将具有相同的错误,并将尝试以相同的方式修复它,继续用try catch包围它。

  • (译注:目前支持lambda的gcc编译器版本为4.5,其它详细的编译器对于C++11新特性的支持请参考http://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport) Lambda表达式是一种描述函数对象的机制,它的主要应用是描述某些具有简单行为的函数(译注:Lambda表达式也可以称为匿名函数,具有复杂行为的函数可以采用命名函数对象,当然,何谓复杂

  • Lambda表达式在Java 8中引入,并被吹捧为Java 8的最大特性.Lambda表达式有助于函数式编程,并简化了很多开发。 语法 (Syntax) lambda表达式的特征在于以下语法。 parameter -> expression body 以下是lambda表达式的重要特征。 Optional type declaration - 无需声明参数类型。 编译器可以从参数的值推断出相同的

  • 问题内容: 我想查找包含字符串的行,如下所示: 但是,这失败了,因为某些元素是NaN: ValueError:无法使用包含NA / NaN值的向量建立索引 所以我诉诸于混乱 有没有更好的办法? 问题答案: 有一个标志: 参见文档: na:默认NaN,填充缺失值的值。 因此,您可以执行以下操作: