Skip to content

Commit

Permalink
Fixed a bug in sqlAgent#merge, mergeAndReturn that failed to update E…
Browse files Browse the repository at this point in the history
…ntity with @Version annotation. (#319)

One Checks in coverall is not working, but the other one is OK, so we will merge it this time.
  • Loading branch information
HidekiSugimoto189 authored May 13, 2024
1 parent 6e4867c commit df15022
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/main/java/jp/co/future/uroborosql/SqlAgentImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ private <E> E mergeAndReturn(final E entity, final boolean locking) {
return query.first()
.map(findEntity -> {
for (MappingColumn mappingColumn : mappingColumns.values()) {
if (!mappingColumn.isId()) {
if (!mappingColumn.isId() && !mappingColumn.isVersion()) {
Object value = mappingColumn.getValue(entity);
if (value != null) {
mappingColumn.setValue(findEntity, value);
Expand Down Expand Up @@ -1294,7 +1294,7 @@ protected <E> int batchInsert(final Class<E> entityType, final Stream<E> entitie
.filter(col -> !excludeColumns.contains(col)
&& !col.getJavaType().getRawType().isPrimitive()
&& col.getValue(entity) != null)
.collect(Collectors.toMap(col -> col.getCamelName(), col -> true));
.collect(Collectors.toMap(MappingColumn::getCamelName, col -> true));
}

entityList.add(entity);
Expand Down Expand Up @@ -1382,7 +1382,7 @@ protected <E> int bulkInsert(final Class<E> entityType, final Stream<E> entities
.filter(col -> !excludeColumns.contains(col)
&& !col.getJavaType().getRawType().isPrimitive()
&& col.getValue(entity) != null)
.collect(Collectors.toMap(col -> col.getCamelName(), col -> true));
.collect(Collectors.toMap(MappingColumn::getCamelName, col -> true));
}
// 退避しておいたid値をこのタイミングで設定する
if (nonNullObjectIdFlags != null && !nonNullObjectIdFlags.isEmpty()) {
Expand Down Expand Up @@ -1552,7 +1552,7 @@ protected <E> int batchUpdate(final Class<E> entityType, final Stream<E> entitie
*
* @param <T> ResultSetの1行を変換した型
*/
private final class ResultSetSpliterator<T> extends Spliterators.AbstractSpliterator<T> {
private static final class ResultSetSpliterator<T> extends Spliterators.AbstractSpliterator<T> {
private final ResultSetConverter<T> converter;
private final ResultSet rs;
private boolean finished = false;
Expand Down
47 changes: 24 additions & 23 deletions src/test/java/jp/co/future/uroborosql/SqlEntityMergeTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package jp.co.future.uroborosql;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;

import java.time.LocalDate;
import java.util.ArrayList;
Expand Down Expand Up @@ -36,7 +38,7 @@ public 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));

Expand All @@ -51,12 +53,12 @@ public 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));

TestEntity insertEntity = new TestEntity()
.setName("名前10_new")
.setAddress(Optional.of("住所10_new"))
.setVersion(0);
.setVersion(1);

assertThat(agent.merge(insertEntity), is(1));
TestEntity result2 = agent.find(TestEntity.class, 10).orElse(null);
Expand Down Expand Up @@ -84,7 +86,7 @@ public 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));

Expand All @@ -97,12 +99,12 @@ public 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));

TestEntity insertEntity = new TestEntity()
.setName("名前10_new")
.setAddress(Optional.of("住所10_new"))
.setVersion(0);
.setVersion(1);

TestEntity result2 = agent.mergeAndReturn(insertEntity);
assertThat(result2.getId(), is(10));
Expand All @@ -128,7 +130,7 @@ public 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));

Expand All @@ -143,12 +145,12 @@ public 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));

TestEntity insertEntity = new TestEntity()
.setName("名前10_new")
.setAddress(Optional.of("住所10_new"))
.setVersion(0);
.setVersion(1);

assertThat(agent.mergeWithLocking(insertEntity), is(1));
TestEntity result2 = agent.find(TestEntity.class, 10).orElse(null);
Expand Down Expand Up @@ -176,7 +178,7 @@ public 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));

Expand All @@ -189,7 +191,7 @@ public 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));

TestEntity insertEntity = new TestEntity()
.setName("名前10_new")
Expand Down Expand Up @@ -220,7 +222,7 @@ public 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));

Expand All @@ -233,7 +235,7 @@ public 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));

TestEntity insertEntity = new TestEntity()
.setName("名前10_new")
Expand Down Expand Up @@ -264,7 +266,7 @@ public 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));

Expand All @@ -277,12 +279,12 @@ public 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));

TestEntity insertEntity = new TestEntity()
.setName("名前10_new")
.setAddress(null)
.setVersion(0);
.setVersion(1);

TestEntity result2 = agent.mergeAndReturn(insertEntity);
assertThat(result2.getId(), is(insertEntity.getId()));
Expand Down Expand Up @@ -327,28 +329,27 @@ public 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));

TestEntityMultiKey beforeEntity = entities.get(1);
TestEntityMultiKey updateEntity = new TestEntityMultiKey()
.setId(beforeEntity.getId())
.setEndAt(beforeEntity.getEndAt())
.setName(beforeEntity.getName() + "_new")
.setVersion(beforeEntity.getVersion());
.setName(beforeEntity.getName() + "_new");

TestEntityMultiKey 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));

TestEntityMultiKey insertEntity = new TestEntityMultiKey()
.setId(11)
.setEndAt(LocalDate.now().plusDays(11))
.setName("名前11_new")
.setVersion(0);
.setVersion(1);

TestEntityMultiKey result2 = agent.mergeAndReturn(insertEntity);
assertThat(result2.getId(), is(insertEntity.getId()));
Expand Down

0 comments on commit df15022

Please sign in to comment.