6

It seems that everywhere I look, there are outdated versions of this which no longer work. My problem seems really simple. I have a class in Java, which maps to a derby database. I'm using annotations, and have successfully managed to create all the other tables in my DB, yet with this specific example, where I just need a Map, which does not use any other class, just two simple string values. I have come across all types of errors trying everything I've found online.

Does anybody know of a simple way of doing this, without using deprecated annotations?

Thanks in advance!

DFM
  • 69
  • 1
  • 1
  • 3

1 Answers1

15

Chapter 2.2.5.3.4 of Hibernate Annotations documentation describes the necessary annotations. You need to do something like:

@Entity
public class MyEntity {
    ...

    @ElementCollection // this is a collection of primitives
    @MapKeyColumn(name="key") // column name for map "key"
    @Column(name="value") // column name for map "value"
    public Map<String,String> getMyMap() {

    ...
}
Pwnstar
  • 2,333
  • 2
  • 29
  • 52
ChssPly76
  • 99,456
  • 24
  • 206
  • 195
  • I used these annotations & made a separate entity for myMap. I am getting this error: Can not set java.lang.Long field AttributeValueRange.id to java.lang.String. What could be the problem here? – Jatin Sehgal Feb 25 '13 at 05:24
  • 5
    I think it should be `@MapKeyColumn` and not `@MapKey`, the latter is used when the key is an attribute of the value. – Tobb Dec 09 '13 at 10:55
  • see https://stackoverflow.com/questions/3393649/storing-a-mapstring-string-using-jpa which gives the annotations on how to customize the collection table / columns name – Thierry Aug 02 '17 at 09:50