java - Persist a Map< Long, Boolean > with JPA/Hibernate? -
i not expert in jpa/hibernate , not know if trying achieve not possible or doing wrong , since expect latter here goes nothing.
i have map< long, boolean > trying persist table , following example described here: storing map<string,string> using jpa
and other examples describe same practice.
what trying is:
@id private long id; @elementcollection @collectiontable(name = "state_map", joincolumns = @joincolumn(name = "id")) @mapkeycolumn(name = "name") @column(name = "value") @type(type = "org.hibernate.type.truefalsetype") private map<long, boolean> mybooleanmap; and table defined as:
create table if not exists state_map (id bigint not null, name bigint not null, value char); but hibernateexception: wrong column type in state_map column value. found: bigint, expected: char(255)
when change bigint char(255) entitymanager starts when trying put in map , persist getting java.lang.classcastexception: java.lang.long cannot cast java.lang.boolean. suppose @type annotation applied on key column instead on value column.
the way managed make work map< string, string >. have tried in same table did not work have declare surrogate id mapped entity:
create table if not exists state_id (id serial primary key); @entity @table(name = "state_id") public class statemodel {...} hope can help, thanks.
update - solution
i have solved introducing embeddable wrap boolean value column:
@embeddable class booleanwrapper{ @column(name = "value") @type(type = "org.hibernate.type.truefalsetype") private boolean myboolean;} and map became:
@elementcollection @collectiontable(name = "state_map", joincolumns = @joincolumn(name = "id")) @mapkeycolumn(name = "name") private map<long, booleanwrapper> mybooleanmap;
i see have annotated mybooleanmap @type(type = "org.hibernate.type.truefalsetype") think incorrect. can define truefalsetype char column not map. don't think bigint problem
Comments
Post a Comment