【问题标题】:Persist collection fields with hibernate使用 hibernate 持久化集合字段
【发布时间】:2010-11-24 12:58:18
【问题描述】:

使用 hibernate,我如何使用 List<String> 字段持久化一个类?

考虑以下实体类:

@Entity
public class Blog {
    private Long id;
    private List<String> list;

    @Id
    @GeneratedValue
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public List<String> getList() { return list; }
    public void setList(List<String> list) { this.list = list; }
}

但是,当我尝试保存它时,我收到以下错误:

[INFO] An exception occured while executing the Java class. null

Could not determine type for: java.util.List, at table: Blog, for columns: [org.hibernate.mapping.Column(list)]

我尝试将“@CollectionOfElements”添加到getList(),但只有id 被保存到库中。没有为列表创建相应的列。

注意:我只是在尝试 Hibernate,所以我可以使用文档链接,我们将帮助我了解 Hibernate 中的集合关系管理

【问题讨论】:

    标签: java hibernate jpa persistence


    【解决方案1】:

    使用Serializable 对象似乎效果更好。将list 属性更改为ArrayList&lt;String&gt; 似乎可以解决问题。

    【讨论】:

      【解决方案2】:

      看看This。也许有帮助。

      您是否按如下方式应用了@CollectionOfElements?

      @org.hibernate.annotations.CollectionOfElements(
      targetElement = java.lang.String.class
      

      )

      【讨论】:

      • 谢谢!创建新表以保存映射
      【解决方案3】:

      看看Hibernate Annotations Documentation about Collections基本上你必须告诉列表它代表什么关系。

      @OneToMany(mappedBy="blog")
      public List<String> getList() { return list; }
      

      那么它应该可以工作了。

      【讨论】:

      • 我会得到以下错误:'@OneToOne or @ManyToOne on uk.co.pookey.hibernate.model.Blog.list 引用了一个未知实体:java.util.List
      • 这应该可以。您是否导入了 Hibernate 或 JPA 注释?
      • 导入的 JPA javax.persistence 注解
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 2010-09-24
      • 2013-11-12
      相关资源
      最近更新 更多