-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problem with JPA Criteria
PostPosted: Sat Feb 09, 2013 4:35 pm 
Newbie

Joined: Sat Aug 04, 2012 12:00 pm
Posts: 6
I got the following query that is working fine:
Code:
TypedQuery<Song> query = em.createQuery("select s from Song s where (s.totalDownloads = 20 or s.weight = 10.00) and (s.price = 20.00 or s.type = :type)", Song.class);
query.setParameter("type", SongType.PRAISE);

System.err.println(query.getResultList()); //bring two results as expected


If I translate it to a Criteria I got an exception.

This is the code:
Code:
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Song> criteriaQuery = criteriaBuilder.createQuery(Song.class);
Root<Song> mainRoot = criteriaQuery.from(Song.class);
criteriaQuery.select(mainRoot);
Predicate predicate1 = criteriaBuilder.equal(mainRoot.get("totalDownloads").as(Long.class), 20L);
Predicate predicate2 = criteriaBuilder.equal(mainRoot.get("weight").as(Float.class), 10.00f);
Predicate firstOr = criteriaBuilder.or(predicate1, predicate2);

Predicate predicate3 = criteriaBuilder.equal(mainRoot.get("price").as(double.class), 20.00d);
Predicate predicate4 = criteriaBuilder.equal(mainRoot.get("type"), SongType.PRAISE);
Predicate secondOr = criteriaBuilder.or(predicate4, predicate3);

Predicate finalPredicate = criteriaBuilder.and(firstOr, secondOr);

criteriaQuery.where(finalPredicate);
TypedQuery<Song> criteriaGeneratedQuery = em.createQuery(criteriaQuery);
System.err.println(criteriaGeneratedQuery.getResultList());


The exception is:
Quote:
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.dialect.function.CastFunction.render(CastFunction.java:55)
at org.hibernate.hql.internal.ast.SqlGenerator.endFunctionTemplate(SqlGenerator.java:219)
at org.hibernate.hql.internal.antlr.SqlGeneratorBase.methodCall(SqlGeneratorBase.java:2166)
at org.hibernate.hql.internal.antlr.SqlGeneratorBase.simpleExpr(SqlGeneratorBase.java:2521)
at org.hibernate.hql.internal.antlr.SqlGeneratorBase.expr(SqlGeneratorBase.java:1383)
at org.hibernate.hql.internal.antlr.SqlGeneratorBase.binaryComparisonExpression(SqlGeneratorBase.java:2747)
at org.hibernate.hql.internal.antlr.SqlGeneratorBase.comparisonExpr(SqlGeneratorBase.java:1211)
at org.hibernate.hql.internal.antlr.SqlGeneratorBase.booleanExpr(SqlGeneratorBase.java:860)
at org.hibernate.hql.internal.antlr.SqlGeneratorBase.booleanOp(SqlGeneratorBase.java:2685)
at org.hibernate.hql.internal.antlr.SqlGeneratorBase.booleanExpr(SqlGeneratorBase.java:840)
at org.hibernate.hql.internal.antlr.SqlGeneratorBase.booleanOp(SqlGeneratorBase.java:2665)
at org.hibernate.hql.internal.antlr.SqlGeneratorBase.booleanExpr(SqlGeneratorBase.java:840)
at org.hibernate.hql.internal.antlr.SqlGeneratorBase.whereExpr(SqlGeneratorBase.java:725)
at org.hibernate.hql.internal.antlr.SqlGeneratorBase.selectStatement(SqlGeneratorBase.java:178)
at org.hibernate.hql.internal.antlr.SqlGeneratorBase.statement(SqlGeneratorBase.java:111)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.generate(QueryTranslatorImpl.java:232)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1735)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:487)
at org.hibernate.ejb.criteria.CriteriaQueryCompiler.compile(CriteriaQueryCompiler.java:227)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:622)
at com.uaihebert.test.Main.main(Main.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)


The problem is that I found an "ugly" solution. To use the attribute totalDownloads as Long instead of long.

Is this a Hibernate bug or the problem is my code? Thanks! =D


Top
 Profile  
 
 Post subject: Re: Problem with JPA Criteria
PostPosted: Mon Feb 11, 2013 11:13 am 
Newbie

Joined: Sat Aug 04, 2012 12:00 pm
Posts: 6
Anyone?

Should it be considered as a bug?


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

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.