Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用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;//...
}

类别关系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;
    }
}

共 (0) 个答案