java - Way to use @Convert to convert key of a Map (JPA)? -
i using java.time.localdate of java 8, , want convert sql date can persisted. here converter:
@converter(autoapply = true) public class localdatepersistenceconverter implements attributeconverter<localdate, date> { @override public date converttodatabasecolumn(localdate attribute) { return java.sql.date.valueof(attribute); } @override public localdate converttoentityattribute(date dbdata) { return dbdata.tolocaldate(); } }
here how getting used:
@entity @access(accesstype.field) public class mutualfund implements serializable { @id private final string schemename; @convert(attributename="key",converter=localdatepersistenceconverter.class) @elementcollection private map<localdate, price> nav; //other fields , methods }
when try persist mutualfund object, following exception:
java.lang.illegalstateexception: @convert placed on map attribute [mypackage.mutualfund.nav] must define attributename of 'key' or 'value'
what wrong in code? please help. thanks.
your mapping of map not completed getting exception.
there 2 options map mapping, not sure 1 trying achieve.
a) localdate key of nav map field of price object:
@embeddable public class price implements serializable { @convert(converter=localdatepersistenceconverter.class) //you don't need @convert if annotated @converter(autoapply = true) public localdate mark; public double price; }
then in collection mapping need specify key field used:
@elementcollection @mapkey(name="mark") public map<localdate, price> nav;
b) key element of nav not part of price object, need explicity map column:
@embeddable public class price implements serializable { public double price; }
and map annotation:
@convert(converter=localdatepersistenceconverter.class) @elementcollection @mapkeycolumn(name="mark") public map<localdate, price> nav;
edit turned out hibernate demanding eclipselink
with @converter(autoapply = true) works without pointing converter both explicit , implicit key column.
@elementcollection @mapkeycolumn(name="mark") //this annotation can omitted key column called nav_key public map<localdate, price> nav;
i not make work without autoapply. persistence context build if converter declared attributename="key.":
@convert(attributename="key.", converter=localdatepersistenceconverter.class) @elementcollection public map<localdate, price> nav = new hashmap<>();
but converter not correctly apllied. during schema generation key column tried generated blob (and failed no length given) , serializaton instead of conversion triggered during insert/fetching. not find way fix it.
Comments
Post a Comment