Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a bug in sqlAgent#merge, mergeAndReturn that failed to update Entity with @Version annotation. #336

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/jp/co/future/uroborosql/SqlAgentImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2316,7 +2316,7 @@ private <E> 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);
Expand Down
45 changes: 22 additions & 23 deletions src/test/java/jp/co/future/uroborosql/SqlEntityMergeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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);
Expand Down Expand Up @@ -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));

Expand All @@ -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));
Expand All @@ -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));

Expand All @@ -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);
Expand Down Expand Up @@ -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));

Expand All @@ -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()));
Expand All @@ -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));

Expand All @@ -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()));
Expand All @@ -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));

Expand All @@ -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()));
Expand Down Expand Up @@ -332,28 +332,27 @@ 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));

var beforeEntity = entities.get(1);
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()));
Expand Down
Loading