Search Hibernate Entities By Prototype
I've got JPA entity class like this: @Entity @Table(name = 'person') public class Person { @Id private Long id; private String lastName; private String firstName; pr
Solution 1:
So you're looking for a prototype? Hibernate has a handy Example
criteria just for this, so if you don't mind tying yourself to the Hibernate API, try this example from the docs:
Cat cat = new Cat();
cat.setSex('F');
cat.setColor(Color.BLACK);
List results = session.createCriteria(Cat.class)
.add( Example.create(cat) )
.list();
It specifically says:
Version properties, identifiers and associations are ignored. By default, null valued properties are excluded.
Which is just what you appear to want.
Solution 2:
You should look into Hibernate Criteria. You can stack restrictions as such:
List cats = sess.createCriteria(Cat.class)
.add( Restrictions.like("name", "Fritz%") )
.add( Restrictions.or(
Restrictions.eq( "age", new Integer(0) ),
Restrictions.isNull("age")
) )
.list();
Restrictions refer to annotated variables in the class. Take a look at the documentation.
Post a Comment for "Search Hibernate Entities By Prototype"