【问题标题】:JPA: Store a list of integers in a single fieldJPA:将整数列表存储在单个字段中
【发布时间】:2012-01-30 14:43:33
【问题描述】:

是否可以使用标准 JPA 2 在相应实体表的单个字段中存储整数列表?

@Entity
@Table(name="tbl_myentities")
public class MyEntity {

@ElementaryCollection
@Column(name="vals") // in table tbl_myentities
private List<Integer> vals;

【问题讨论】:

    标签: java spring-boot jpa-2.0


    【解决方案1】:

    不能在单个字段中存储多个值。将它们存储在单个字段中的原因是什么?

    一种方法是使用 String 类型的字段并将所有整数添加到逗号分隔的列表中,然后在 getter 和 setter 中加入/分解:

    private String vals;
    
    public setVals(int vals[])
    {
         // this.vals = Iterate vals[] and create a comma separated string
    }
    
    public int[] getVals()
    {
        // vals.split(",") to get a list of Strings, then typecast/parse them to ints before returning
    }
    

    使用@ElementCollection 注释和@CollectionTable 来控制映射需要一个单独的表来存储值。

    @ElementCollection
    private Collection<Integer> integers;
    

    http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection上阅读有关元素集合的更多信息

    这里有类似的问题Does JPA @ElementCollection annotation always produce an one-to-many relationship?

    【讨论】:

    • 它说,“ElementCollection 值始终存储在单独的表中。”
    • 我认为这不是 OP 想要的。
    【解决方案2】:

    您可以创建一个转换器并将其与注解@Converter 一起使用。

    此转换器必须实现 AttributeConverter,这是一个具有两个方法 convertToDatabaseColumn 和 convertToEntityAttribute 的通用接口。

    它很容易使用,你可以在这里查看:jpa independent custom type mapping / javax.persistence.x alternative to org.hibernate.annotations.Type and org.hibernate.annotations.TypeDef

    【讨论】:

      【解决方案3】:

      您可以将所有 val 存储在一个字符串字段中,用逗号分隔,然后像这样更改关联的 getter 和 setter:

      public List<Integer> getVals() {
          List<Integer> lstVals = new ArrayList<Integer>();
          int val = 0;
      
          for(String field : this.vals.split(",")) {
              try {
                  val = Integer.parseInt(field);
              }
              // If the String contains other thing that digits and commas
              catch (NumberFormatException e) {
              }
              lstVals.add(val);
          }
      
          return lstVals;
      }
      
      public void setVals(List<Integer> vals) {
          String newVals = "";
          for(int i : vals) {
              newVals.concat(String.valueOf(i));
          }
          this.vals = newVals;
      }
      

      【讨论】:

        【解决方案4】:

        我认为这是不可能的。因为您不能在数据库表中拥有一个允许您存储整数列表的列。

        你可以做的是使用字符串类型字段而不是整数列表 -

        @Column(name="vals") // in table tbl_myentities
        private String vals;
        

        并在保存实体之前和读取实体之后手动从整数列表转换为字符串。

        【讨论】:

          【解决方案5】:

          也许@Lob 适合你? (尽管它暗示)

          @Lob
          ArrayList<String> vals;
          

          (请注意,您的集合必须明确地是一个 ArrayList)

          【讨论】:

          • @Wingie 您的 getter/setter 是否也明确用于 ArrayList ?
          • 出现以下错误:class=java.lang.ClassCastException, error=java.util.ArrayList cannot be cast to java.sql.Blob 不要认为这个解决方案有效。
          猜你喜欢
          • 1970-01-01
          • 2020-11-01
          • 2013-11-09
          • 1970-01-01
          • 1970-01-01
          • 2018-09-23
          • 1970-01-01
          • 2021-12-09
          • 1970-01-01
          相关资源
          最近更新 更多