I'm attempting to upgrade to Micronaut 3.2, but starting from 3.1 some write operations on the db started failing. I have created a sample project to showcase this: https://github.com/dpozinen/optimistic-lock
furthermore I have created an issue at https://github.com/micronaut-projects/micronaut-data/issues/1230
Briefly, my entities:
@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 {}
Pretty basic setup. The exception occurs when I'm deleting the question from the game:
Game game = gameRepository.findById(gameId).orElseThrow()
Question question = questionRepository.findByGameAndId(gameId, questionId).orElseThrow()
game.getQuestions().remove(question)
gameRepository.saveAndFlush(game) // optional
Expected result: the question gets detached from the game and deleted, cascading the deletion to answerOptions. This was working up until Micronaut 3.0.3, you can change the version in the sample project and the test will succeed.
Actual result:
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 is the SQL that gets executed, notice the version numbers getting messed up. Any help would be greatly appreciated.
Edit 1:
Problem occurs only when applying ImageSingleChoiceQuestion#setValidAnswer with an instance that is also used in Question#setAnswerOptions
why is that the case, since this worked in Micronaut 3.0.3?
Edit 2:
- verified to be working on spring
Edit 3:
- confirmed as bug and fixed in PR