这是我的文件类
@Entity
@Table(name = "file")
public class File{
public File() {
}
public File(String file_id,String file_path,Date start_time,Date end_time) {
this.file_id=file_id;
this.file_path=file_path;
this.start_time=start_time;
this.end_time=end_time;
}
@Id
private String file_id;
private String file_path;
private Date start_time;//time
private Date end_time;//time
@JsonBackReference(value = "MainScheduleHasFile-File")
@OneToMany(mappedBy = "file",cascade = {CascadeType.ALL})
private Set<MainScheduleHasFile> mainScheduleHasFiles = new HashSet<>();
//getters and setters and other attributes
这是我的mainSchedule类
@Entity
@Table(name = "main_schedule")
public class MainSchedule{
public MainSchedule() {
}
public MainSchedule(String scheduleId,double version) {
this.scheduleId=scheduleId;
this.version=version;
}
@Id
private String scheduleId;
private double version;
@JsonBackReference(value = "MainScheduleHasFile-MainSchedule")
@OneToMany(mappedBy = "mainSchedule",cascade = { CascadeType.ALL })
private Set<MainScheduleHasFile> mainScheduleHasFile;
//getters and setters and other attributes
我的mainSchedulePlayFile类
@Entity
@Table(name = "main_schedule_play_file")
public class MainSchedulePlayFile implements Serializable{
public MainSchedulePlayFile() {
}
public MainSchedulePlayFile(MainSchedule mainSchedule, File file) {
this.mainSchedule=mainSchedule;
this.file=file;
}
@Id
@ManyToOne
@JoinColumn(name = "schedule_id")
private MainSchedule mainSchedule;
@Id
@ManyToOne
@JoinColumn(name = "file_id")
private File file;
private Integer playPoint;
//getters and setters and other attributes
数据插入逻辑
MainSchedulePlayFile mSchFile=new MainSchedulePlayFile();
Set<MainSchedulePlayFile> set=new HashSet<>();
MainSchedule m=mainScheduleRepo.findById(mid).get();
mSchFile.setMainSchedule(m);
mSchFile.setFile(fileRepo.findById(fid).get());
mSchFile.setPlayPoint(1);
set.add(mSchFile);
m.setMainSchedulePlayFile(set);
mainScheduleRepo.save(m);
这是错误。
20-03-19 Thu 00:39:53.532 DEBUG SQL select mainschedu0_.schedule_id as schedule2_4_0_, mainschedu0_.file_id as file_id3_4_0_, mainschedu0_.play_point as play_poi1_4_0_ from main_schedule_has_file mainschedu0_ where mainschedu0_.schedule_id=? and mainschedu0_.file_id=?
Hibernate: select mainschedu0_.schedule_id as schedule2_4_0_, mainschedu0_.file_id as file_id3_4_0_, mainschedu0_.play_point as play_poi1_4_0_ from main_schedule_has_file mainschedu0_ where mainschedu0_.schedule_id=? and mainschedu0_.file_id=?
20-03-19 Thu 00:39:53.533 TRACE BasicBinder binding parameter [1] as [VARCHAR] - [s001]
20-03-19 Thu 00:39:53.534 TRACE BasicBinder binding parameter [2] as [VARCHAR] - [f001]
20-03-19 Thu 00:39:53.598 DEBUG SQL insert into main_schedule_has_file (play_point, schedule_id, file_id) values (?, ?, ?)
Hibernate: insert into main_schedule_has_file (play_point, schedule_id, file_id) values (?, ?, ?)
20-03-19 Thu 00:39:53.604 TRACE BasicBinder binding parameter [1] as [INTEGER] - [1]
20-03-19 Thu 00:39:53.605 TRACE BasicBinder binding parameter [2] as [VARCHAR] - [null]
20-03-19 Thu 00:39:53.606 TRACE BasicBinder binding parameter [3] as [VARCHAR] - [null]
20-03-19 Thu 00:39:53.639 WARN SqlExceptionHelper SQL Error: 1048, SQLState: 23000
20-03-19 Thu 00:39:53.639 ERROR SqlExceptionHelper Column 'schedule_id' cannot be null
20-03-19 Thu 00:39:53.670 ERROR [dispatcherServlet] Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
java.sql.SQLIntegrityConstraintViolationException: Column 'schedule_id' cannot be null
当我尝试将数据添加到多对多关系时,此错误就会出现 向上。如何解决呢?我试图在其中添加一个额外的列的数据 关系表,但是如果有人可以解决此问题,则会出现错误 请帮助我!
答案 0 :(得分:0)
我解决了。 数据插入逻辑应该是。
MainSchedulePlayFile mSchFile=new MainSchedulePlayFile(); MainSchedule
m=mainScheduleRepo.findById(mid).get();
mSchFile.setMainSchedule(m);
mSchFile.setFile(fileRepo.findById(fid).get());
mSchFile.setPlayPoint(1);
m.getMainSchedulePlayFile().add(mSchFile);
fileRepo.save mainScheduleRepo.save(m);