【问题标题】:JPQL inconsitency between hibernate and eclipselinkhibernate和eclipselink之间的JPQL不一致
【发布时间】:2011-05-13 14:04:21
【问题描述】:

我有以下查询在 Hibernate 下有效,但在 eclipse 下无效:

select o from Organisation o where o.code=:code and o.type=:type

我不清楚为什么会这样,我希望其他人可以为我们详细说明。返回的错误是:

Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Error compiling the query [select o from Organisation o where  o.code=:code and o.type=:type]. Unknown entity type [Organisation].

除了在 persistence.xml 文件中切换提供程序类以指示我们现在使用 eclipselink 之外,我们没有进行其他更改。

【问题讨论】:

    标签: hibernate orm jpa eclipselink jpql


    【解决方案1】:

    看起来 EclipseLink 没有扫描您的 JAR 文件/类中包含组织类的部分。在 persistence.xml 中显式声明它,看看会发生什么:

    <persistence 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"
                 version="2.0">
    <persistence-unit name="YourPersUnit">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    
        <class>foo.bar.Organisation</class>
    </persistence-unit>
    </persistence>
    

    【讨论】:

    • 我们有多个 jar 文件,其中包含不同的可持久对象集。事实证明hibernate处理这种情况与eclipse不同,最简单的方法是打开&lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
    • 我相信 Hibernate 默认会在类路径中添加带注释的 @Entity 类,除非您告诉它不要这样做。这可能是它以前起作用的原因。 Ninja 的答案是正确的,但另一种方法可能是为 EclipseLink 找到一个等效属性,告诉它在运行时查找所有 @Entity 类。我假设有一个。
    • 请注意,exclude-unlisted-classes 不适合在 J2SE 环境中使用(根据规范),尽管某些实现确实在那里使用了它……这可能是您看到不同行为的原因未包含在规范中。你没有提到你正在使用哪个环境
    猜你喜欢
    • 1970-01-01
    • 2012-11-17
    • 2018-09-02
    • 1970-01-01
    • 2011-03-30
    • 2017-01-19
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多