[英]Hibernate criteria search on Class(Table) with composite Key
我正在嘗試獲取 class 的所有數據庫條目,該表正在使用復合鍵。
這是我的映射 class:
@Entity("MyTable")
public class HBMyTable {
@Id
@Column(name = "CategoryID")
private String categoryId;
@Id
@Column(name = "StartpointID")
private String startpointId;
....
}
在我的 Dao 中,我想使用這種方法來讀取相應表中的所有條目:
public List<HBMyTable> getAllTableEntries(Session session) {
Criteria criteria = session.createCriteria(HBMyTable.class);
return criteria.list();
}
}
這將得到我的結果,但所有 startpointIds 都是 null。該表有 2045 行,所有這些都在列表中返回所有 CategoryIDs 都設置正確,但所有 StartpointIDs 都是 NULL
....
HBCategoryStartpoint [categoryId=Workflows, startpointId=null]
HBCategoryStartpoint [categoryId=Workflows, startpointId=null]
HBCategoryStartpoint [categoryId=Workflows, startpointId=null]
FOUND 2045 CATEGORY SP ENTRIES
我找到了解決方案,要查詢使用復合鍵的表,您首先必須使用 @Embeddable 注釋創建一個鍵 Class
@Embeddable
public class CategoryStartpointPK implements Serializable {
/**
*
*/
private static final long serialVersionUID = -1355843364236433447L;
private String categoryId;
private String startpointId;
public CategoryStartpointPK() {}
// getter, setter, toString
}
然后在映射表的實際 class 中使用 Keyclass 作為 Id
@Entity
@Table(name = "Category_StartPoint")
public class HBCategoryStartpoint implements Serializable {
/**
*
*/
private static final long serialVersionUID = -5880268624037227966L;
private CategoryStartpointPK categoryStartpointPK;
@Id
public CategoryStartpointPK getCategoryStartpointPK() {
return this.categoryStartpointPK;
}
我認為您需要對查詢添加限制:
criteria.add(Restrictions.isNotNull("StartpointID"));
而且我不知道你是否在 class name 之前加上注解@Entity
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.