我正在接收JSON请求的简单值列表部分,希望将其另保存为逗号分隔的值。尝试使用以下内容,但不起作用。
@Column(nullable = true)
@GeneratedValue(strategy = GenerationType.AUTO)
private ArrayList<String> services = new ArrayList<String>() ;
和
@Column(nullable = true)
@ElementCollection(targetClass = String.class)
private List<String> services = new ArrayList<String>() ;
@ElementCollection
掷回例外状况,表示table services does not exist
。
3条答案
按热度按时间svmlkihl1#
@ElementCollection需要一个表来存储多行值,
因此,您可以定义为String列,并在getter和setter中进行联接/分解,如下所示
nkkqxpd92#
正如其他人在评论中提到的,
AttributeConverter
工作得很好。这个使用Jackson序列化为JSON数组。我推荐JSON,因为它可以干净地处理分隔符转义、空值、引号等:我使用过这个类,它在大多数情况下都能很好地工作。我发现的一个警告是,使用这个转换器可能会混淆一些JPA条件查询,因为它在实体上期望类型List,但在数据库中却找到String。
ha5z0ras3#
一个更简单的变体对我来说很有用,没有Jackson,但修剪字符串: