JPA复合主键/外键映射

时间:2018-11-20 02:51:33

标签: java hibernate jpa

我有2张桌子

enter image description here

然后我尝试使用This reference之类的@Embeddable进行映射

Week_Effort.java

@Entity 
@IdClass(Week_Effort.class)
@Table(name = "week_effort")
public class Week_Effort implements Serializable  {

private static final long serialVersionUID = -2825819221813101109L;

@Id
@Column(name="week_id") 
private int week_id;

@Id
@Column(name="timesheet_id")
private int timesheet_id;

@Column(name="day1")
private int day1;

@ManyToOne(fetch = FetchType.LAZY ,cascade=CascadeType.ALL)
@JoinColumn(name="timesheet_id" , referencedColumnName =     "timesheet_id",insertable=false, updatable=false , foreignKey =   @ForeignKey(name = "fkweekts"))
private Timesheet timesheet_id ;
.
.
.
//get and set
}

Timesheet.java

@Entity
@Table(name = "timesheet")
public class Timesheet implements Serializable{

private static final long serialVersionUID = 7014333990749288304L;

@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "timesheet_id")
private int timesheet_id;

@OneToMany(mappedBy="timesheet_id" , cascade=CascadeType.ALL)
private Set<Week_Effort> weekeffort ;
.
.
.
//get and set 

}

WeekEmbed.java

@Embeddable
public class WeekEmbed implements Serializable {

private static final long serialVersionUID = 3229945076116848141L;

int timesheet_id;
int week_id;

//get set

}

我使用AJAX将参数像json一样发布

{"timesheet_id":1,"day1":"8"} //and more 6 parameter

week_id是由某些进程生成的,然后在添加到Jparepository中的CRUD之前进行设置。但是会出错

java.sql.SQLException: Parameter index out of range (10 > number of parameters, which is 9)

如何解决此问题?我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

来自:Hibernate: Parameter index out of range (8 > number of parameters, which is 7)

我将@IdClass更改为WeekEmbed.class并删除某些部分,然后将@Id添加到对象映射。

Week_Effort.java

@Entity 
@IdClass(WeekEmbed.class)
@Table(name = "week_effort")
public class Week_Effort implements Serializable  {

private static final long serialVersionUID = -2825819221813101109L;

@Id
@Column(name="week_id") 
private int week_id;

//Delete this part
//@Id
//@Column(name="timesheet_id")
//private int timesheet_id;

@Column(name="day1")
private int day1;

@Id
@ManyToOne(fetch = FetchType.LAZY )
@JoinColumn(name="timesheet_id" , referencedColumnName = "timesheet_id",insertable=false, updatable=false , foreignKey = @ForeignKey(name = "fkweekts"))
private Timesheet timesheet_id ;
.
.
.
//get and set
}