- My Hibernate bean ContentElementTypeProperty references another Hibernate Bean TestUnitType (Many to One).
- TestUnitType is a field of ContentElementTypeProperty.
- In the database, testunittypeid is a column in the table contentelementtypeproperty.
I am seeking to retrieve all rows from contentelementtypeproperty WHERE testunittypeid is null OR testunittypeid = [given Long value]
The method shown below returns 0 results.
However, if I delete: either
.add(Restrictions.isNull("testUnitType"));
or
cr.createCriteria("testUnitType")
.add(Restrictions.eq("id", tutId));
I get results (but obviously not all the rows I need).
How do I combine these two so that they make an OR criteria given that each is created under a distinct createCriteria call?
@Transactional(propagation = Propagation.MANDATORY)
public List<ContentElementTypeProperty> getContentElementTypePropertiesForTut(Long businessId, Long tutId)
throws TestStructureException
{
SS.getLogger().debug("getContentElementTypePropertiesForTut business id:"+businessId +" tutid: "+tutId);
try
{
Session session = this.sessionFactory.getCurrentSession();
Criteria cr = session.createCriteria(ContentElementTypeProperty.class)
.add(Restrictions.isNull("testUnitType"));
cr.createCriteria("testUnitType")
.add(Restrictions.eq("id", tutId));
cr.createCriteria("business").add(Restrictions.eq("id", businessId));
List<ContentElementTypeProperty> result = cr.list();
SS.getLogger().debug("getContentElementTypePropertiesForNullTutOnly result size:"+result.size());
return result;
}
catch (Exception e)
{
SS.getLogger().error(e.getMessage(), e);
throw new TestStructureException(e);
}
}
UPDATE Following advice from Malagunna, I tried using criterion (below). However, this only returns rows for crAux2.
Criteria cr = session.createCriteria(ContentElementTypeProperty.class);
cr.createAlias("testUnitType", "tut");
Criterion crAux1 = Restrictions.isNull("testUnitType");
Criterion crAux2 = Restrictions.eq("tut.id", tutId);
cr.add(Restrictions.or(crAux1, crAux2));