Search code examples
javastringhibernatelistannotations

How to persist a List of Strings in Hibernate?


I seem to have problems with mapping a List in Hibernate. In our project there a class Card with contains a class Answer with Answer containing a List<String>.

Is a List<String> mappable by Hibernate using annotations? I mean, since it does not have the @Entity annotation?

Regards


Solution

  • Use @ElementCollection:

    @ElementCollection
    @CollectionTable(name="Nicknames", joinColumns=@JoinColumn(name="user_id"))
    @Column(name="nickname")
    public List<String> getNicknames() { ... } 
    

    Source: 7.2.3. Collections of basic types and embeddable objects