event2work2instrumentation

event2work2instrumentation

我正在尝试将下表映射到实体。

java - 带有3个外键的 hibernate 表-LMLPHP
事件表的映射如下所示:

@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时,我得到:字段没有默认值。

如果我给字段默认值,我得到:

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的引用。

最佳答案

尝试截断两个表,然后存储新的FK。当我有一个object_related_id不在对象表中作为id时,这发生在我身上

10-08 02:04