如何使用JPA注释映射一组字符串对象?

时间:2009-05-28 15:32:17

标签: java hibernate jpa

@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)]

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:5)

哦,我必须这样做。

@CollectionOfElements(targetElement = String.class)