5

I am trying to store a Map<String, List<String>>; using JPA.

My entity looks like:

@Entity
@Table(name = "Profiles_table")
public class Profiles {

    @Id
    @Column(name = "profile_ID", updatable = false, nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    private final HashMap<String, List<String>> AllProfiles;
    ...
}

I have tried a lot of settings for the map but its not working...

The last one I tried:

@ElementCollection
@MapKeyColumn(name = "Profil")
@Column(name = "Permissions")
@CollectionTable(name = "Profiles_permissions", joinColumns = @JoinColumn(name = "profile_ID"))

The following exception is thrown:

org.hibernate.AnnotationException: Illegal attempt to map a non collection as a
@OneToMany, @ManyToMany or @CollectionOfElements: [...]Profiles.AllProfiles

thanks in advance

blackpanther
  • 10,998
  • 11
  • 48
  • 78
XioRcaL
  • 653
  • 2
  • 11
  • 23
  • Take a look at this question: http://stackoverflow.com/questions/939235/how-do-i-map-a-nested-collection-mapkey-listvalues-with-hibernate-jpa-anno It seems there is no proper way to do this. You will have to use a custom class, I'm afraid. – Cyrille Ka Jan 31 '13 at 15:41
  • thanks for that, I missed it when I search! I'll have a look – XioRcaL Jan 31 '13 at 15:46

3 Answers3

1

Strings are not entities, so you shouldn't use @OneToMany, etc...

Did you try this:

@CollectionOfElements
private Map<String, List<String>> allProfiles;
K.C.
  • 2,084
  • 2
  • 25
  • 38
  • 1
    that wont works with error Could not determine type for: java.util.List, at table: ...... – Nawa May 06 '13 at 23:05
  • 1
    Pretty sure this answer meant `@ElementCollection`, which fails with the same error @Nawa mentions. – nickb Jun 19 '18 at 19:52
1

Not really an answer but this is the way I've done it.

I store the second level of collection as a blob.

@Entity
@Table(name = "Profiles_table")
public class Profiles {

@Id
@Column(name = "profile_ID", updatable = false, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private int                                 id;

@Column(length = 16777210)
private final HashMap<String, Set<String>>  AllProfiles;
XioRcaL
  • 653
  • 2
  • 11
  • 23
0

Have you tried declaring AllProfiles as an interface type (let's say Map)? Check "Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements" in hibernate when annotating a ConcurrentHashMap

Community
  • 1
  • 1
Luis Sep
  • 2,384
  • 5
  • 27
  • 33
  • using interface type for allprofile throws this (as hibernate is bad with interfaces): Could not determine type for: java.util.Map, at table: profiles_table, for columns: [org.hibernate.mapping.Column(all_profiles) – XioRcaL Jan 31 '13 at 15:54