Search hibernate entities by prototype

前端 未结 2 1581
无人及你
无人及你 2021-01-26 20:06

I\'ve got JPA entity class like this:

@Entity
@Table(name = \"person\")
public class Person {
   @Id
   private Long id;
   private String lastName;
   private S         


        
相关标签:
2条回答
  • 2021-01-26 20:24

    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.

    0 讨论(0)
  • 2021-01-26 20:40

    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.

    0 讨论(0)
提交回复
热议问题