string 将实体中的列表转换为数据库中的单个字符串列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34048978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:28:37  来源:igfitidea点击:

Convert list in entity to single string column in database

stringhibernatearraylist

提问by Fra83

I have a VARCHARfield in my database, and the value of this field is val1,val2,val3.

VARCHAR我的数据库中有一个字段,该字段的值为val1,val2,val3.

Is it possible to set this into an ArrayList<String>attribute of an entity using comma as split delimiter?

是否可以ArrayList<String>使用逗号作为拆分分隔符将其设置为实体的属性?

回答by Tobias Liefke

If you use JPA 2.1, then you can create an AttributeConverter:

如果您使用 JPA 2.1,那么您可以创建一个AttributeConverter

@Converter
public class StringListConverter implements AttributeConverter<List<String>, String> {

  @Override
  public String convertToDatabaseColumn(List<String> list) {
    // Java 8
    return String.join(",", list); 
    // Guava
    return Joiner.on(',').join(list); 
  }

  @Override
  public List<String> convertToEntityAttribute(String joined) {
    return new ArrayList<>(Arrays.asList(joined.split(",")));
  }

}

You can use this converter in your entity:

您可以在您的实体中使用此转换器:

@Column
@Convert(converter = StringListConverter.class)
private List<String> strings;

For before JPA 2.1 you could do this by hand:

在 JPA 2.1 之前,您可以手动执行此操作:

@Entity
private MyEntity {
  ...
  private String strings;

  public List<String> getStrings() {
    return Arrays.asList(strings.split(","));
  }

  public void setStrings(List<String> list) {
    strings = String.join(",", list);
  }
}

I wrap Arrays.asListin an ArrayListin the converter, because the result is stored in the attribute and any change to that list will be written back to the database - thus I need a changeable list (I can't add anything to the result of Arrays.asList). In the before 2.1solution the result is not connected with the attribute and a changeable list would not be synchronized with the attribute.

Arrays.asListArrayList转换器中包装了一个,因为结果存储在属性中,对该列表的任何更改都将写回数据库 - 因此我需要一个可更改的列表(我无法向 的结果添加任何内容Arrays.asList)。在2.1 之前的解决方案中,结果与属性无关,并且可变列表不会与属性同步。

To query for an entity that contains a specific item in such an attribute, see my answer here

要查询在此类属性中包含特定项目的实体,请在此处查看我的答案

回答by Christian Beikov

Yes this is possible.

是的,这是可能的。

With Hibernate 4.3.x+ you can define an AttributeConverteralthough I am pretty sure this will not work for early Hibernate versions because of the Listtype. See this for an example: http://www.thoughts-on-java.org/jpa-21-how-to-implement-type-converter/

使用 Hibernate 4.3.x+,您可以定义一个,AttributeConverter尽管我很确定这不适用于早期的 Hibernate 版本,因为它的List类型。请参阅此示例:http: //www.thoughts-on-java.org/jpa-21-how-to-implement-type-converter/

The other way to make this work is by implementing a custom UserTypeand annotating the field/getter with org.hibernate.annotations.Type. Here is a nice write up of this with examples: http://blog.xebia.com/understanding-and-writing-hibernate-user-types/

使这项工作的另一种方法是实现自定义UserType并使用org.hibernate.annotations.Type. 这是一个很好的例子:http: //blog.xebia.com/understanding-and-writing-hibernate-user-types/

Yet another way which is JPA compatible is to have two fields, the Listannotated with javax.persistence.Transientand the Stringbut then you have manage the state synchronization between these two field in PrePersistand PreUpdatelisteners yourself. Here an example for using listeners: http://alexandregama.org/2014/03/23/entity-listeners-and-callback-methods-jpa/

然而,另一种方式是JPA兼容是有两个领域,List注解为javax.persistence.TransientString但你必须管理这两个领域之间的状态同步PrePersistPreUpdate听众自己。这是使用侦听器的示例:http: //alexandregama.org/2014/03/23/entity-listeners-and-callback-methods-jpa/

回答by Kalana Weerarathne

Try to use @ElementCollection and @CollectionTable annotations

尝试使用 @ElementCollection 和 @CollectionTable 注释