Downcast problem

64 views
Skip to first unread message

Pablo Berra

unread,
May 28, 2015, 2:57:01 AM5/28/15
to quer...@googlegroups.com
Hi,

I need to downcast a field in JPA and I can't find the way with Querydsl.

For example, I have a 'Car' class that extends class 'Vehicle', and 'Car' class has a field 'engine'.

Whit JPQL I have cast to Car:
TypedQuery<Person> query =
    em
.createQuery ("SELECT person FROM Table person WHERE ((Car)person.vehicle).engine = :eng",
                   
Person.class);
List <persons> results = query.setParameter("eng", "diesel").getResultList();
And it works perfectly.

With Querydsl I need to cast the field engine, so I have:
QPerson person = QPerson.person;
JPAQuery query = new JPAQuery(em);
List <persons> results = query.from(person).where(person.vehicle.as(QCar.class).engine.eq("diesel")).list(person);
When I try to run this query, I get an error from JPA that says "Field 'engine' is not found in type 'Vehicle'"

It is the same error that I get if try to run the JPQL without cast:
SELECT person FROM Table person WHERE person.vehicle.engine = :eng

Which is the correct way to do that?

Thanks in advance!

Best Regards,
Pablo.

Pablo Berra

unread,
Jun 1, 2015, 1:28:20 AM6/1/15
to quer...@googlegroups.com
Please, ¿does anybody have an answer?

Pablo Berra

unread,
Jul 14, 2015, 11:42:20 PM7/14/15
to quer...@googlegroups.com
45 days and no answer yet...

Anybody???


On Thursday, May 28, 2015 at 3:57:01 AM UTC-3, Pablo Berra wrote:

Timo Westkämper

unread,
Jul 15, 2015, 2:01:10 AM7/15/15
to Querydsl on behalf of Dingo

Sorry for the delay. I think you need to make an explicit join to vehicle in querydsl to get the cast working.

I am currently on vacation, so I can't verify it.

--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pablo Berra

unread,
Jul 14, 2016, 12:43:53 PM7/14/16
to Querydsl
Hi timowest,

I had dropped this project about a year ago, and now I am back on it.

With the last version I still get this error, and I was trying to do an explicit join, but it didn't work either.

Could you send me an example please?

Thank you in advance.

Best Regards,
Pablo.


On Wednesday, July 15, 2015 at 3:01:10 AM UTC-3, timowest wrote:

timowest

unread,
Jul 16, 2016, 2:37:01 AM7/16/16
to Querydsl
Could you post the JPQL output of the explicit join version?
Reply all
Reply to author
Forward
0 new messages