當前我正在嘗試使用hibernate(版本4.3.Final)檢索包含另一個hibernate bean關聯的組合主鍵的對象。我使用的標準如下:Hibernate複合鍵嵌套對象標準從條款中缺失
session.createCriteria(IdentityIdentifierHibernateBean.class)
.setFetchMode("key.type", FetchMode.JOIN);
.createAlias("key.type","typeAlias",JoinType.INNER_JOIN);
.add(Restrictions.and(
Restrictions.eq("key.value", "value"),
Restrictions.eq("typeAlias.id", "id value")))
.list();
當運行此我得到的誤差:
1. SQL Error: 0, SQLState: 42P01
2. missing FROM-clause entry for table "typealias1_"
其原因是顯而易見的,當我查看生成的SQL,如下所示:
select
this_.type as type4_4_0_,
this_.value as value1_4_0_,
this_.id as id2_4_0_, this_.scope as scope3_4_0_
from
identityIdentifier this_
where
(this_.value=? and typealias1_.id=?)
當運行createAlias(或createCritera)不休眠假設生成一個連接語句?我已經嘗試了這兩種方法,並嘗試爲複合主鍵創建別名。無論哪種方式,這些方法都不起作用,因爲連接語句從不創建。這是一個解決嵌入式複合主鍵引用的嵌套hibernate bean的錯誤嗎?還是我失去了一些東西....
僅供參考這裏的Hibernate類的簡化版本(的hashCode,equals和未列入制定者):
@Entity
@Table(name = "identityIdentifier")
public class IdentityIdentifierHibernateBean implements Serializable {
private IdentityIdentifierPrimaryKey key;
@EmbeddedId
public IdentityIdentifierPrimaryKey getKey() {
return key;
}
}
@Embeddable
public class IdentityIdentifierPrimaryKey implements Serializable {
private String value;
private IdentityIdentifierTypeHibernateBean type;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "type", referencedColumnName="id", unique = true, nullable = false)
public IdentityIdentifierTypeHibernateBean getType() {
return type;
}
@Column(name = "value", unique = false, nullable = false, length = 255)
public String getValue() {
return value;
}
}
@Entity
@Table(name = "identityIdentifierType")
public class IdentityIdentifierTypeHibernateBean implements Serializable {
private String id;
@Id
@Column(name = "id", unique = true, nullable = false, length=38)
public String getId() {
return id;
}
}