Hibernate Criteria: Joining Table Without a Mapped Association

Hibernate criteria: Joining table without a mapped association

My understanding is that if you do this using HQL, you are creating a Cartesian join with a filter, rather than an inner join. Criteria queries do not support doing this.

Hibernate Criteria - Joining on un-mapped entity

First of all, start with HQL. Criteria is useful when a query must be composed dynamically, based on a set of... criteria. Even if you absolutely want to use the criteria API, having the HQL query and translate it is easier.

OK, so you want the events that belong to a certain SomeEntity ordered by date and within some range:

select event from SomeEntity entity
join entity.events event
where entity.id = :entityId
and event.date between :startDate and :endDate
order by event.date

Now you can translate it to criteria. The problem is that the criteria API doesn't allow selecting any other entity than the root entity, and since you don't have an association going from SomeEntityEvent to SomeEntity, you're stuck.

My advice would thus be: do it in HQL, or make the association bidirectional. If it is bidirectional, you can write the HQL query like this:

select event from SomeEntityEvent event
join event.entities entity
where entity.id = :entityId
and event.date between :startDate and :endDate
order by event.date

And it can easily be translated to criteria:

Criteria c = session.createCriteria(SomeEntityEvent.class, "event");
c.createAlias("event.entities", "entity");
c.add(Restrictions.eq("entity.id", entityId);
c.add(Restrictions.between("event.date", startDate, endDate);
c.addOrder("event.date");

Note how every part of the HQL query directly translates to a similar criteria instruction.

Hibernate Criteria Projection without mapped association of tables

Yes, this is supported in Hibernate. The only thing here is that we have to use HQL:

  • 16.2. The from clause (cite:)

Multiple classes can appear, resulting in a cartesian product or "cross" join.

from Formula, Parameter
from Formula as form, Parameter as param

So in our case:

session
.createQuery("SELECT t1.prop1, t1.prop2, t1.prop3 "
+ " t2.prop4, t2.prop5 "
+ " FROM Entity1 AS t1, Entity2 As t2 " +
// if there is some relation - unmapped
+ " WHERE t1.someProperty = t2.someProperty "
+ " AND ... "
)
.setMaxResults(10) // we can even page here
.list()

NOTE: I used prop1, prop2 and Entity1, Entity2 ... to force the feeling that this is HQL. We are talking about mapped entities, not tables or columns

And we will recieve collection of object[] array...

Hibernate Criteria Query without Relationship Between Entities

String           dateIdValue   = "...";
Class<?> mainClass = A.class;
Class<?> dateClass = B.class;

String colDate1 = "date1";
String colDate2 = "date2";
String colDateId = "id";

Property date1Prop = Property.forName(colDate1);
Property date2Prop = Property.forName(colDate2);

DetachedCriteria date1Criteria = DetachedCriteria.forClass(dateClass)
.setProjection(date1Prop)
.add(Restrictions.eq(colDateId, dateIdValue));
DetachedCriteria date2Criteria = DetachedCriteria.forClass(dateClass)
.setProjection(date2Prop)
.add(Restrictions.eq(colDateId, dateIdValue));

Criteria criteria = getSession().createCriteria(mainClass)
.add(date1Prop.lt(date1Criteria))
.add(Restrictions.or(date2Prop.isNull(), date2Prop.le(date2Criteria)))
.addOrder(Order.desc("x"));

/*
SELECT A.*
FROM A, P
WHERE A.date1 < P.date1
AND (A.date2 is null or A.date2 <= P.date2 )
ORDER BY A.x DESC;
*/

Join two table using Criteria in Hibernate?

I don't think it is possible to join the same association twice with Criteria

You can find the related JIRA here that is still open.



Related Topics



Leave a reply



Submit