我试图在两个类之间建立一对多的连接,但我得到了这个错误org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany
。
这是我的代码,
Job.java
@OneToMany
@JoinColumn(name = "id", referencedColumnName = "id", insertable = false, updatable = false)
private Set<JobCostSplit> jobCostSplit;
JobCostSplit.java
@Column(name = "job_id")
private Long jobId;
@JsonIgnore
@OneToMany
@JoinColumn(name = "job_id", referencedColumnName = "id", insertable = false, updatable = false)
private Job job;
答案 0 :(得分:2)
您必须在JobCostSplit中将该字段注释为@ManyToOne
@ManyToOne
private Job job
答案 1 :(得分:2)
您使用Job
为@OneToMany
添加注释。表示Job
上有一个属性,JobCostSplit
上有更多属性。
因此,如果您想要一个Job
以及更多JobCostSplit
,则必须将其设置为:
@ManyToOne
@JoinColumn(name = "job_id", referencedColumnName = "id", insertable = false, updatable = false)
private Job job;
答案 2 :(得分:1)
首先你需要写
@OneToMany(fetch = FetchType.LAZY, mappedBy = "job")
private Set<JobCostSplit> jobCostSplit= new HashSet<JobCostSplit>();
在Job.java中,然后添加
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="jobId",nullable=false)
private Job job;
在JobCostSplit.java
中答案 3 :(得分:0)
在job.java中用@ManyToOne替换@OneToMany