mrnfrancesco
mrnfrancesco

Reputation: 1026

Hibernate criteria equivalent for "not in" with join

I'm trying to do a "not in" query with Hibernate (4.3.6) criteria, but I do not want to use

// get session, create criteria, etc. ... Restrictions.not(Restrictions.in( ... )) ...

My entity situation is the following:

Report with its id and a list of AcceptedReport

AcceptedReport with the Report.id reference and idModerator

My query (in natural language) should be: "All the Report which are not yet accepted by the specified moderator"

How could I do this query using only joins?

Upvotes: 2

Views: 611

Answers (1)

barchiesi
barchiesi

Reputation: 32

You could try this, it should work in your exact situation:

return (List<Report>) sessionFactory.getCurrentSession()
.createCriteria(Report.class, "AllReport")
.createAlias("AllReport.acceptedReports", "AcceptedReport",
    //This is waht you were probably missing
    JoinType.LEFT_OUTER_JOIN,
    Restrictions.eq("AcceptedReport.idmoderator", idModerator) 
)
.add(Restrictions.isNull("AcceptedReport.idModerator"))
.list();

Upvotes: 1

Related Questions