From 99250a2cb3568c665e3c2c35ee0584d7288a6292 Mon Sep 17 00:00:00 2001 From: Hideki Sugimoto Date: Mon, 23 Sep 2024 17:09:17 +0900 Subject: [PATCH] Fixed a bug in sqlAgent#merge, mergeAndReturn that failed to update Entity with @Version annotation. (v1.0.0) --- .../jp/co/future/uroborosql/SqlAgentImpl.java | 2 +- .../future/uroborosql/SqlEntityMergeTest.java | 45 +++++++++---------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/main/java/jp/co/future/uroborosql/SqlAgentImpl.java b/src/main/java/jp/co/future/uroborosql/SqlAgentImpl.java index d6094e74..e608219c 100644 --- a/src/main/java/jp/co/future/uroborosql/SqlAgentImpl.java +++ b/src/main/java/jp/co/future/uroborosql/SqlAgentImpl.java @@ -2316,7 +2316,7 @@ private E doMergeAndReturn(final E entity, final boolean locking) { return query.first() .map(findEntity -> { for (var mappingColumn : mappingColumns.values()) { - if (!mappingColumn.isId()) { + if (!mappingColumn.isId() && !mappingColumn.isVersion()) { var value = mappingColumn.getValue(entity); if (value != null) { mappingColumn.setValue(findEntity, value); diff --git a/src/test/java/jp/co/future/uroborosql/SqlEntityMergeTest.java b/src/test/java/jp/co/future/uroborosql/SqlEntityMergeTest.java index 8623cd48..d1b6c84f 100644 --- a/src/test/java/jp/co/future/uroborosql/SqlEntityMergeTest.java +++ b/src/test/java/jp/co/future/uroborosql/SqlEntityMergeTest.java @@ -39,7 +39,7 @@ void testEntityMergeWithId() throws Exception { .mapToObj(i -> new TestEntity() .setName("名前" + i) .setAddress(Optional.of("住所" + i)) - .setVersion(0)) + .setVersion(1)) .collect(Collectors.toList()); assertThat(agent.inserts(TestEntity.class, entities.stream()), is(9)); @@ -54,12 +54,12 @@ void testEntityMergeWithId() throws Exception { assertThat(result1.getId(), is(updateEntity.getId())); assertThat(result1.getName(), is(updateEntity.getName())); assertThat(result1.getAddress(), is(Optional.of("住所2_new"))); - assertThat(result1.getVersion(), is(updateEntity.getVersion() + 1)); + assertThat(result1.getVersion(), is(2)); var insertEntity = new TestEntity() .setName("名前10_new") .setAddress(Optional.of("住所10_new")) - .setVersion(0); + .setVersion(1); assertThat(agent.merge(insertEntity), is(1)); var result2 = agent.find(TestEntity.class, 10).orElse(null); @@ -87,7 +87,7 @@ void testEntityMergeAndReturnWithId() throws Exception { .mapToObj(i -> new TestEntity() .setName("名前" + i) .setAddress(Optional.of("住所" + i)) - .setVersion(0)) + .setVersion(1)) .collect(Collectors.toList()); assertThat(agent.inserts(TestEntity.class, entities.stream()), is(9)); @@ -100,12 +100,12 @@ void testEntityMergeAndReturnWithId() throws Exception { assertThat(result1.getId(), is(updateEntity.getId())); assertThat(result1.getName(), is(updateEntity.getName())); assertThat(result1.getAddress(), is(Optional.of("住所2_new"))); - assertThat(result1.getVersion(), is(updateEntity.getVersion() + 1)); + assertThat(result1.getVersion(), is(2)); var insertEntity = new TestEntity() .setName("名前10_new") .setAddress(Optional.of("住所10_new")) - .setVersion(0); + .setVersion(1); var result2 = agent.mergeAndReturn(insertEntity); assertThat(result2.getId(), is(10)); @@ -131,7 +131,7 @@ void testEntityMergeWithLockingWithId() throws Exception { .mapToObj(i -> new TestEntity() .setName("名前" + i) .setAddress(Optional.of("住所" + i)) - .setVersion(0)) + .setVersion(1)) .collect(Collectors.toList()); assertThat(agent.inserts(TestEntity.class, entities.stream()), is(9)); @@ -146,12 +146,12 @@ void testEntityMergeWithLockingWithId() throws Exception { assertThat(result1.getId(), is(updateEntity.getId())); assertThat(result1.getName(), is(updateEntity.getName())); assertThat(result1.getAddress(), is(Optional.of("住所2_new"))); - assertThat(result1.getVersion(), is(updateEntity.getVersion() + 1)); + assertThat(result1.getVersion(), is(2)); var insertEntity = new TestEntity() .setName("名前10_new") .setAddress(Optional.of("住所10_new")) - .setVersion(0); + .setVersion(1); assertThat(agent.mergeWithLocking(insertEntity), is(1)); var result2 = agent.find(TestEntity.class, 10).orElse(null); @@ -179,7 +179,7 @@ void testEntityMergeWithLockingAndReturnWithId() throws Exception { .mapToObj(i -> new TestEntity() .setName("名前" + i) .setAddress(Optional.of("住所" + i)) - .setVersion(0)) + .setVersion(1)) .collect(Collectors.toList()); assertThat(agent.inserts(TestEntity.class, entities.stream()), is(9)); @@ -192,12 +192,12 @@ void testEntityMergeWithLockingAndReturnWithId() throws Exception { assertThat(result1.getId(), is(updateEntity.getId())); assertThat(result1.getName(), is(updateEntity.getName())); assertThat(result1.getAddress(), is(Optional.of("住所2_new"))); - assertThat(result1.getVersion(), is(updateEntity.getVersion() + 1)); + assertThat(result1.getVersion(), is(2)); var insertEntity = new TestEntity() .setName("名前10_new") .setAddress(Optional.of("住所10_new")) - .setVersion(0); + .setVersion(1); var result2 = agent.mergeWithLockingAndReturn(insertEntity); assertThat(result2.getId(), is(insertEntity.getId())); @@ -223,7 +223,7 @@ void testEntityMergeWithIdOptionalEmpty() throws Exception { .mapToObj(i -> new TestEntity() .setName("名前" + i) .setAddress(Optional.of("住所" + i)) - .setVersion(0)) + .setVersion(1)) .collect(Collectors.toList()); assertThat(agent.inserts(TestEntity.class, entities.stream()), is(9)); @@ -236,12 +236,12 @@ void testEntityMergeWithIdOptionalEmpty() throws Exception { assertThat(result1.getId(), is(updateEntity.getId())); assertThat(result1.getName(), is(updateEntity.getName())); assertThat(result1.getAddress(), is(Optional.empty())); - assertThat(result1.getVersion(), is(updateEntity.getVersion() + 1)); + assertThat(result1.getVersion(), is(2)); var insertEntity = new TestEntity() .setName("名前10_new") .setAddress(Optional.empty()) - .setVersion(0); + .setVersion(1); var result2 = agent.mergeAndReturn(insertEntity); assertThat(result2.getId(), is(insertEntity.getId())); @@ -267,7 +267,7 @@ void testEntityMergeWithIdOptionalNull() throws Exception { .mapToObj(i -> new TestEntity() .setName("名前" + i) .setAddress(Optional.of("住所" + i)) - .setVersion(0)) + .setVersion(1)) .collect(Collectors.toList()); assertThat(agent.inserts(TestEntity.class, entities.stream()), is(9)); @@ -280,12 +280,12 @@ void testEntityMergeWithIdOptionalNull() throws Exception { assertThat(result1.getId(), is(updateEntity.getId())); assertThat(result1.getName(), is(updateEntity.getName())); assertThat(result1.getAddress(), is(Optional.of("住所2"))); // 更新されないこと - assertThat(result1.getVersion(), is(updateEntity.getVersion() + 1)); + assertThat(result1.getVersion(), is(2)); var insertEntity = new TestEntity() .setName("名前10_new") .setAddress(null) - .setVersion(0); + .setVersion(1); var result2 = agent.mergeAndReturn(insertEntity); assertThat(result2.getId(), is(insertEntity.getId())); @@ -332,7 +332,7 @@ void testEntityMergesMultiKey() throws Exception { .setId(i) .setEndAt(LocalDate.now().plusDays(i)) .setName("名前" + i) - .setVersion(0)) + .setVersion(1)) .collect(Collectors.toList()); assertThat(agent.inserts(TestEntityMultiKey.class, entities.stream()), is(9)); @@ -340,20 +340,19 @@ void testEntityMergesMultiKey() throws Exception { var updateEntity = new TestEntityMultiKey() .setId(beforeEntity.getId()) .setEndAt(beforeEntity.getEndAt()) - .setName(beforeEntity.getName() + "_new") - .setVersion(beforeEntity.getVersion()); + .setName(beforeEntity.getName() + "_new"); var result1 = agent.mergeAndReturn(updateEntity); assertThat(result1.getId(), is(updateEntity.getId())); assertThat(result1.getEndAt(), is(updateEntity.getEndAt())); assertThat(result1.getName(), is(updateEntity.getName())); - assertThat(result1.getVersion(), is(updateEntity.getVersion() + 1)); + assertThat(result1.getVersion(), is(beforeEntity.getVersion() + 1)); var insertEntity = new TestEntityMultiKey() .setId(11) .setEndAt(LocalDate.now().plusDays(11)) .setName("名前11_new") - .setVersion(0); + .setVersion(1); var result2 = agent.mergeAndReturn(insertEntity); assertThat(result2.getId(), is(insertEntity.getId()));