HQL SELECT子句不起作用

时间:2015-03-30 13:36:37

标签: java sql hibernate hql criteria

我有一个名为CategorySet的实体。

@Entity
@Table(name = "CATEGORY_SET")
public class CategorySet {
    @Id
    @Column(name = "ID")
    @GeneratedValue
    private Integer id;
@OneToOne
@JoinColumn(name = "ID")
private Category parent;

@OneToOne
@JoinColumn(name = "ID")
private Category child;

public CategorySet(Category parent, Category child) {
    this.parent = parent;
    this.child = child;
}

public CategorySet() {
}
}

在我的DaoImpl中,我只从实体中获取子列。 但它正在返回空列表。

String innerQueryString = "select CS.child FROM CategorySet CS";
Query innerQuery = sessionFactory.getCurrentSession().createQuery(innerQueryString);
List list = innerQuery.list();

我也使用过标准。它抛出ArrayIndexOutOfBoundException

    Criteria  criteria  =sessionFactory.getCurrentSession().createCriteria(CategorySet.class);
    criteria.setProjection(Projections.property("child")); //Projections.property is used to retrieve specific columns

    List students = criteria.list();

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

由父母和孩子加入列修复它。

@Entity
@Table(name = "CATEGORY_SET")
public class CategorySet {
    @Id
    @Column(name = "ID")
    @GeneratedValue
    private Integer id;

    @OneToOne
    @JoinColumn(name = "PARENT")
    private Category parent;

    @OneToOne
    @JoinColumn(name = "CHILD")
    private Category child;

    public CategorySet(Category parent, Category child) {
        this.parent = parent;
        this.child = child;
    }

    public CategorySet() {
    }
}