Hibernate HQL-将PK中的列包含到查询中
我使用SpringRoo对现有数据库进行反向工程,以便在现有数据库上进行hibernate建模,在存在复合主键的情况下,它会生成XyzClass和XyzClassPK 如果我想对组合主键的某些列执行HQL查询,我会得到一个错误,假设组合主键由primaryKey1和primaryKey2组成:Hibernate HQL-将PK中的列包含到查询中,hibernate,hql,spring-roo,Hibernate,Hql,Spring Roo,我使用SpringRoo对现有数据库进行反向工程,以便在现有数据库上进行hibernate建模,在存在复合主键的情况下,它会生成XyzClass和XyzClassPK 如果我想对组合主键的某些列执行HQL查询,我会得到一个错误,假设组合主键由primaryKey1和primaryKey2组成: String hql = "select o from XyxClass o where primaryKey1 = :key1 and nonPrimaryKey = :key2"; Typed
String hql = "select o from XyxClass o where primaryKey1 = :key1 and nonPrimaryKey = :key2";
TypedQuery<XyzClass> query = entityManager().createQuery(hql, XyzClass.class);
query.setParameter(......
query.setParameter(......
query.getResultList()
以及主键:
@RooIdentifier(dbManaged = true)
public final class XyzClassPK {
@Column(name = "PRIMARY_KEY1", nullable = false)
private Integer primaryKey1;
@Column(name = "PRIMARY_KEY2", nullable = false)
private Integer primaryKey2;
我的HQL显然不正确,如何在HQL查询中包含复合主键?您的HQL应该如下所示:
String hql = "select o from XyxClass o where o.xyz.primaryKey1 = :key1 and o.nonPrimaryKey = :key2";
其中,xyz是XyxClass中Id字段的名称(我在代码中看不到它…)
希望这有帮助 使用@Embeddeble注释(属性:其id和两个PK)为实体PK(例如EntityPK)创建一个类,然后在实体的类中添加一个属性private EntityPK EntityPK。在HQL中这样使用它:…从实体e开始,其中e.entityPk.pk1=:primaryKey1和e.attribute=:attribute我的id字段是“@EmbeddedId private XyzClassPK XyzClass.id;”,因此它将是o.id.primaryKey1。谢谢你的快速回答!
String hql = "select o from XyxClass o where o.xyz.primaryKey1 = :key1 and o.nonPrimaryKey = :key2";