該賞金過期3天。此問題的答案有資格獲得 100聲望獎勵。 see303正在尋找信譽良好的來源的答案。
我正在嘗試升級到 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=?
這是執行的 SQL,注意版本號變得混亂。任何幫助將不勝感激。
編輯1:
僅當將 ImageSingleChoiceQuestion#setValidAnswer 與在 Question#setAnswerOptions 中使用的實體一起應用時才會出現問題
為什么會這樣,因為這在 Micronaut 3.0.3 中有效?
編輯2:
- 經驗證可以在 spring上作業
編輯3:
- 確認為錯誤并在PR 中修復
uj5u.com熱心網友回復:
您遇到了 Micronaut Data 3.1 中引入的錯誤。在您的情況下,版本屬性不正確地增加。Micronaut 資料團隊已確認此錯誤,并且有一個待處理的拉取請求(請參閱PR)可以解決此問題。
這是關于io.micronaut.data.runtime.event.listeners.VersionGeneratingEntityEventListener錯誤地增加 JP A 物體的版本屬性(請參閱代碼指標)。
我認為您可以期待即將發布的 Micronaut 版本 3.2.2 中的修復
uj5u.com熱心網友回復:
更新 正如@saw303 正確指出的那樣,這是 Micronaut Data 版本 <= 3.2.0 中的一個錯誤。該錯誤已在 io.micronaut.data:micronaut-data-hibernate-jpa:3.2.1 中修復,該錯誤于 2021-12-07 發布(今天撰寫本文時)。
https://github.com/micronaut-projects/micronaut-data/releases
我用這個變化更新了我的 PR。
我能夠使用您的存盤庫中的代碼重現該問題。Micronaut資料變更日志顯示v3.1.0有相關變更。 https://github.com/micronaut-projects/micronaut-data/releases
僅當將 ImageSingleChoiceQuestion#setValidAnswer 與也在 Question#setAnswerOptions 中使用的實體一起應用時,才會出現問題。
因此問題做這樣的時候就會消失:
question.setValidAnswer(new AnswerOption());。說明:當在一對多和一對一中使用相同的 AnswerOption 實體時,Hibernate 會嘗試洗掉兩個地方的實體。
其原因似乎是 CascadeType.ALL。從類問題中洗掉它時,測驗通過。
以下解決方案通過了測驗,但目前我無法解釋為什么它基于生成的 SQL 作業。這種方法需要一些進一步的調查。
@Getter
@Setter
@ToString
@Entity(name = "ImageSingleChoiceQuestion")
@Table(name = "image_single_choice_question")
public class ImageSingleChoiceQuestion extends Question {
@OneToOne
@PrimaryKeyJoinColumn
private AnswerOption validAnswer;
}
迄今為止通過測驗的最佳可解釋解決方案是將@OneToOne 替換為@OneToMany,如下所示。Hibernate 將使用這種方法創建連接表image_single_choice_question_valid_answers。這當然不是最優的。
@Getter
@Setter
@ToString
@Entity(name = "ImageSingleChoiceQuestion")
@Table(name = "image_single_choice_question")
public class ImageSingleChoiceQuestion extends Question {
@OneToMany
private Set<AnswerOption> validAnswers = new HashSet<>();
public AnswerOption getValidAnswer() {
return validAnswers.stream().findFirst().orElse(null);
}
public void setValidAnswer(final AnswerOption answerOption) {
validAnswers.clear();
validAnswers.add(answerOption);
}
}
公關在這里:https : //github.com/dpozinen/optimistic-lock/pull/1
我將 testcontainers 添加到您的專案中,現在測驗類如下所示:
package optimistic.lock
import optimistic.lock.answeroption.AnswerOption
import optimistic.lock.image.ImageSingleChoiceQuestion
import optimistic.lock.testframework.ApplicationContextSpecification
import optimistic.lock.testframework.testcontainers.MariaDbFixture
import javax.persistence.EntityManager
import javax.persistence.EntityManagerFactory
class NewOptimisticLockSpec extends ApplicationContextSpecification
implements MariaDbFixture {
@Override
Map<String, Object> getCustomConfiguration() {
[
"datasources.default.url" : "jdbc:mariadb://localhost:${getMariaDbConfiguration().get("port")}/opti",
"datasources.default.username": getMariaDbConfiguration().get("username"),
"datasources.default.password": getMariaDbConfiguration().get("password"),
]
}
GameRepository gameRepository
QuestionRepository questionRepository
TestDataProvider testDataProvider
GameTestSvc gameTestSvc
EntityManagerFactory entityManagerFactory
EntityManager entityManager
@SuppressWarnings('unused')
void setup() {
gameRepository = applicationContext.getBean(GameRepository)
questionRepository = applicationContext.getBean(QuestionRepository)
testDataProvider = applicationContext.getBean(TestDataProvider)
gameTestSvc = applicationContext.getBean(GameTestSvc)
entityManagerFactory = applicationContext.getBean(EntityManagerFactory)
entityManager = entityManagerFactory.createEntityManager()
}
void "when removing question from game, game has no longer any questions"() {
given:
def testGame = testDataProvider.saveTestGame()
and:
Game game = gameRepository.findById(testGame.getId()).orElseThrow()
and:
Question question = testGame.questions.first()
assert question != null
and:
def validAnswer = ((ImageSingleChoiceQuestion)question).getValidAnswer()
assert validAnswer != null
when:
gameTestSvc.removeQuestionFromGame(game.getId(), question.getId())
then:
noExceptionThrown()
and:
0 == entityManager.find(Game.class, game.getId()).getQuestions().size()
and:
null == entityManager.find(AnswerOption.class, validAnswer.getId())
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/375403.html
標籤:休眠 jpa 微型宇航员 micronaut-数据
