TAGS :Viewed: 7 - Published at: a few seconds ago

[ Fetch subclass with Hibernate criteria ]

I'm trying to get 'Object' (which is a row in my db) using Hibernae. The 'Object' is referenced in the db by 'Nofitication'. 'Notification' contains a row that is referenced to 'Object'.

My problem is that the object has also subclass, and i want to get the full subclass while i'm only getting the lazy Object. Here is my code:

public ScrollableResults getScrollableObjects(Transaction transaction, NotificationAttributes notificationAttributes) {
    Criteria criteria = transaction.getSession().createCriteria(Notification.class);
    criteria.setProjection(Projections.property("object"));
    criteria.add(Property.forName("name").eq(notificationAttributes.getName()));
    criteria.createAlias("object", "object",     CriteriaSpecification.LEFT_JOIN);
    ScrollableResults scroll = criteria.setFetchSize(1000).scroll(ScrollMode.FORWARD_ONLY);
    return scroll;
}

Any Suggestions on how can i solve it? Thanks.

Answer 1


Try it with this example

Criteria person = session.getCurrentSession().createCriteria(Person.class).createAlias("personId", "personId");

person.add(Restrictions.disjunction().add(Restrictions.ilike("PersonFirstname",Search,MatchMode.ANYWHERE))
.add(Restrictions.ilike("personId.Jacket",Search,MatchMode.ANYWHERE))
.add(Restrictions.ilike("personId.Color",Search,MatchMode.ANYWHERE))
.add(Restrictions.ilike("personId.Size",Search,MatchMode.ANYWHERE)));