如何查询与JPA的这种一对一关系

时间:2014-06-05 23:57:57

标签: java hibernate jpa

我有下一个ER结构:

enter image description here

我的实体是这样的:

Event.java

public class Event implements Serializable {

.. many attributes here

@OneToOne(mappedBy = "evento",cascade = CascadeType.ALL,orphanRemoval = true)
     private Quiz quiz;

many getters & setters here ...

}

Quiz.java

public class Quiz implements Serializable {

    @JoinColumn(name = "evento",referencedColumnName = "id")
    @OneToOne
    private Evento evento;
}

我想在没有测验的情况下获取每个事件,所以我的查询是这样的:

Query q = em.createQuery("SELECT e FROM Evento AS e WHERE e.encuesta IS NULL",Evento.class);

但我得到一个空列表

我正在使用hibernate进行持久化。

有人可以帮助我:)?

1 个答案:

答案 0 :(得分:0)

尝试IS EMPTY子句:

Query q = em.createQuery("SELECT e FROM Evento AS e WHERE e.encuesta IS EMPTY",Evento.class);

见:

HQL Query to check if size of collection is 0 or empty