开发者

Criteria API: query.from returns only null

I'm pretty new to Criteria API, as a matter of fact I just pushed our latest project to JBoss 6 and JPA 2.0 just for the Criteria API.

Now I am trying to get the easiest query running, but somehow without success:

Without Criteria API it works without problems (Msv being my entity class):

Msv someMsv = manager.find(Msv.class, 1234L);
System.out.println(someMsv);

The criteria stuff however does not give me any results from the DB:

CriteriaBuilder builder = manager.getCriteriaBuilder();
CriteriaQuery<Msv> criteriaQuery = builder.createQuery(Msv.class);
Root<Msv> from = criteriaQue开发者_如何转开发ry.from(Msv.class);
// this returns null! /\
Path<Msv> pk = from.get("PK_MSV_NR");
Expression<Boolean> exp = builder.equal(pk, 1234L);
CriteriaQuery<Msv> select = criteriaQuery.select(from).where(exp);
[...]

When running the second code snipped I get a NullPointerException thrown on line 5 (Path pk ...), hence the above cireriaQuery.from returns null.

Did I miss something essential, or what is the problem here? The first snippet is able to pull the correct entry from the DB and syso it, so I figured it is not a problem with my persistance config but rather with my Criteria API code. But that one I pulled from a tutorial basically line by line...

Tech: I am using hibernate-3.6.6, hibernate-jpa-2.0-cr, jboss-as-6.0.0 and oracle11.

EDIT: Looked into this some more and discovered the following: it is not actually criteriaQuery.from(...) that produces null. The NullPointerException is not thrown because .get() is called on null, but is actually thrown by/in .get() method. I can for the life of me not see why however...


Argh, I finally found out what I did wrong. As I let eclipse generate the entity classes for me I wasn't paying enough attention to the nomenclature of their fields.

The field I was looking for is called 'PK_MSV_NR' in the database, however eclipse named it 'pkMsvNr' in the entity class. Hence the problem.

Still, I think it is kind of strange behavior to throw a NullPointerException off of this. There's really not much null pointing going on here...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜