-->
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.  [ 8 posts ] 
Author Message
 Post subject: Cannot cast javax.persistence.Query to QueryImpl?
PostPosted: Mon Apr 04, 2011 6:43 am 
Newbie

Joined: Mon Apr 04, 2011 6:37 am
Posts: 4
Hi All,

My first post here.

I am a newbie and have ran into a Class Cast Exception when attempting to cast javax.persistence.Query to org.hibernate.ejb.QueryImpl.

And apparently Query is an instance of QueryImpl??

Code:
javax.persistence.Query query = em.createQuery(SOME_QUERY);
   
org.hibernate.ejb.QueryImpl qi = (org.hibernate.ejb.QueryImpl)query;


I am getting the following exception

Code:

java.lang.ClassCastException: $Proxy34 cannot be cast to org.hibernate.ejb.QueryImpl



What am I doing wrong? I am hoping to get some advice, thanks!


Top
 Profile  
 
 Post subject: Re: Cannot cast javax.persistence.Query to QueryImpl?
PostPosted: Mon Apr 04, 2011 7:55 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
According to your exception, your query variable is a dynamic proxy which implements the interface, and it is not a direct implementation of org.hibernate.ejb.QueryImpl, and who knows maybe it delegates most of method implementations to a org.hibernate.ejb.QueryImpl or maybe a org.hibernate.impl.QueryImpl

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject: Re: Cannot cast javax.persistence.Query to QueryImpl?
PostPosted: Mon Apr 04, 2011 12:14 pm 
Newbie

Joined: Mon Apr 04, 2011 6:37 am
Posts: 4
Hi, thanks for the reply.

I checked in the Eclipse debugger that it is an instance of org.hibernate.ejb.QueryImpl.

This seems to be related to target class proxying.

http://static.springsource.org/spring/d ... p-api.html


Top
 Profile  
 
 Post subject: Re: Cannot cast javax.persistence.Query to QueryImpl?
PostPosted: Tue Apr 05, 2011 3:09 am 
Beginner
Beginner

Joined: Mon Apr 04, 2011 3:31 am
Posts: 41
hey try this

import org.hibernate.Query;

private Transaction tx = null;
private Session session = null;

public Admin verifyLogin(String userId, String userPass) throws Exception
{
try
{
session = HibernateUtil.getSession();
tx = session.beginTransaction();
Query q = session.createQuery("from Admin where userId =:userId AND userPass = :userPass );
q.setString("userId", userId);
q.setString("userPass", userPass);
Admin result = (Admin) q.uniqueResult();
tx.commit();
return result;
}
catch (HibernateException e)
{
tx.rollback();
System.err.println("Error-> " + e);
throw new Exception("Failed at trying to get session");
}
}

here Admin is nothing but the hibernet class name
db having admin table

_________________
Thanks
Niki


Top
 Profile  
 
 Post subject: Re: Cannot cast javax.persistence.Query to QueryImpl?
PostPosted: Thu Apr 07, 2011 2:17 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
you shouldn't need the QueryImpl. what are you trying to do?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Cannot cast javax.persistence.Query to QueryImpl?
PostPosted: Fri Apr 08, 2011 4:18 am 
Newbie

Joined: Mon Apr 04, 2011 6:37 am
Posts: 4
s.grinovero wrote:
you shouldn't need the QueryImpl. what are you trying to do?


Hi Sanne, thanks for the reply. This is a really good question!

I was trying a HQL query with IN clause and query.setParameter(String string, Object value), see the example below:

I have the following query:

Code:

select prod from Product as prod where prod.categories IN (:cat)



In the Product bean, I have the following annotation mapping:

Code:

@ManyToMany(targetEntity=Category.class,fetch = FetchType.LAZY)
@JoinTable(
            name="CATEGORYPRODUCTREL",
            joinColumns=@JoinColumn(name="PRODPK"),
            inverseJoinColumns=@JoinColumn(name="CATPK")
)
@Cascade({CascadeType.REFRESH, CascadeType.PERSIST, CascadeType.SAVE_UPDATE, CascadeType.MERGE})
public Set<Category> getCategories() {
.....
}


When I use the HQL above, I get something like a SQL Gramma Exception.

But then I found this:

http://opensource.atlassian.com/project ... e/HHH-5126

So I upgraded Hibernate core and entity manager to the latest version and everything's fine now.

Thanks!


Top
 Profile  
 
 Post subject: Re: Cannot cast javax.persistence.Query to QueryImpl?
PostPosted: Wed Apr 13, 2011 4:22 pm 
Newbie

Joined: Mon Apr 04, 2011 6:37 am
Posts: 4
Just want to follow up on this, I executed the following using entity manager:

Code:
Query query = em.createQuery("SELECT count(p.pk) from Product as p where p.categories IN :cat");
query.setParameter("cat", category);


And I was getting SQL grammar exception, the SQL query was:

Code:
Hibernate: select count(product0_.PK) as col_0_0_ from PRODUCT product0_ cross join CATEGORYPRODUCTREL categories1_, CATEGORY category2_ where product0_.PK=categories1_.PRODPK and categories1_.CATPK=category2_.PK and (. in (?))


Category and product has a many to many relationship.

Can anyone help?


Top
 Profile  
 
 Post subject: Re: Cannot cast javax.persistence.Query to QueryImpl?
PostPosted: Wed Apr 13, 2011 7:07 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
please start new threads for new questions.
The "in" operator expects a single object to be present in a collection. so it should be like "p.category in (:categories)" : the other way around.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.