【发布时间】:2022-01-06 05:26:13
【问题描述】:
我正在尝试升级到 Micronaut 3.2,但从 3.1 开始,数据库上的一些写入操作开始失败。我创建了一个示例项目来展示这一点:https://github.com/dpozinen/optimistic-lock
此外,我在https://github.com/micronaut-projects/micronaut-data/issues/1230创建了一个问题
简单地说,我的实体:
@MappedSuperclass
public abstract class BaseEntity {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
@Column(updatable = false, nullable = false, length = 36)
@Type(type = "optimistic.lock.extra.UuidUserType")
private UUID id;
@Version
@Column(nullable = false)
private Integer version;
}
public class Game extends BaseEntity {
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@ToString.Exclude
@OrderColumn(name = "sort_index")
private List<Question> questions = new ArrayList<>();
}
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "question")
public abstract class Question extends BaseEntity {
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@ToString.Exclude
@OrderColumn(name = "sort_index")
private List<AnswerOption> answerOptions = new ArrayList<>();
}
public class ImageSingleChoiceQuestion extends Question {
@OneToOne(cascade = CascadeType.ALL)
private AnswerOption validAnswer;
}
@Table(name = "answer_option")
public class AnswerOption extends BaseEntity {}
相当基本的设置。当我从游戏中删除问题时出现异常:
Game game = gameRepository.findById(gameId).orElseThrow()
Question question = questionRepository.findByGameAndId(gameId, questionId).orElseThrow()
game.getQuestions().remove(question)
gameRepository.saveAndFlush(game) // optional
预期结果:question 与 game 分离并删除,将删除级联到 answerOptions。一直工作到Micronaut 3.0.3,您可以在示例项目中更改版本,测试会成功。
实际结果:
javax.persistence.OptimisticLockException:
Batch update returned unexpected row count from update [0];
actual row count: 0; expected: 1;
statement executed: delete from question where id=? and version=?
Here 是被执行的 SQL,注意版本号被弄乱了。任何帮助将不胜感激。
编辑 1:
仅在将 ImageSingleChoiceQuestion#setValidAnswer 与同样用于 Question#setAnswerOptions 的实例应用时才会出现问题
为什么会这样,因为这在 Micronaut 3.0.3 中有效?
编辑 2:
编辑 3:
- 确认为错误并在PR中修复
【问题讨论】:
-
嗨。使用 Micronaut Data 3.2.1,我的测试与您的原始代码一起通过。感谢您的合作。 BR
标签: hibernate jpa micronaut micronaut-data