JPA NamedQuery with Object in WHERE clause that is converted using AttributeConverter

661 views Asked by At

I have got a bit of a problem using JPA NamedQuery in combination with an AttributeConverter.

So basically, I have got an Entity with a field xyz of type XYZ, that is converted using AttributeConverter<XYZ, String>, so the value is stored as a VARCHAR in the database. This works nicely. But I'm not quite sure about how to query for Entity.xyz using a NamedQuery:

@NamedQuery(name="Entity.testQuery", query="SELECT e FROM Entity e WHERE e.xyz=:xyz

If I try it in this way, Hibernate crashes during startup. So that can't be right. Is there any way to tell Hibernate to compare the "unconverted" (i.e. String) value like WHERE e.xyz=:xyzAsString?

Best regards Jonas

EDIT: Hibernate crashes, I think, because it tries to query e.xyz as a JOIN with entity XYZ which doesn't exist. So maybe, this is a bug (I'm on WildFly 9.2, which uses Hibernate 4.3)

2

There are 2 answers

0
JonasB On

After adding some changes in other places, the problem no longer occurs. So my conclusion, that it is related to a converted attribute as parameter seems to be wrong.

Thanks nonetheless

1
Peter Šály On

The parameter has to be of type XYZ and not String. Use converter method to obtain corresponding instance XYZ for needed String value.