Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1022

Re: CriteriaQuery and JPA

From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.help
Subject Re: CriteriaQuery and JPA
Date 2011-09-07 11:02 -0700
Organization http://groups.google.com
Message-ID <fcd93a94-1fe2-42ee-aa7d-545df845d5c1@glegroupsg2000goo.googlegroups.com> (permalink)
References <4e676e7b$1@news.x-privat.org>

Show all headers | View raw


Lele wrote:
> I'm trying to make some examples of query using CriteriaQuery.
> 
> For now i'm [sic] trying to do something like that:
> 
> @Entity
> @Table(name = "USERS")
> public class ClassTest {
>      public ClassTrueAuth(){
>      this.username = "test";
>      }
> 
>      @Id
>      @Column(name="testfield")
>      public boolean testField ;
> 
>      @Column(name="username")
>      public String username;
> }
> 
> and inside my code:
> 
> ClassTest cta = new ClassTest();
> 
> CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
> CriteriaQuery cq = criteriaBuilder.createQuery(ClassTest.class);
> Root<ClassTest> from = cq.from(ClassTest.class);
> cq.select(from);
> Predicate p = criteriaBuilder.equal(from.get(cta.username),"test");

Here's your problem -----------------------------^

> cq.where(p);
> TypedQuery<ClassTest> q = entityManager.createQuery(cq);
> List<ClassTest> result = q.getResultList();
> 
> This give to me a NullPointerException... How to check if a string field 
> is equal to a constant?

Read the Javadocs for 'Root#get(String attributeName)'.  You gave a 'null' attribute name, and what you tried to do wasn't giving an attribute *name*, but an attribute *value*.  
 
> I tried to make another field in TestClass:
> @Column(name="test")
> public String test = "test";
> 
> and retry the same code above. The query string printed in console is:

The same code?  The exact same code?  Really?  No change whatsoever?  You didn't substitute 'test' for 'username'?  Are you sure?  Because I don't believe you.  If it was the same code, it would have had the same result.  You had a different result, ergo the code differed.

> select classtest0_.testfield as testfield35_, classtest0_.username as 
> username35_, classtest0_.test as test36_ from USERS classtruea0_ where 
> classtruea0_.test=?
> 
> and 'result' is empty... Why where clause is test=? instead of 
> username=test ?

Because you gave it "test" as the attribute name.

'Root#get(String name)' takes the *name* of the attribute as an argument.

In your first attempt, you gave it a 'null' value, hence the NPE. 

BTW, you said only that "[t]his give to me a NullPointerException" with no details such as, importantly, *where* the exception was and *what* it told you.  Bug-hunting requires attention to detail, so don't omit the important details that tell you what exactly you did wrong.

In your second attempt, you told 'from()' to use "test" as the attribute name, so it did.

Read the Javadocs.
<http://java.sun.com/javaee/6/docs/api/javax/persistence/criteria/Path.html#get(java.lang.String)>

-- 
Lew

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

CriteriaQuery and JPA Lele <lamia@mail.lcom> - 2011-09-07 15:15 +0200
  Re: CriteriaQuery and JPA Lew <lewbloch@gmail.com> - 2011-09-07 11:02 -0700
    Re: CriteriaQuery and JPA Lele <lele@lele.com> - 2011-09-08 09:48 +0200
    Re: CriteriaQuery and JPA Lele <lelel@lele.com> - 2011-09-08 15:03 +0200
      Re: CriteriaQuery and JPA markspace <-@.> - 2011-09-08 06:39 -0700
        Re: CriteriaQuery and JPA Lele <lelel@lele.com> - 2011-09-08 16:53 +0200
          Re: CriteriaQuery and JPA Lew <lewbloch@gmail.com> - 2011-09-08 09:05 -0700

csiph-web