非法尝试将非集合映射为@ OneToMany,@ ManyToMany或@CollectionOfElements [英] Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements

查看:73
本文介绍了非法尝试将非集合映射为@ OneToMany,@ ManyToMany或@CollectionOfElements的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个律师表,其中id(int)作为主键,而Country表中具有country_code(String)作为主键.我想在休眠中使用@JoinTable批注创建第三个表,并在其中添加两个外键.但是当我运行它时,下面的错误来了.不确定如何将一个字符串和一个int映射为第三张表中的外键.

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.

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

这是我的代码

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

推荐答案

该错误表明私人律师律师必须是集合,因为它是 @OneToMany 关系.在 Country 类中,最后一个关系应该是

The error indicates that private Lawyer lawyer needs to be a collection as it's a @OneToMany relationship. In the Country class, the last relationship should be

@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.

这篇关于非法尝试将非集合映射为@ OneToMany,@ ManyToMany或@CollectionOfElements的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆