这个HQL查询有什么问题?

时间:2010-05-17 13:37:04

标签: java hibernate hql

我遇到了一个hibernate错误,或者我有一个错误,我没有看到:

select enty.number from EntityAliasName enty
  where enty.myId in
  (
    select cons.myId from Consens cons where cons.number in
    (
      select ord.number from Orders ord where ord.customer = :customer 
      and ord.creationDate <
      (
        select max(ord.creationDate) from Orders ord where ord.customer = :customer
      )
    )
  )

我得到的是以下内容:

org.hibernate.util.StringHelper.root(StringHelper.java:257)

Caused by: java.lang.NullPointerException
        at org.hibernate.util.StringHelper.root(StringHelper.java:257)
        at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1391)
        at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:54)
        at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1367)
        at org.hibernate.hql.ast.tree.FromElement.getIdentityColumn(FromElement.java:320)
        at org.hibernate.hql.ast.tree.IdentNode.resolveAsAlias(IdentNode.java:154)
        at org.hibernate.hql.ast.tree.IdentNode.resolve(IdentNode.java:100)
        at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:117)
        at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:113)
        at org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:854)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.propertyRef(HqlSqlBaseWalker.java:1172)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.propertyRefLhs(HqlSqlBaseWalker.java:5167)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.propertyRef(HqlSqlBaseWalker.java:1133)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:1993)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1932)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1476)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:580)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:288)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:231)
        at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
        at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
        at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
        at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
        at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
        at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
        at org.hibernate.impl.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:484)
        at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:394)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)

使用:Hibernate 3.3.2.GA / postgresql


更新

我试图通过尝试为自己的子句执行每个子句来解决错误。我想通过执行:

我得到了同样的例外
select enty.number from EntityAliasName enty

但如果我执行以下操作,则可以:

select number from EntityAliasName enty

现在问题是,为什么?

也许我应该提到EntityAliasName是通过以下方式定义的实体别名:

<class name="package.EntityName"
         table="entities"
        entity-name="EntityAliasName"
        mutable="false"> ... </class>

更新2:

我找到了解决问题的方法。我的POJO以这种方式映射:

<class name="package.EntityName"
             table="entities"
            entity-name="EntityAliasName"
            mutable="false">

  <composite-id>
    <key-property name="val1" column="val1" type="long"/>
    <key-property name="val2" column="val2" type="integer"/>
  </composite-id>

  <property name="id" column="entity_id" type="string" length="255" not-null="true"/>

  ...

</class>

拥有复合ID并使用HHH-1851报告的名为idAnthony Patricio的属性时出现问题。将属性名称从id更改为entityId解决了以下问题:

感谢mdma让我指向了正确的方向。

2 个答案:

答案 0 :(得分:5)

我找到了解决问题的方法。我的POJO以这种方式映射:

<class name="package.EntityName"
             table="entities"
            entity-name="EntityAliasName"
            mutable="false">

  <composite-id>
    <key-property name="val1" column="val1" type="long"/>
    <key-property name="val2" column="val2" type="integer"/>
  </composite-id>

  <property name="id" column="entity_id" type="string" length="255" not-null="true"/>

  ...

</class>

拥有复合ID并使用HHH-1851报告的名为idAnthony Patricio的属性时出现问题。将属性名称从id更改为entityId解决了以下问题:

<property name="entityId" column="entity_id" 
type="string" length="255" not-null="true"/>

感谢mdma让我指向了正确的方向。

刚刚从我上面的更新中复制了答案,然后将其关闭并保持接受率

答案 1 :(得分:0)

我得到null创建我的entityManager 1-2个小时,不明白为什么,这篇文章刚刚解决了我的问题。

我在一个班级(Java):

@GeneratedValue
private Long id;
public Long getId() {
    return id;
}

我刚刚阅读了您的帖子并将其更改为:

 @JoinColumn(name = "id")
    @GeneratedValue
    private Long ide;
    public Long getId() {
        return ide;
    }

最后是一个很好的汇编。