0

I have the following classes

class Category {
    Long id;
}
class Question  {
    Long id;

    @OneToOne (cascade=CascadeType.ALL)
    Category category;
}
class Test   {
    Long id;    
    Map<Category, Long> splitUp;
}

These are partial versions of the my classes, I have annotated the category and Quesiton classes but having trouble annotating the map "splitUp" in the Test Class. I would like some help in that.

AbrahamDaniel
  • 569
  • 2
  • 8
  • 19
  • It has been answered here: http://stackoverflow.com/questions/3393649/storing-a-mapstring-string-using-jpa – Olivier Meurice Dec 13 '14 at 14:20
  • 1
    @OlivierMeurice Its not answered there. I have a key which of type Category and not String ergo relationship annotations have to be added additionally which is were I m having trouble – AbrahamDaniel Dec 13 '14 at 16:18
  • See this https://stackoverflow.com/questions/11325059/mapping-map-with-an-entity-as-a-key-in-jpa but as the answers show, JPA doesn't cater properly for an Entity key (and non-Entity value) so is hacky. It also insists on you having a join table, whereas it would be nice to have the ability to have the value as a field in the key entity also (not sure if you intended that, but just mentioning it), whereas this ElementCollection route doesn't allow that. – Neil Stockton Dec 13 '14 at 18:00

1 Answers1

1
class Test   {
    Long id;    

    @ElementCollection
    @JoinTable(name = "CATEGORY_JOIN_TABLE", joinColumns = @JoinColumn(name = "category_join_table_id_column"))
    @MapKeyJoinColumn (name = "category_id")
    @Column(name = "id")
    Map<Category, Long> splitUp;
}

This should work.

Andy Dufresne
  • 6,022
  • 7
  • 63
  • 113