Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA 2 CriteriaQuery Question

I am just starting out with JPA 2 criteria query API and finding it tough to learn. Looked around the net a bit, but haven't found good examples/tutorials yet. Can someone suggest a good tutorial and/or help me with the following simple query I am trying to code?

I have a class called Transaction that has a reference to the Account that it belongs:

public class Transaction {
    private Account account;
    ...
}

public class Account {
    private Long id;
    ...
}

I need to code a query that gets all the transactions for an account given its account id. Here's my attempt at doing this (which obviously doesn't work):

public List<Transaction> findTransactions(Long accountId) {        
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class);
    Root<Transaction> transaction = query.from(Transaction.class);

    // Don't know if I can do "account.id" here
    query.where(builder.equal(transaction.get("account.id"), accountId));
    return entityManager.createQuery(query).getResultList();
}

Can someone point me in the right direction?

Thanks. Naresh

like image 842
Naresh Avatar asked Jan 20 '11 02:01

Naresh


People also ask

How do I add by order in criteria builder?

You can define an ORDER BY clause with the orderBy method of the CriteriaQuery interface and the asc or desc method of the CriteriaBuilder interface. The following CriteriaQuery returns Book entities in the ascending order of their title attribute. List<Book> books = em. createQuery(cq).

What method can be used to sort the extracted results using criteria queries?

The ORDER BY clause is used to sort the data and arrange them either in ascending or descending order. The CriteriaQuery interface provides orderBy() method to define the type of ordering.

What is CriteriaBuilder in Java?

Interface CriteriaBuilder. public interface CriteriaBuilder. Used to construct criteria queries, compound selections, expressions, predicates, orderings. Note that Predicate is used instead of Expression<Boolean> in this API in order to work around the fact that Java generics are not compatible with varags.


1 Answers

Solution:-

public List<Transaction> findTransactions(Long accountId) { 
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class);
        Root<Transaction> _transaction = query.from(Transaction.class);

        Path<Account> _account = _transaction.get(Transaction_.account);
        Path<Long> _accountId = _account.get(Account_.id);

        query.where(builder.equal(_accountId, accountId));
        return entityManager.createQuery(query).getResultList();
    }

To understand the meaning of above code please read:- Dynamic, typesafe queries in JPA 2.0

And to understand/generate JPA Metamodel please read:- Hibernate Metamodel Generator Reference Guide

like image 119
dira Avatar answered Nov 02 '22 06:11

dira