我试图将下表映射到实体。
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer _id;
@Column(name = "title")
private String _title;
@Column(name = "description")
private String _description;
@Column(name = "location")
private String _place;
@Column(name = "start")
private Date _start;
@Column(name = "end")
private Date _stop;
@Column(name = "points")
private int _points;
@Enumerated(EnumType.STRING)
@Column(name = "type")
private EventType _eventtype;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "event2work2instrumentation", joinColumns = {
@JoinColumn(name = "event")},
inverseJoinColumns = {@JoinColumn(name = "work")})
private Set<WorkMapper> _works = new HashSet<>(0);
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "event2work2instrumentation", joinColumns = {
@JoinColumn(name = "event")},
inverseJoinColumns = {@JoinColumn(name = "instrumentation")})
private Set<InstrumentationMapper> _instrumentations = new HashSet<>(0);
它工作正常,我从数据库中获取所有数据,但是当我尝试存储一个分配了工作和工具的新事件时,我会收到错误。
当event2work2instrumentation表中的字段配置为不为null时,我得到:field没有默认值。
如果我给出字段默认值,我得到:
SQLIntegrityConstraintViolationException: Cannot add or update a child row:
a foreign key constraint fails (`schema`.`event2work2instrumentation`, CONSTRAINT `e2w2i_instrumentation_fk` FOREIGN KEY (`instrumentation`) REFERENCES `instrumentation` (`id`))
你有什么想法吗?它可能取决于仪器和/或工作的映射吗?这些类在其映射中没有对event2work2instrumentation的引用。
答案 0 :(得分:0)
尝试截断两个表,然后存储新的FK。当我有一个object_related_id并且在对象表中没有作为id
时,它发生在我身上答案 1 :(得分:0)
解决方案是使用map和MapKeyJoinColumn注释:
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "event2work2instrumentation", joinColumns = {
@JoinColumn(name = "event")},
inverseJoinColumns = {@JoinColumn(name = "instrumentation")})
@MapKeyJoinColumn(name = "work")
private Map<WorkMapper, InstrumentationMapper> _eventToWorkAndInstrumentationMappers = new HashMap<>();