spring-data-jpa Spring JPA / Hibernate -持久化到H2工作正常,但在Oracle中失败,并显示ORA-01400“无法插入NULL”

z0qdvdin  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(133)

我有两个Oracle表,它们之间的关系为1:M:

EVENT(ID, NAME, DATE)

EVENT_LOG(ID, EVENT_ID, COMPLETED)

EVENT的PK=ID
EVENT_LOG在事件ID上具有PK=ID和FK=事件ID
它们对应的JPA实体定义如下:

@Entity
@Table(name = "EVENT")
public class EventEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "ID")
    private Long id;

    @OneToMany(mappedBy = "event", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<EventLogEntity> eventLogs;

    ...
}

@Entity
@Table(name = "EVENT_LOG")
public class EventLogEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "ID")
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "EVENT_ID")
    private EventEntity event;

}

持久化是通过以下方法完成的:

private EventEntity saveEvent(EventDto eventDto) {

    EventEntity eventEntity = new EventEntity();

    ModelMapper modelMapper = new ModelMapper();
    modelMapper.map(eventDto, eventEntity);

    List<EventLogEntity> eventLogs = new ArrayList<EventLogEntity>();

    EventLogEntity eventLogEntity = new EventLogEntity();
    eventLogEntity.setCompleted(0); //0-not-completed, 1-completed
    eventLogs.add(eventLogEntity);

    eventEntity.setEventtLogs(eventLogs);

    EventEntity storedEvent = eventRepository.saveAndFlush(eventEntity);

    return storedEvent;
}

更新

EVENT和EVENT_LOG数据按其应有的方式持久化,我得到的数据如下:

EVENT(ID, NAME, DATE) = (1, 'send_msg', 20220210T11:55:10)

EVENT_LOG(ID, EVENT_ID, COMPLETED) = (1, 1, 0) // 0 = not sent

这一切都很好,但之后我将收到send_msg事件的响应,我需要插入新的EVENT_LOG插入,以便成功发送,该插入应如下所示:

EVENT_LOG(ID, EVENT_ID, COMPLETED) = (1, 1, 1) // 1 = sent

这是由另一个名为saveEventLog的方法完成的:

private EventLog saveEventLog(EventEntity eventEntity) {

    EventLog eventLogEntity = new EventLog();
    eventLogEntity.setIsSent(1);
    eventLogEntity.setEvent(eventEntity); //WAS MISSING THIS LINE WHICH LED TO ERROR ORA-01400
    EventLog storedEventLog = eventLogRepository.saveAndFlush(eventLogEntity); 

    if (storedEventLog == null) {
        throw new Exception(...);
    }

    return storedEventLog;
}

问题

如果我使用H2数据库,我的实体将被持久化,如下所示:

EVENT(ID, NAME, DATE) = (1, 'send_msg', 20220210T11:55:10)

EVENT_LOG(ID, EVENT_ID, COMPLETED) = (1, 1, 0)    //1ST ENTRY
EVENT_LOG(ID, EVENT_ID, COMPLETED) = (1, null, 1) //2ND ENTRY

上述EVENT_ID的NULL值不正确,应为1。此问题已通过上述缺少的行得到修复,不再需要关注
但是,在Oracle中,我收到错误ORA-01400:无法将NULL插入到(“MYDB”.“EVENT_LOG”.“EVENT_ID”)中,并且此错误发生在对saveEventLog的调用中此错误也已通过上面缺少的行修复,不再需要关注
上述2个问题的原因是缺少设置EVENT到EVENT_LOG连接的行:eventLogEntity.setEvent(eventEntity) .
添加了这个之后,它工作得很好。

但是,我想了解为什么在H2上,EVENT_ID中出现NULL且没有错误,而在Oracle中,出现ORA-01400。我尝试在H2上设置MODE=Oracle以使它们的行为一致,但仍然可以将记录插入到H2中,没有任何问题。

fslejnso

fslejnso1#

我认为您又一次遗漏了为EventLogEntity设置EventEntity,应该这样做:

EventLogEntity eventLogEntity = new EventLogEntity();
eventLogEntity.setCompleted(1);
eventLogEntity.setEventEntityDetails(eventEntity);
eventLogs.add(eventLogEntity);

相关问题