How to persist a List of Strings in Hibernate?
If you are happy to store them in the same column as JSON (MySQL, PostgreSQL that also support indexing and binary JSON), you could simply use com.vladmihalcea/hibernate-types-52
library.
@TypeDefs({
@TypeDef(name = "json", typeClass = JsonType.class)
})
public class YourEntity {
@Column(nullable = false)
@Type(type = "json")
private List<String> fieldName = List.of();
...
}
See full guide here
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
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