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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/921517/
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-08-11 21:06:38  来源:igfitidea点击:

How do I map a set of string objects using JPA Annotations?

javahibernatejpa

提问by ScArcher2

@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;
    }
}

I get the following error.

我收到以下错误。

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

or if I leave off the @OneToMany

或者如果我离开@OneToMany

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

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

采纳答案by Cogsy

You'll find a pretty decent answer here. The rules for Lists apply to Sets too.

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

回答by Joshua

Ooh oh, I had to do this one.

哦哦,我不得不做这个。

@CollectionOfElements(targetElement = String.class)