I am trying to convert a type in my db to another type for use in my entity. Doing some research I found out about the AttributeConverter class defined in JPA 2.1 I believe.
My classes are all generated from Avro so there will be no JPA/HBM annotations used
All classes are mapped using hbm.xml files not entity mapping All db connections reading and writing works. So hbm configs are correct simply having issues with type conversion.
Relevant Tech Stack
-
HB Version: 4.3.11.Final
JPA Version: 2.1
I have tried to simply add the converter as the type
This is the only thing I can think to try. Outside of annotations which are not possible with my implementation
class.hbm.xml
<hibernate-mapping default-lazy="false">
<class name="ACLASS" table="CASE">
<id name="id" type="long" column="id">
<generator class="native"/>
</id>
<property name="aColumn" type = "dao.converter.DateToLongConverter" column = "A_COLUMN" />
</class>
</hibernate-mapping>
DateToLongConverter.java [Ignore the placeholder logic!]
@Converter(autoApply=true)
public class DateToLongConverter implements AttributeConverter<Long, java.sql.Date> {
@Override
public java.sql.Date convertToDatabaseColumn(Long millitime) {
System.out.println(millitime);
return new Date(1);
}
@Override
public Long convertToEntityAttribute(java.sql.Date dbData) {
System.out.println(dbData.toString());
return 1L;
}
}
Results with the above code are simply the exception:
Could not determine type for: dao.converter.DateToLongConverter, at table: ATABLE, for columns: [org.hibernate.mapping.Column(A_COLUMN)]
I've also tried adding META-INF/persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence
version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="case-consumer-persistence">
<class>dao.converter.DateToLongConverter</class>
<class>aClass</class>
</persistence-unit>
</persistence>