【问题标题】:How do I map a set of string objects using JPA Annotations?如何使用 JPA 注释映射一组字符串对象?
【发布时间】:2010-10-29 14:55:45
【问题描述】:
@Entity
public class TestClass implements Serializable{
    private Integer id;
    private Set<String> mySet;

    @Id
    @GeneratedValue
    public Integer getId() {
        return id;
    }
    @OneToMany(cascade={CascadeType.ALL})
    public Set<String> getMySet() {
        return mySet;
    }
}

我收到以下错误。

Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: TestClass.mySet[java.lang.String]

或者如果我离开@OneToMany

org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: test_class, for columns: [org.hibernate.mapping.Column(my_sets)]

【问题讨论】:

  • Hibernate 的 @CollectionOfElements 注释为我解决了这个问题。它是特定于休眠的,但我不打算换掉我的 JPA 实现。

标签: java hibernate jpa


【解决方案1】:

here 你会找到一个相当不错的答案。列表的规则也适用于集合。

【讨论】:

    【解决方案2】:

    哦哦,我必须这样做。

    @CollectionOfElements(targetElement = String.class)
    

    【讨论】:

    • 这也是我必须做的,但它是特定于休眠的。在我的情况下,这对我来说很好。
    • @ElementCollection 在 JPA2 中,这个答案是更好的方法。见stackoverflow.com/questions/287201/…
    猜你喜欢
    • 2010-10-22
    • 2012-07-28
    • 1970-01-01
    • 2023-03-22
    • 2011-02-25
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多