Question

I have a view named V_ENTITY in my database which is populated with a few entries. Using SQL scrapbook in Eclipse I can easily do queries which return desired results.

However, when I try to retrieve the entries from my DAO class, I get nothing. My count returns 0 as number of entries, while there are 7 of them, which I can fetch with native SQL queries via scrapbook.

I have created a separate entity for that particular view:

@Entity
@Table(name = "V_ENTITY")
public class ViewEntity {
    ...
}

I use JPA2 CriteriaQuery API for dynamic queries I do. Here is the example for count that is bugging me:

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<ViewEntity> transactionQuery = cb.createQuery(ViewEntity.class);
    Root<ViewEntity> ViewEntityRoot = transactionQuery.from(ViewEntity.class);

    Predicate[] predicates = new Predicate[<someSize>]
    predicates[<someIndex>] = cb.equal(
        root.get("foreignName").as(String.class),
        "%" + foreignName + "%"
    )

    CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
    countQuery.select(cb.count(ViewEntityRoot));
    countQuery.where(predicates);

    TypedQuery<Long> finalQuery = entityManager.createQuery(countQuery);
    return finalQuery.getSingleResult().intValue();

Can anyone help me find a flaw in my code/thinking?

Was it helpful?

Solution

Use the <property name="show_sql">true</property> property to show us the generated sql pls

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top