简体   繁体   中英

How set where in templates using jpa in CriteriaQuery

I am beginner in java EE and I need know, how set a clausule where in this code of criteria

public <T> List<T> findEntity(Class<T> entityClass) {
    CriteriaQuery<T> criteria = builder.createQuery(entityClass);
    Root<T> entityRoot = criteria.from(entityClass);
    criteria.select(entityRoot);
    criteria.orderBy(order);
    return em.createQuery(criteria).getResultList();
}

They will notice that I am using templates in java ( <T> ) to make this code work with various entities from my database.

Then I pass the sql (in postgresql) code and the entity class.

SQL:

CREATE TABLE activity
(
  id integer NOT NULL,
  name text NOT NULL,
  _modified timestamp without time zone,
  _user integer,
  _enable boolean,
)

And class entity

public class activity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "activity_id_activity_seq" )
    @SequenceGenerator( name = "actividad_id_actividad_seq", sequenceName = "actividad_id_actividad_seq", allocationSize = 1, initialValue = 110 )
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;
    @Column(name = "_modified")
    @Temporal(TemporalType.TIMESTAMP)
    private Date modified;
    @Column(name = "_enable")
    private Boolean enable;
    @Column(name = "_user")
    private Integer user;
    .......
}

I need to know is how to add a where clause in the code of the function findEntity using methods template.

The where clause of criteria should be matching the column _Enable , this column this column mentioned is repeated in 4 tables in my database, so you note that it is better to reuse code in that function.

thanks

Here is an example how to add where() clause:

public static <T> List<T> findEntity(Class<T> entityClass, boolean isEnabled) {
    CriteriaQuery<T> criteria = builder.createQuery(entityClass);
    Root<T> entityRoot = criteria.from(entityClass);
    criteria.select(entityRoot);

    criteria.where(builder.equal(
            entityRoot.get("enable"), //path expression
            builder.parameter(Boolean.class, "isEnabled")) //parameter expression
    );

    criteria.orderBy(order);
    return em.createQuery(criteria)
            .setParameter("isEnabled", isEnabled)
            .getResultList();
}

The entityRoot.get("enable") statement defines Path<String> expression. This is equivalent of a.enable which uses a colon to denote a parameter in JPQL, ie

SELECT a FROM Activity a WHERE a.enable = :isEnabled

To build conditional expression one must also create a ParameterExpression<Boolean> which corresponds to the parameter of typed query that executes our criteria.

Using this approach you can easily extend your generic method with new parameters.

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM