Hibernate: 4.2.18.Final-redhat-2 Java 1.7/1.8 JBOSS EAP 6.4
The issue is about the conversion of Java enum type to Varchar when a filter-def/filter is used to filter the results from a child class. I see that on passing a collection of enum (VehicleType) to t he filter did not produce a resultset. I looked at the generated query, and it looks correct. FYI to confirm, I took the query and ran it through SQL developer by passing valid params, it produced the desired result.
I was curious and logged the params and found: 17:30:12,088 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (http-/127.0.0.1:8080-2) binding parameter [1] as [VARBINARY] - AUDI 17:30:12,088 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (http-/127.0.0.1:8080-2) binding parameter [2] as [VARBINARY] - PORSCHE
VARBINARY appears to be the issue, the column vehicle_type in table Vehicle is defined as VARCHAR.
I passed a Collection of<String> to the filter instead of Collection<VehicleType>, updated the type in filter-param to string, and this worked.
The Question: The hbm already maps the VehicleType to org.hibernate.type.EnumType ( config below), the inserts work fine, the VehicleType.AUDI are is converted to VARCHAR and I see the vehicle_type column populated with correct values e.g. AUDI , PORSCHE.
The issue occurred with a filter-def/filter, I'm expecting hibernate to handle the translation as well i.e. the enum to VARCHAR, but it appears that is instead translating it to VARBINARY. I would at least expect hibernate to throw an exception as the VARBINARY does not match the target column type VARCHAR. Looks like a bug to me.
Annotated Vehicle.VehicleType with @Enumerated(EnumType.STRING), this did not make any difference.
Appreciate your answer.
Parent is the root table, that holds a one-to-one with a Vehicle table. Vehicle table has a property of type enum (VehicleType) The filter-def is used to filter results with a collection of enum Collection<VehicleType> of matching type.
Collection<VehicleType> vehicleTypes= new ArrayList<VehicleType>(); changeRequestReferences.add(VehicleType.AUDI); changeRequestReferences.add(VehicleType.PORSCHE);
Filter filter = getCurrentSession().enableFilter("vehicleFilter"); filter.setParameterList("filterParams", vehicleTypes);
<hibernate-mapping package="com.volks.cars.Parent" default-access="field">
<list name="vehicles" inverse="false" cascade="all-delete-orphan" lazy="false" batch-size="100"> <key column="cr_id" not-null="true"/> <list-index column="cr_idx"/> <one-to-many entity-name="Vehicle"/> <filter name="vehicleFilter" condition="vehicle_type in (:filterParams)"/> </list>
<filter-def name="vehicleFilter"> <filter-param name="filterParams" type="com.volks.car.enums.VehicleType"/> </filter-def> </hibernate-mapping>
<hibernate-mapping package="com.volks.cars.Parent" default-access="field"> <class name="Vehicle" entity-name="Vehicle"> <id name="id"> <generator class="native"/> </id> <property name="vehicleType" not-null="true"> <type name="org.hibernate.type.EnumType"> <param name="enumClass"> com.rj.cob.domain.enums.VehicleType </param> <param name="useNamed">true</param> </type> </property> </class> </hibernate-mapping>
public enum VehicleType{ AUDI, PORSCHE }
|