我有一个律师表,其中 id(int) 作为主键,而 Country 表则以 Country_code(String ) 作为主键。我想在 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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)