XioRcaL
XioRcaL

Reputation: 673

how to store a Map<String, List<String>> using JPA

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

Upvotes: 5

Views: 2970

Answers (3)

XioRcaL
XioRcaL

Reputation: 673

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;

Upvotes: 1

K.C.
K.C.

Reputation: 2112

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

Did you try this:

@CollectionOfElements
private Map<String, List<String>> allProfiles;

Upvotes: 1

Luis Sep
Luis Sep

Reputation: 2402

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

Upvotes: 0

Related Questions