我在@OneToMany hibernate映射中遇到错误?

时间:2016-11-08 09:45:02

标签: java hibernate spring-boot annotations hibernate-mapping

我试图在两个类之间建立一对多的连接,但我得到了这个错误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;

4 个答案:

答案 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