gpt4 book ai didi

spring - SQL 错误 : 23503, SQLState : 23503 in SpringBoot 2. 1.4.RELEASE 应用程序

转载 作者:行者123 更新时间:2023-12-03 21:40:22 26 4
gpt4 key购买 nike

我有一个 SpringBoot 2.1.4.RELEASE RESTful Web 服务应用程序,使用 Spring Initializer、嵌入式 Tomcat、Thymeleaf 模板引擎,并打包为可执行 JAR 文件。

我有这门课:

@Entity
@Table(name="t_menu_alert_notification")
public class MenuAlertNotification implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;


public MenuAlertNotification() {
super();
}


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonProperty("id")
private Long id;

@JsonProperty("subject")
protected String subject;

@JsonIgnore
protected Integer trend;

@JsonIgnore
protected String message;

@JsonProperty("notified")
private Boolean notified;

@Column(name = "is_read")
protected Boolean read;

@JsonProperty("creationDate")
@Convert(converter = LocalDateTimeAttributeConverter.class)
protected LocalDateTime creationDate;


@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "menu_alert_id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="name")
@JsonIdentityReference(alwaysAsId=true)
protected MenuAlert menuAlert;
..
}

存储库中的这个方法:

  @Transactional(propagation =Propagation.REQUIRED,
isolation=Isolation.SERIALIZABLE,
readOnly=false,
transactionManager="transactionManager")
@Modifying
@Query("update MenuAlertNotification n set n.read = :status where n.id in :notificationIdList and n.menuAlert.menu.user.id = :userId")
void changemenuNotificationListReadStatus( @Param("notificationIdList") List<Long> notificationIdList,
@Param("userId") long userId,
@Param("status") boolean status);

我已经创建了一个 Junit 测试:

MenuAlertNotification menuAlertNotification = new MenuAlertNotification (menuAlert);        
menuAlertNotificationService.save(menuAlertNotification);
assertFalse (menuAlertNotification.getRead());
Long menuAlertNotificationId = menuAlertNotification.getId();
List<Long> notificationIdList = new ArrayList <Long>();
notificationIdList.add (menuAlertNotificationId);

menuAlertNotificationService
.changeMenuNotificationReadStatus (notificationIdList, user.getId(), Boolean.TRUE);

当我保存对象时,一切都很好,但是当我调用方法 changeMenuNotificationReadStatus 时,我得到了这个错误:

019-04-16 11:21  [main] WARN  o.h.e.jdbc.spi.SqlExceptionHelper.logExceptions(137) - SQL Error: 23503, SQLState: 23503
2019-04-16 11:21 [main] ERROR o.h.e.jdbc.spi.SqlExceptionHelper.logExceptions(142) - Referential integrity constraint violation: "FK56KKTN0YV9SJJIOJJVJBPAGNW: PUBLIC.T_MENU_ALERT_NOTIFICATION FOREIGN KEY(MENU_ALERT_ID) REFERENCES PUBLIC.T_MENU_ALERT(ID) (1)"; SQL statement:
delete from t_menu_alert where id=? [23503-197]

最佳答案

当您执行 MenuAlertNotification menuAlertNotification = new MenuAlertNotification (menuAlert); 以及 Long menuAlertNotificationId = menuAlertNotification.getId(); 时,不会生成 ID 这将始终返回

您应该将 junit 测试的第二行更改为

menuAlertNotification = menuAlertNotificationService.save(menuAlertNotification);

我假设您的服务 menuAlertNotificationService.save(menuAlertNotification); 返回类似于 return notificationRepo.save(entity) 的内容。

通过这种方式,您可以在插入行后用 ID 填充新对象,因此不会得到规定的异常。

关于spring - SQL 错误 : 23503, SQLState : 23503 in SpringBoot 2. 1.4.RELEASE 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55705001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com