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>
I have never defined a persistent entity with .xml config, but I hope it helps:
You are defining a type dao.converter.DateToLongConverter
to the "aColumn", but in fact, you should define a converter to it. The type you mentioned should be the type object you are working with in your code: In your example, your type should be a Long
.
This other answer will help you define your converter on xml config. Its simply another format for type name.
This is how @Convert
is defined with annotations. You should be looking for replicating this template on your hbm.
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Convert(converter = DateToLongConverter.class)
private Long aColumn;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.