Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate detached queries as a part of the criteria query

java experts can you please help me write detached queries as a part of the criteria query for the following SQL statement.

select A.*
FROM AETABLE A
where not exists
(
    select entryid
    FROM AETABLE B
    where B.classpk = A.classpk
    and B.userid = A.userid
    and B.modifiedDate > A.modifiedDate
)
and userid = 10146
like image 217
user199471 Avatar asked Oct 30 '09 07:10

user199471


People also ask

What is Hibernate detached criteria?

The detached criteria allows you to create the query without Session . Then you can execute the search in an arbitrary session. In fact you should think carefully when using a detached criteria using another, or a new, session (no cache, and creation of the session).

What is Hibernate Criteria query?

Hibernate provides alternate ways of manipulating objects and in turn data available in RDBMS tables. One of the methods is Criteria API, which allows you to build up a criteria query object programmatically where you can apply filtration rules and logical conditions.

Which method can be used for ordering the results using criteria queries?

The CriteriaQuery interface defines the orderBy method to order query results according to attributes of an entity.

How can we join multiple tables in Hibernate criteria?

Criteria in Hibernate can be used for join queries by joining multiple tables, useful methods for Hibernate criteria join are createAlias(), setFetchMode() and setProjection() Criteria in Hibernate API can be used for fetching results with conditions, useful methods are add() where we can add Restrictions.


2 Answers

You need to write a correlated subquery. Assuming property / class names match column / table names above:

DetachedCriteria subquery = DetachedCriteria.forClass(AETable.class, "b")
 .add(Property.forName("b.classpk").eqProperty("a.classpk"))
 .add(Property.forName("b.userid").eqProperty("a.userid"))
 .add(Property.forName("b.modifiedDate").gtProperty("a.modifiedDate"));

Criteria criteria = session.createCriteria(AETable.class, "a")
 .add(Property.forName("userid").eq(new Integer(10146)))
 .add(Subqueries.notExists(subquery);
like image 59
ChssPly76 Avatar answered Sep 23 '22 13:09

ChssPly76


Just one addition to the above query. If the entryid is not the primary key, then you'll need to add projection.


DetachedCriteria subquery = DetachedCriteria.forClass(AETable.class, "b")
 .add(Property.forName("b.classpk").eqProperty("a.classpk"))
 .add(Property.forName("b.userid").eqProperty("a.userid"))
 .add(Property.forName("b.modifiedDate").gtProperty("a.modifiedDate"))
 .add(setProjection(Projections.property("entryId"));  // Additional projection property

Criteria criteria = session.createCriteria(AETable.class, "a")
 .add(Property.forName("userid").eq(new Integer(10146)))
 .add(Subqueries.notExists(subquery);


like image 34
Moody Avatar answered Sep 22 '22 13:09

Moody