gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:15:17 27 4
gpt4 key购买 nike

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

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

最佳答案

错误表明 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.

关于java - 非法尝试将非集合映射为 @OneToMany、@ManyToMany 或 @CollectionOfElements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40822559/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com