Hibernate persist Map <String, String> without reference to other tables
Could you help me save a line map using Hibernate?
Map values โโcome from the client and are random, so I donโt want to keep a separate table for map values
Exception
Called: org.hibernate.AnnotationException: related class not found: java.lang.String
code
@Entity public class UserConfig { @Id @SequenceGenerator(sequenceName = "CONFIG_SEQ", name = "ConfigSeq", allocationSize = 1) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ConfigSeq") private Long id; @ElementCollection(targetClass = String.class) @CollectionTable(name = "MAP") @MapKey(name="key") @Column(name="value") private Map<String, String> map; Update
Could you also explain how to persist Map<MyEnum, String> if MyEnum is a MyEnum -mapped class?
+5
1 answer
According to the specification, you should annotate the following map:
@ElementCollection(targetClass = String.class) @CollectionTable(name = "MAP") @MapKeyColumn(name="key") @Column(name="value") private Map<String, String> map; So @MapKeyColumn instead of @MapKey .
So you should annotate the map if it is defined as:
private Map<Basic, Basic> map; // (ie Map<String, String>) You use the @MapKey annotation when you have a map defined as:
private Map<Basic, Entity> map; // (ie Map<String, User>) And finally, you use the @MapKeyEnumerated annotation when you have a specific map declaration defined:
private Map<Enumeration, Basic> map; // (ie Map<MyEnum, String>) +9