java 非法尝试将非集合映射为 @OneToMany、@ManyToMany 或 @CollectionOfElements

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

Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements

javamysqlhibernatehibernate-mappingjpa-2.1

提问by henrycharles

I have one lawyer table which is having id(int) as a primary key and Country table having country_code(String ) as a primary key. I want to create third table using @JoinTable annotation in hibernate with two foreign key in it. But when I run it following error is coming. Not sure how to map one string and one int as foreign keys in third table.

我有一个以 id(int) 作为主键的律师表和以 country_code(String ) 作为主键的 Country 表。我想在 hibernate 中使用 @JoinTable 注释创建第三个表,其中有两个外键。但是当我运行它时会出现以下错误。不确定如何将一个字符串和一个 int 映射为第三个表中的外键。

Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.test.common.entities.Country.lawyer

This is my code

这是我的代码

@Entity
@Table(name = "lawyer")
public class Lawyer {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "lawyer_batch_no")
    private int lawyerbatchNo;

@ManyToOne(targetEntity = Country.class, cascade = { CascadeType.ALL })
    @JoinTable(name = "lawyer_cscd", joinColumns = {
            @JoinColumn(name = "lawyer_batch_no", referencedColumnName = "lawyer_batch_no") }, inverseJoinColumns = {
                    @JoinColumn(name = "country_code", referencedColumnName = "country_code") })
    private Country country;

getter setter...
}

@Entity
@Table(name = "country")
public class Country {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "country_code")
    protected String country_code;

    @Column(name = "abbreviation")
    protected String abbreviation;

    @Column(name = "name", nullable = false)
    protected String name;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "country")
    protected Set<State> state = new HashSet<State>();

    @OneToMany(targetEntity = Lawyer.class, cascade = { CascadeType.ALL }, orphanRemoval = true)
    @JoinTable(name = "lawyer_cscd", joinColumns = {
            @JoinColumn(name = "country_code", referencedColumnName = "country_code") }, inverseJoinColumns = {
                    @JoinColumn(name = "lawyer_batch_no", referencedColumnName = "lawyer_batch_no") })
    private Lawyer lawyer;

getter setter....
}

回答by baao

The error indicates that private Lawyer lawyerneeds to be a collection as it's a @OneToManyrelationship. In the Countryclass, the last relationship should be

该错误表明它private Lawyer lawyer需要是一个集合,因为它是一个@OneToMany关系。在Country类中,最后一个关系应该是

@OneToMany(targetEntity = Lawyer.class, cascade = { CascadeType.ALL }, orphanRemoval = true)
@JoinTable(name = "lawyer_cscd", joinColumns = {
    @JoinColumn(name = "country_code", referencedColumnName = "country_code") }, inverseJoinColumns = {
    @JoinColumn(name = "lawyer_batch_no", referencedColumnName = "lawyer_batch_no") })
private Set<Lawyer> lawyer;
// or a Collection/List/etc.