Can the same CriteriaBuilder (JPA 2) instance be used to create multiple queries?

Posted by pkainulainen on Stack Overflow See other posts from Stack Overflow or by pkainulainen
Published on 2010-05-19T09:02:02Z Indexed on 2010/05/21 23:51 UTC
Read the original article Hit count: 578

Filed under:
|
|

This seems like a pretty simple question, but I have not managed to find a definitive answer yet. I have a DAO class, which is naturally querying the database by using criteria queries. So I would like to know if it is safe to use the same CriteriaBuilder implementation for the creation of different queries or do I have to create new CriteriaBuilder instance for each query. Following code example should illustrate what I would like to do:

public class DAO() {  
    CriteriaBuilder cb = null;

    public DAO() {
        cb = getEntityManager().getCriteriaBuilder();
    }

    public List<String> getNames() {
        CriteriaQuery<String> nameSearch = cb.createQuery(String.class);
        ...
    }

    public List<Address> getAddresses(String name) {
        CriteriaQuery<Address> nameSearch = cb.createQuery(Address.class);
        ...
    }
}

Is it ok to do this?

© Stack Overflow or respective owner

Related posts about java

Related posts about criteria-api