如何在Hibernate中选择没有子节点的父对象?

时间:2016-05-25 22:05:21

标签: java hibernate one-to-many

我有以下实体,父和子,父对象可能没有子对象。如何选择在休眠状态下没有孩子的父母?它类似于SQL中的not existing子句。这是我的实体:

@Entity
@Table(name = "PARENT")
public class Parent implements Serializable {

    @Id
    @Column(name="PARENT_ID")
    private Long parentId;

    @Column(name="NAME")
    private String name;

    @OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
    Set<Child> children;
}
@Entity
@Table(name = "CHILD")
public class Child implements Serializable {

    @Id
    @Column(name="CHILD_ID")
    private Long childId;

    @Column(name="NAME")
    private String name;

    @ManyToOne(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    @JoinColumn(name="PARENT_ID", nullable=false, updatable=false)
    private Parent parent;
}

我的查询:

public List<Parent> getParentsNotHavingChildren() {
    Session session = this.sessionFactory.getCurrentSession();
    Transaction txn = session.beginTransaction();
    List<Parent> result = null;
    Criteria criteria = session.createCriteria(Parent.class)
            .add(Restrictions.eq("children", null));
    result = criteria.list();
    txn.commit();
    if (session.isOpen()) {
        session.close();
    }
    log.info(String.format("Get %s parents", result.size()));
    return result;
}

然后我出错了

org.hibernate.exception.GenericJDBCException: could not execute query
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2545)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
    at org.hibernate.loader.Loader.list(Loader.java:2271)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716)
    at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
    at HibernateParentDao.getParentsNotHavingChildren(HibernateParentDao.java:42)
Caused by: java.sql.SQLException: Missing IN or OUT parameter at index:: 1
    at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
    at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:110)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:171)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:227)
    at oracle.jdbc.driver.OraclePreparedStatement.processCompletedBindRow(OraclePreparedStatement.java:1737)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3376)
    at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3425)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1202)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1953)
    at org.hibernate.loader.Loader.doQuery(Loader.java:802)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
    at org.hibernate.loader.Loader.doList(Loader.java:2542)

我确定我的标准不正确,但我找不到这种查询的正确方法。任何帮助将非常感谢!

2 个答案:

答案 0 :(得分:3)

要将属性测试为null,请使用isNull ...更改

Criteria criteria = session.createCriteria(Parent.class)
        .add(Restrictions.eq("children", null));

Criteria criteria = session.createCriteria(Parent.class)
        .add(Restrictions.isNull("children"));

否则,Hibernate将期望一个非null值来执行eq

答案 1 :(得分:0)

这是选择没有孩子的父母的简单标准(对于一对多的关系,您应该使用isEmpty)。

Criteria criteria = currentSession().createCriteria(Parent.class);
criteria.add(Restrictions.isEmpty("children"));
return criteria.list();

顺便说一句,对于简单的“选择”查询,您无需创建transaction。只要上面三行就足够了。

另一个答案可能有效,但仅在一对一关系中有效。 希望它能起作用!