无法使用HQL访问JPA加入的Entity

时间:2017-11-08 14:58:49

标签: hibernate jpa join entity hql

我有2个ORM类,一个名为OpExAlloc,另一个名为VesselManComp。

第一个与@ManyToOne关系,第二个关系不在其主键上,而是在自定义列上。

这是OpExAlloc类中的代码

@Fetch(FetchMode.JOIN)
    @JoinColumns({
            @JoinColumn(name = "companyId", referencedColumnName = "companyId",  insertable = false, updatable = false),
            @JoinColumn(name = "shipCode", referencedColumnName = "code", insertable = false, updatable = false) })
    @ManyToOne(fetch = FetchType.EAGER)
    private VesselManComp vesselManComp;

    public VesselManComp getVesselManComp() {
        return this.vesselManComp;
    }
    public void setVesselManComp(VesselManComp vesselManComp) {
        this.vesselManComp = vesselManComp;
    }

这是HQL(以前在旧版本的Hibernate和XML配置中工作而不是注释)

    String qs = " select ca.id.docId, ca.id.itemId, ca.id.shipCode, ca.account, "
            + " sum(isnull(ca.quantity, 0)) as quantity, "
            + " sum(isnull(ca.directAmtLocal,0) + isnull(ca.indirectAmtLocal,0) + isnull(ca.nationalTaxLocal,0)) as amountLocal, "
            + " sum(ca.directAmtUsd + ca.indirectAmtUsd + isnull(ca.nationalTaxUsd, 0)) as amountUSD, "
            + " min(ca.id.allocDate) as minDate, "
            + " max(ca.id.allocDate) as maxDate "
            + " from OpExAlloc"
            + " as ca "
            + " where ca.id.companyId = :companyId "
            + " and ca.id.ssId = :sourceSystemId "
            + " and ca.id.type in (:documentTypes) "
            + " and (ca.opexTypeId is null or ca.opexTypeId not in (14, 15)) "
            + " and ca.vesselManComp.code is not null ";

我得到的错误是

java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: code of: com.companyname.orm.sqlserver.aether.dbo.OpExAlloc [ select ca.id.docId, ca.id.itemId, ca.id.shipCode, ca.account,  sum(isnull(ca.quantity, 0)) as quantity,  sum(isnull(ca.directAmtLocal,0) + isnull(ca.indirectAmtLocal,0) + isnull(ca.nationalTaxLocal,0)) as amountLocal,  sum(ca.directAmtUsd + ca.indirectAmtUsd + isnull(ca.nationalTaxUsd, 0)) as amountUSD,  min(ca.id.allocDate) as minDate,  max(ca.id.allocDate) as maxDate  from com.companyname.orm.sqlserver.aether.dbo.OpExAlloc as ca  where ca.id.companyId = :companyId  and ca.id.ssId = :sourceSystemId  and ca.id.type in (:documentTypes)  and (ca.opexTypeId is null or ca.opexTypeId not in (14, 15))  and ca.vesselManComp.code is not null  and ca.id.allocDate >= :allocationDateFrom  and ca.id.allocDate < :allocationDateTo  group by ca.id.docId, ca.id.itemId, ca.id.shipCode, ca.account ]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164)

并且奇怪的事实是Hibernate在OpExAlloc对象中寻找“code”属性而不是VesselManComp对象。

如果我更改查询以在嵌套对象上添加连接提取

    String qs = " select ca.id.docId, ca.id.itemId, ca.id.shipCode, ca.account, "
            + " sum(isnull(ca.quantity, 0)) as quantity, "
            + " sum(isnull(ca.directAmtLocal,0) + isnull(ca.indirectAmtLocal,0) + isnull(ca.nationalTaxLocal,0)) as amountLocal, "
            + " sum(ca.directAmtUsd + ca.indirectAmtUsd + isnull(ca.nationalTaxUsd, 0)) as amountUSD, "
            + " min(ca.id.allocDate) as minDate, "
            + " max(ca.id.allocDate) as maxDate "
            + " from OpExAlloc"
            + " as ca "
            + " join fetch ca.vesselManComp vmc "
            + " where ca.id.companyId = :companyId "
            + " and ca.id.ssId = :sourceSystemId "
            + " and ca.id.type in (:documentTypes) "
            + " and (ca.opexTypeId is null or ca.opexTypeId not in (14, 15)) "
            + " and vmc.code is not null ";

我得到一个空指针异常

java.lang.NullPointerException
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:424)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3921)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3707)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3585)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:576)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:266)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141)

有人可以解释一下我做错了什么吗? 这是我们项目的旧项目迁移,使用Hibernate 3和类配置的XML配置,查询工作得很好。

新的使用hibernate 5.2和类的注释配置。

最诚挚的问候,谢谢你, 乔治

1 个答案:

答案 0 :(得分:0)

您的@ManyToOne注释可能会被完全忽略。我注意到注释位于场上。特定类层次结构(OpExAlloc类)中使用的访问类型是什么?你能发全班吗?默认情况下,类层次结构的访问类型由@Id或@EmbeddedId注释的位置定义。如果这些注释在getter上,那么@ManyToOne注释也应该在getter上。有一个相关的帖子 Where to put hibernate annotations?