提供者是休眠 3.6。
我有一个通过@IdClass 声明的复合键,由 4 列组成。通过完整的主键存储和检索实体实例时一切正常。但是,如果我希望查询仅在单个列中具有特定值的所有实体,hibernate 会抛出异常。
@Entity
@IdClass(MyBeanId.class)
public class MyBean {
@Id
private Integer partId1;
@Id
private Integer partId2;
@Id
private Integer partId3;
@Id
private String partId4;
private Integer otherProperty1;
private Double otherProperty2;
private Double otherProperty3;
private String otherProperty4;
/* removed getters and setters */
}
另一个类中的查询代码:
public List<MyBean> findByPartId1(Integer partId1) {
return em.createQuery("select b from MyBean b where b.partId1 = :partId1", MyBean.class).setParameter("partId1", partId1).getResultList();
}
另一个失败的查询,带有关于不知道参数类型的错误消息如下:
public List<Integer> getAllPartId1s() {
return em.createQuery("select distinct b.partId1 from MyBean b", Integer.class).getResultList();
}
我从第一个查询中得到的异常如下:
Caused by: org.hibernate.QueryException: could not resolve property: partId1 of: some.company.entity.MyBean [select t from some.company.entity.MyBean b where b.partId1 = :partId1]
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:75)
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1465)
at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:315)
at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:487)
at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:611)
at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:263)
at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:210)
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:880)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1330)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4471)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3944)
当然,有一些方法可以访问查询中复合键的各个列,而无需借助 @Embeddable id 对象?否则,为什么还要有@IdClass 机制,因为它几乎没用。我尝试了许多变体,例如“where b.id.partId1 = :partId1”,但它们都失败了。
是的,我有一个 MyBeanId 类,它具有相同的属性和一个有效的 hashCode 和 equals,但我只想清理这么多代码以便发布问题。