当前我正在尝试使用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;
}
}