实体
@javax.persistence.Entity
@javax.persistence.Table(name = "entidade")
public class Entidade {
private static final long serialVersionUID = -6831078183847196839L;
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "id_titulo", column = @Column(name = "titulo_id", nullable = false)),
@AttributeOverride(name = "id_empresa", column = @Column(name = "empresa_id", nullable = false)) })
private PK pk;
//getter//setter
}
//PK
@Embeddable
public class PK implements Serializable {
private static final long serialVersionUID = -5441836698300495848L;
@javax.persistence.Column(name = "id_titulo")
private Long titulo_id;
@javax.persistence.Column(name = "id_empresa")
private Long empresa_id;
//getter // setter
}
查询正常:如果实例 pk setter
Criteria criteria = novoCriteria();
criteria.createAlias("id", "id");
TituloPK pk = new TituloPK();
pk.setEmpresa(2L);
pk.setTitulo(6364L);
criteria.add(Restrictions.eq("id", pk));
criteria.list();
查询错误
Criteria criteria = novoCriteria();
criteria.createAlias("id", "id");
criteria.add(Restrictions.eq("id.id_empresa", 2L));
criteria.list();
控制台错误: *无法解析属性:id_empresa of:Entidade*
只需要按公司搜索,如果我使用约束返回上述错误。