1

I am trying to do a sort on a Hibernate dataset, ordering by a attribute of an (optionally) associated object. I followed the example from hibernate order by association - and that works to an extent. The problem comes when there is not an associated object, the whole record is ignored.

If I change the association type of the object to be an outer join, will it help matters? And if so, can someone please point me towards the correct directive?

Many thanks.

Community
  • 1
  • 1
readikus
  • 369
  • 1
  • 6
  • 17

2 Answers2

1

The question referenced utilized Criteria queries, so I am not sure if you need an answer with those or if you just want HQL. Assuming HQL is OK, if you have entities like this:

class EntityA {
   private Long id;
   @OneToMany
   private EntityB b;

   ....
}

class EntityB {
  private Long id;
  private String name;
}

Where you have an optional OneToMany relation between EntityA and EntityB, then you should be able to use a query something like:

"SELECT obj FROM " + EntityA.class.getName() + " obj LEFT JOIN obj.b obj2 ORDER BY obj2.name";

Where you will select all EntityA joined with EntityB where it exists, ordering by the EntityB.name field.

jcern
  • 7,798
  • 4
  • 39
  • 47
  • Thx for your time, but I was trying to avoid HQL, as the object is pretty complex and has a lot of joins, so I didn't see the point of re implementing all of those joins individually. – readikus Oct 16 '12 at 14:13
1

I managed to solve the problem with only one line of code change. When constructing the Criteria object, I was able to pass the parameter JoinFragment.LEFT_OUTER_JOIN. For example:

    Criteria subCriteria = criteria.createCriteria(subTypeName, JoinFragment.LEFT_OUTER_JOIN);
readikus
  • 369
  • 1
  • 6
  • 17