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


Groups > comp.lang.java.programmer > #7673

CriteriaQuery and JPA

Date 2011-09-07 14:58 +0200
From Lele <lamia@mail.lcom>
Newsgroups comp.lang.java.programmer
Subject CriteriaQuery and JPA
Message-ID <4e676a82$2@news.x-privat.org> (permalink)
Organization X-Privat.Org NNTP Server - http://www.x-privat.org

Show all headers | View raw


Hi.

I'm trying to make some examples of query using CriteriaQuery.

For now i'm 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");
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?


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:

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 ?

Please help me!

Thank you so much!

Back to comp.lang.java.programmer | Previous | NextNext in thread | Find similar


Thread

CriteriaQuery and JPA Lele <lamia@mail.lcom> - 2011-09-07 14:58 +0200
  Re: CriteriaQuery and JPA Lew <lewbloch@gmail.com> - 2011-09-07 11:04 -0700

csiph-web