4

I am having trouble converting MySQL Query into appropriate Hibernate code. Specifically the following SQL

select a.id, b.id as id, b.timestamp as timestamp, b.speed as speed from abc a join xyz b on b.abc_id = a.id left xyz b2 on (a.id = b2.abc_id and b.timestamp < b2.timestamp OR b.timestamp = b2.timestamp AND b.id < b2.id))

Above query is run perfectly and got expected result but when I tried to convert into Hibernate Criteria then I have no idea about bellow code

(a.id = b2.abc_id and (b.timestamp < b2.timestamp OR b.timestamp = b2.timestamp AND b.id < b2.id))

I have already tried following code

Criteria criteria = currentSession().createCriteria(Abc.class);
criteria.createAlias("xyz", "b");
criteria.createAlias("xyz", "b2");
criteria.add(Restrictions.le("b.timestamp", "b2.timestamp"));
criteria.add(Restrictions.lt("b.id", "b2.id"));

But it does not work as per MySql Query

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Maulik Kakadiya
  • 1,467
  • 1
  • 21
  • 31

1 Answers1

1

I know this is an old question, but for people looking for an answer you can try the following code:

Criteria cr = session.createCriteria(YourClass.class, "entity1");
cr.add(Restrictions.ltProperty("field1", "field2"));
...
Amine ABBAOUI
  • 175
  • 1
  • 12