Sleep criteria using property not in (subquery)

I want to execute a query something like

Select id, name from information where name not in (select firstname from contact where id = 1) Information Id Name 1 Test Contact id firstname 1 name 2 Test 

If I use the neProperty () function, it will return the entries as name != Test.

How can I use the sleep criteria?

thanks

+3
subquery hibernate hibernate-criteria
source share
2 answers

Create selection criteria:

 Criteria cr = session.createCriteria(Your.class); List list = cr.list(); 

Then you can add a constraint to it, that is, where the column is 1 = 8, etc., for example:

 cr.add(Restrictions.eq("YourCondition", YourCondition)); 

Finally, you may not provide the proposal as follows:

 cr.add(Restrictions.not(Restrictions.in("YourNotInCondition", YourNotInCondition))); 
+4
source share

You can use DetachedCriteria to create a subquery.

 // This corresponds to (select firstname from contact where id = 1) DetachedCriteria subquery = DetachedCriteria.forClass(Contact.class) .add(Restrictions.eq("id", 1)) .setProjection(Projections.property("name")) // This corresponds to (select information where name not in (subquery)) ICriteria criteria = session .createCriteria(Information.class) .add(Subqueries.notIn("name", subquery)); 

Instead of using a subquery, you can simply load the contact using session.get, with the possibility of getting into the cache:

 Contact contact = session.Get<Contact>(1); ICriteria criteria = session .createCriteria(Information.class) .add(Property.ne("name", contact.getName())); 

Disclamer: I'm a) not a Java programmer and b) may have made mistakes, so it probably won't compile. The code more crudely demonstrates the idea and, hopefully, will be useful in any case.

+10
source share

All Articles