-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Problem with JPA Criteria
PostPosted: Wed Oct 13, 2010 4:33 am 
Newbie

Joined: Wed Oct 13, 2010 4:07 am
Posts: 1
Hi!,

I'm newbie with JPA and I can't help with some JPA Criteria query.

Fragment of my code:
Code:
ArrayList<Predicate> predicates = new ArrayList<Predicate>();
predicates.add(criteriaBuilder.like(commitRoot.<String> get("name"), "%USER_ENTRY%"));
(...)

CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Commit> criteriaQuery = criteriaBuilder.createQuery(Commit.class);
Root<Commit> commitRoot = criteriaQuery.from(Commit.class);
commitRoot.fetch("branches");

criteriaQuery.where(predicates.toArray(new Predicate[] {}));
criteriaQuery.orderBy(criteriaBuilder.desc(commitRoot.get("commitDate")));

TypedQuery<Commit> typedQuery = em.createQuery(criteriaQuery);
List<Commit> results = typedQuery.getResultList();


As my code shows, I want to compare some user entry text with "name" attribute of "branches" collection which is fetched with "Commit" objects.
When I run this code I get :
Code:
java.lang.NullPointerException
   org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:110)
   org.hibernate.ejb.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:218)
   org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:189)


It points to line:
Code:
predicates.add(criteriaBuilder.like(commitRoot.<String> get("name"), "%USER_ENTRY%"));


fragment of Commit.java
Code:
    @ManyToMany(cascade = { CascadeType.ALL })
    @JoinTable(name = "branch_commit", joinColumns = { @JoinColumn(name = "commitId") }, inverseJoinColumns = { @JoinColumn(name = "branchId") })
    public List<Branch> getBranches() {
        return branches;
    }


fragment of Branch.java:
Code:
@Entity
@Table(name = "branch")
public class Branch implements Serializable, Comparable<Branch> {

        private static final long serialVersionUID = 1L;

        private Long branchId;

        private String name;


What is wrong? I suppose, Hibernate can't see "name" attribute.
This code also fails with the same exception:
Code:
predicates.add(criteriaBuilder.like(commitRoot.<String> get("branches.name"), "%USER_ENTRY%"));


But, when I run JPQL code like this :
Code:
       Query q = em.createQuery("from Commit commit left join fetch commit.branches where name like '%USER_ENTRY%'");
        List results2 = q.getResultList();

it works as I expect. How to translate it to Criteria?

Please, help! Thanks in advance!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.