0
如何使用Criteria API選擇沒有父項的記錄與Hibernate?使用Criteria API在Hibernate中選擇沒有父項的記錄
這裏是選擇我的Java代碼與父母
getSessionFactory().getCurrentSession().createCriteria(Category.class).add(
Restrictions.eq("parent", new Category(parentId))).list();
類別Java代碼
@Entity
@Table(name = "CATEGORY")
public class Category implements NamedModel{
@Id
@Column(name = "CATEGORY_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@OneToOne(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
@JoinTable(name = "CATEGORY_RELATIONS",
joinColumns = {
@JoinColumn(name = "CATEGORY_RELATIONS_CATEGORY_ID", referencedColumnName = "CATEGORY_ID")},
inverseJoinColumns = {
@JoinColumn(name = "CATEGORY_RELATIONS_PARENT_ID", referencedColumnName = "CATEGORY_ID")})
private Category parent;
@OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER, mappedBy = "parent")
private List<Category> children;//...
}
CategoryRelations Java代碼
@Entity
@Table(name = "CATEGORY_RELATIONS")
@IdClass(CategoryRelations.CategoryRelationsPrimaryKey.class)
public class CategoryRelations implements Serializable {
@Id
@Column(name = "CATEGORY_RELATIONS_CATEGORY_ID")
private long categoryId;
@Id
@Column(name = "CATEGORY_RELATIONS_PARENT_ID")
private long parentId;
@Entity
@IdClass(CategoryRelationsPrimaryKey.class)
public static class CategoryRelationsPrimaryKey implements Serializable {
private long categoryId;
private long parentId;
}
}