我试图在Hibernate中执行一个hql查询来实现一些表之间的连接,但我得到了下面的例外。任何想法如何解决这个问题? 实际上我想以HQL样式实现以下查询:
select * from documentlink dl
left join digitalfile df on df.iddigitalfile = dl.ParentId and dl.entity = 261
where (dl.entity = 5 and dl.parentId = 39845) OR (df.entity = 5 and df.parentId = 39845);
DocumentLink.java:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "DigitalFileCategory")
private DigitalFileCategory digitalFileCategory;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "DocumentMetaData")
private DocumentMetaData documentMetaData;
@Column(name = "ParentId", insertable = false, updatable = false)
private Integer parentId;
@Any(metaColumn = @Column(name = "Entity"), fetch = FetchType.LAZY)
@AnyMetaDef(idType = "integer", metaType = "integer", metaValues = {
@MetaValue(value = "1", targetEntity = Raetsrelation.class), @MetaValue(value = "5", targetEntity = Claim.class),
@MetaValue(value = "6", targetEntity = Policy.class), @MetaValue(value = "7", targetEntity = Quotation.class),
@MetaValue(value = "8", targetEntity = Declaration.class), @MetaValue(value = "29", targetEntity = Claimsecurity.class),
@MetaValue(value = "30", targetEntity = Survey.class), @MetaValue(value = "33", targetEntity = Incidentlocation.class),
@MetaValue(value = "41", targetEntity = Endorsement.class), @MetaValue(value = "46", targetEntity = Invoice.class),
@MetaValue(value = "79", targetEntity = BlueCard.class), @MetaValue(value = "88", targetEntity = MajorClaimNotice.class),
@MetaValue(value = "91", targetEntity = DeficiencyEvent.class), @MetaValue(value = "97", targetEntity = RiskAssessment.class),
@MetaValue(value = "109", targetEntity = PolicyCancellation.class), @MetaValue(value = "158", targetEntity = Broker.class),
@MetaValue(value = "195", targetEntity = Supplier.class), @MetaValue(value = "196", targetEntity = Sla.class),
@MetaValue(value = "261", targetEntity = DigitalFile.class), @MetaValue(value = "268", targetEntity = DocumentEmail.class)
})
@JoinColumn(name = "ParentId")
private Object parentEntityObject;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Entity", insertable = false, updatable = false)
private Entity entity;
//so on
查询:
final TypedQuery<DocumentLink> query1 = entityManager
.createQuery("select distinct dl " +
"from DocumentLink as dl, DigitalFile as df " +
"where (df.iddigitalfile = 174694) and " +
"((dl.entity.identity = :entityId and dl.parentId = :parentId) or (df.entity.identity = :entityId and df.parentId = :parentId)) "
,DocumentLink.class);
query1.setParameter("entityId", baseInstance.getEntityId());
query1.setParameter("parentId", baseInstance.getId());
List<DocumentLink> documentLinks1 = query1.getResultList();
例外:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: An AnyType attribute cannot be join fetched [select distinct dl from com.raetsmarine.raetsbase3.domain.DocumentLink as dl join fetch dl.documentMetaData as dmd join fetch dmd.publishedBy join fetch dmd.documentLinkSet as dmddl join fetch dmd.documentType as dt left join fetch dl.parentEntityObject as peo with (peo.parentId=261) where (dl.entity.identity = 261 and dl.parentId= :parentId) or (dl.entity.identity = :entityId and dl.parentId= :parentId) ]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:284)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:131)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:93)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1836)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:342)
... 171 more