How to persist a List of Strings in Hibernate?

2022-08-31 16:53:37

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

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

Regards


答案 1

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


答案 2

try

  @org.hibernate.annotations.CollectionOfElements(
        targetElement = java.lang.String.class
    )
    @JoinTable(
        name = "foo",
        joinColumns = @JoinColumn(name = "foo_id")
    )
    @org.hibernate.annotations.IndexColumn(
        name = "POSITION", base = 1
    )
    @Column(name = "baz", nullable = false)
    private List<String> arguments = new ArrayList<String>();

or see this detail example