Skip to content

Commit eb0e2ac

Browse files
Fix some odd id saveall Behavior
1 parent b79b302 commit eb0e2ac

File tree

10 files changed

+112
-33
lines changed

10 files changed

+112
-33
lines changed

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/support/id/IdManager.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ public <S extends T> void checkIds(final Collection<S> entities)
147147
}
148148

149149
final List<ID> multipleEqualIds = ids.stream()
150-
.filter(id -> id != null && Collections.frequency(ids, id) > 1)
150+
.filter(
151+
id -> id != null
152+
&& !id.equals(this.idSetter.getDefaultValue())
153+
&& Collections.frequency(ids, id) > 1)
151154
.toList();
152155
if(!multipleEqualIds.isEmpty())
153156
{

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/support/id/IdSetter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,6 @@ static <T> IdSetter<T> createIdSetter(
6464
void ensureId(T objectToSetIdIn);
6565

6666
boolean isAutomaticSetter();
67+
68+
Object getDefaultValue();
6769
}

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/support/id/NotSettingIdSetter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@ public boolean isAutomaticSetter()
2828
{
2929
return false;
3030
}
31+
32+
@Override
33+
public Object getDefaultValue()
34+
{
35+
return null;
36+
}
3137
}

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/support/id/SimpleIdSetter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void ensureId(final T objectToSetIdIn)
5959
objectToSetIdIn))
6060
{
6161
final Object existingId = fam.getValueOfField(objectToSetIdIn);
62-
if(existingId == null)
62+
if(existingId == null || existingId.equals(this.idFinder.getDefaultValue()))
6363
{
6464
final ID newId = this.idFinder.findId();
6565
fam.writeValueOfField(objectToSetIdIn, newId, true);
@@ -77,4 +77,10 @@ public boolean isAutomaticSetter()
7777
{
7878
return true;
7979
}
80+
81+
@Override
82+
public Object getDefaultValue()
83+
{
84+
return this.idFinder.getDefaultValue();
85+
}
8086
}

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/support/id/strategy/IdFinder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,6 @@ else if(Long.class.isAssignableFrom(idField.getType()) || long.class.isAssignabl
6464
}
6565

6666
ID findId();
67+
68+
ID getDefaultValue();
6769
}

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/support/id/strategy/auto/AutoIntegerIdFinder.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@ public AutoIntegerIdFinder(final Supplier<Object> idGetter)
2828
@Override
2929
protected Integer getNext(final Integer oldId)
3030
{
31-
if(oldId == null || oldId == Integer.MAX_VALUE)
31+
if(oldId == null || oldId == 0 || oldId == Integer.MAX_VALUE)
3232
{
33-
return 0;
33+
return 1;
3434
}
3535
return oldId + 1;
3636
}
37+
38+
@Override
39+
public Integer getDefaultValue()
40+
{
41+
return 0;
42+
}
3743
}

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/support/id/strategy/auto/AutoLongIdFinder.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,27 @@ public AutoLongIdFinder(final Supplier<Object> lastIdGetter)
2828
@Override
2929
protected Long getNext(final Long oldId)
3030
{
31-
if(oldId == null)
31+
if(oldId == null || oldId == 0L)
3232
{
33-
return 0L;
33+
return 1L;
3434
}
3535
try
3636
{
3737
if(oldId == Long.MAX_VALUE)
3838
{
39-
return 0L;
39+
return 1L;
4040
}
4141
return oldId + 1L;
4242
}
4343
catch(final NumberFormatException e)
4444
{
45-
return 0L;
45+
return 1L;
4646
}
4747
}
48+
49+
@Override
50+
public Long getDefaultValue()
51+
{
52+
return 0L;
53+
}
4854
}

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/support/id/strategy/auto/AutoStringIdFinder.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,26 @@ protected String getNext(final String oldId)
3030
{
3131
if(oldId == null)
3232
{
33-
return Long.toUnsignedString(0L);
33+
return Long.toUnsignedString(1L);
3434
}
3535
try
3636
{
3737
final long newId = Long.parseUnsignedLong(oldId);
3838
if(newId == Long.MAX_VALUE)
3939
{
40-
return Long.toUnsignedString(0L);
40+
return Long.toUnsignedString(1L);
4141
}
4242
return Long.toUnsignedString(newId + 1L);
4343
}
4444
catch(final NumberFormatException e)
4545
{
46-
return Long.toUnsignedString(0L);
46+
return Long.toUnsignedString(1L);
4747
}
4848
}
49+
50+
@Override
51+
public String getDefaultValue()
52+
{
53+
return null;
54+
}
4955
}

spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/id/IdTest.java

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void testCreateSingleWithAutoIdInteger(@Autowired final CustomerWithIdIntegerRep
6868
TestUtil.doBeforeAndAfterRestartOfDatastore(
6969
this.configuration,
7070
() -> {
71-
final Optional<CustomerWithIdInteger> loadedCustomer = customerRepository.findById(0);
71+
final Optional<CustomerWithIdInteger> loadedCustomer = customerRepository.findById(1);
7272
Assertions.assertTrue(loadedCustomer.isPresent());
7373
Assertions.assertEquals(customer1, loadedCustomer.get());
7474
}
@@ -87,17 +87,52 @@ void saveBulkWithAutoIdInteger(@Autowired final CustomerWithIdIntegerRepository
8787
() -> {
8888
Assertions.assertEquals(2, customerRepository.count());
8989

90-
final Optional<CustomerWithIdInteger> loadedCustomer1 = customerRepository.findById(0);
90+
final Optional<CustomerWithIdInteger> loadedCustomer1 = customerRepository.findById(1);
9191
Assertions.assertTrue(loadedCustomer1.isPresent());
9292
Assertions.assertEquals(customer1, loadedCustomer1.get());
9393

94-
final Optional<CustomerWithIdInteger> loadedCustomer2 = customerRepository.findById(1);
94+
final Optional<CustomerWithIdInteger> loadedCustomer2 = customerRepository.findById(2);
9595
Assertions.assertTrue(loadedCustomer2.isPresent());
9696
Assertions.assertEquals(customer2, loadedCustomer2.get());
9797
}
9898
);
9999
}
100100

101+
@Test
102+
void saveBulkWithAutoIdInt(@Autowired final CustomerWithIdIntRepository customerRepository)
103+
{
104+
final CustomerWithIdInt customer1 = new CustomerWithIdInt(TestData.FIRST_NAME, TestData.LAST_NAME);
105+
final CustomerWithIdInt customer2 = new CustomerWithIdInt(TestData.FIRST_NAME, TestData.LAST_NAME);
106+
customerRepository.saveAll(List.of(customer1, customer2));
107+
108+
TestUtil.doBeforeAndAfterRestartOfDatastore(
109+
this.configuration,
110+
() -> {
111+
Assertions.assertEquals(2, customerRepository.count());
112+
final Iterable<CustomerWithIdInt> all = customerRepository.findAll();
113+
114+
final Optional<CustomerWithIdInt> loadedCustomer1 = customerRepository.findById(1);
115+
Assertions.assertTrue(loadedCustomer1.isPresent());
116+
Assertions.assertEquals(customer1, loadedCustomer1.get());
117+
118+
final Optional<CustomerWithIdInt> loadedCustomer2 = customerRepository.findById(2);
119+
Assertions.assertTrue(loadedCustomer2.isPresent());
120+
Assertions.assertEquals(customer2, loadedCustomer2.get());
121+
}
122+
);
123+
}
124+
125+
@Test
126+
void saveBulkWithAutoIdIntAndHardcodedId(@Autowired final CustomerWithIdIntRepository customerRepository)
127+
{
128+
final CustomerWithIdInt customer1 = new CustomerWithIdInt(1, TestData.FIRST_NAME, TestData.LAST_NAME);
129+
final CustomerWithIdInt customer2 = new CustomerWithIdInt(1, TestData.FIRST_NAME, TestData.LAST_NAME);
130+
Assertions.assertThrows(
131+
IllegalArgumentException.class,
132+
() -> customerRepository.saveAll(List.of(customer1, customer2))
133+
);
134+
}
135+
101136
/**
102137
* In other tests {@link EclipseStoreStorage#clearData} is called. Here the datastore is restarted again to ensure
103138
* no previous method is called before the test.
@@ -112,7 +147,7 @@ void testSaveSingleWithoutAnyPreviousCall(@Autowired final CustomerWithIdInteger
112147
TestUtil.doBeforeAndAfterRestartOfDatastore(
113148
this.configuration,
114149
() -> {
115-
final Optional<CustomerWithIdInteger> loadedCustomer = customerRepository.findById(0);
150+
final Optional<CustomerWithIdInteger> loadedCustomer = customerRepository.findById(1);
116151
Assertions.assertTrue(loadedCustomer.isPresent());
117152
Assertions.assertEquals(customer1, loadedCustomer.get());
118153
}
@@ -143,7 +178,7 @@ void testCreateMultipleWithAutoIdInteger(@Autowired final CustomerWithIdIntegerR
143178
this.configuration,
144179
() -> {
145180
final List<CustomerWithIdInteger> loadedCustomers =
146-
TestUtil.iterableToList(customerRepository.findAllById(List.of(0, 1)));
181+
TestUtil.iterableToList(customerRepository.findAllById(List.of(1, 2)));
147182
Assertions.assertEquals(2, loadedCustomers.size());
148183
Assertions.assertNotEquals(loadedCustomers.get(0), loadedCustomers.get(1));
149184
}
@@ -164,9 +199,9 @@ void testCreateMultipleWithAutoIdIntegerSingleFinds(
164199
TestUtil.doBeforeAndAfterRestartOfDatastore(
165200
this.configuration,
166201
() -> {
167-
final Optional<CustomerWithIdInteger> loadedCustomer1 = customerRepository.findById(0);
202+
final Optional<CustomerWithIdInteger> loadedCustomer1 = customerRepository.findById(1);
168203
Assertions.assertEquals(customer1, loadedCustomer1.get());
169-
final Optional<CustomerWithIdInteger> loadedCustomer2 = customerRepository.findById(1);
204+
final Optional<CustomerWithIdInteger> loadedCustomer2 = customerRepository.findById(2);
170205
Assertions.assertEquals(customer2, loadedCustomer2.get());
171206
}
172207
);
@@ -181,7 +216,7 @@ void testCreateSingleWithAutoIdInt(@Autowired final CustomerWithIdIntRepository
181216
TestUtil.doBeforeAndAfterRestartOfDatastore(
182217
this.configuration,
183218
() -> {
184-
final Optional<CustomerWithIdInt> loadedCustomer = customerRepository.findById(0);
219+
final Optional<CustomerWithIdInt> loadedCustomer = customerRepository.findById(1);
185220
Assertions.assertTrue(loadedCustomer.isPresent());
186221
Assertions.assertEquals(customer1, loadedCustomer.get());
187222
}
@@ -197,7 +232,7 @@ void testCreateSingleWithAutoIdString(@Autowired final CustomerWithIdStringRepos
197232
TestUtil.doBeforeAndAfterRestartOfDatastore(
198233
this.configuration,
199234
() -> {
200-
final Optional<CustomerWithIdString> loadedCustomer = customerRepository.findById("0");
235+
final Optional<CustomerWithIdString> loadedCustomer = customerRepository.findById("1");
201236
Assertions.assertTrue(loadedCustomer.isPresent());
202237
Assertions.assertEquals(customer1, loadedCustomer.get());
203238
}
@@ -217,7 +252,7 @@ void testCreateMultipleWithAutoIdString(@Autowired final CustomerWithIdStringRep
217252
this.configuration,
218253
() -> {
219254
final List<CustomerWithIdString> loadedCustomers =
220-
TestUtil.iterableToList(customerRepository.findAllById(List.of("0", "1")));
255+
TestUtil.iterableToList(customerRepository.findAllById(List.of("1", "2")));
221256
Assertions.assertEquals(2, loadedCustomers.size());
222257
Assertions.assertNotEquals(loadedCustomers.get(0), loadedCustomers.get(1));
223258
}
@@ -233,10 +268,10 @@ void testCreateSingleWithAutoIdLong(@Autowired final CustomerWithIdLongRepositor
233268
TestUtil.doBeforeAndAfterRestartOfDatastore(
234269
this.configuration,
235270
() -> {
236-
final Optional<CustomerWithIdLong> loadedCustomer = customerRepository.findById(0L);
271+
final Optional<CustomerWithIdLong> loadedCustomer = customerRepository.findById(1L);
237272
Assertions.assertTrue(loadedCustomer.isPresent());
238273
Assertions.assertEquals(customer1, loadedCustomer.get());
239-
Assertions.assertEquals(0L, loadedCustomer.get().getId());
274+
Assertions.assertEquals(1L, loadedCustomer.get().getId());
240275
}
241276
);
242277
}
@@ -254,12 +289,12 @@ void testCreateMultipleWithAutoIdLong(@Autowired final CustomerWithIdLongReposit
254289
this.configuration,
255290
() -> {
256291
final List<CustomerWithIdLong> loadedCustomers =
257-
TestUtil.iterableToList(customerRepository.findAllById(List.of(0L, 1L)));
292+
TestUtil.iterableToList(customerRepository.findAllById(List.of(1L, 2L)));
258293
Assertions.assertEquals(2, loadedCustomers.size());
259294
Assertions.assertNotEquals(loadedCustomers.get(0), loadedCustomers.get(1));
260295
final List<Long> idList = loadedCustomers.stream().map(CustomerWithIdLong::getId).toList();
261-
Assertions.assertTrue(idList.contains(0L));
262296
Assertions.assertTrue(idList.contains(1L));
297+
Assertions.assertTrue(idList.contains(2L));
263298
}
264299
);
265300
}
@@ -320,7 +355,7 @@ void testSaveSingleWithAutoIdInteger(
320355
final List<CustomerWithIdInteger> loadedCustomer =
321356
TestUtil.iterableToList(customerRepository.findAll());
322357
Assertions.assertEquals(1, loadedCustomer.size());
323-
Assertions.assertEquals(0, loadedCustomer.get(0).getId());
358+
Assertions.assertEquals(1, loadedCustomer.get(0).getId());
324359
Assertions.assertEquals(customer1, loadedCustomer.get(0));
325360
}
326361
);
@@ -355,7 +390,7 @@ void testAutoIdWithSubnodeWithId(
355390
TestUtil.iterableToList(customerRepository.findAll());
356391
Assertions.assertEquals(1, loadedCustomer.get(0).getPurchases().size());
357392
Assertions.assertEquals(purchaseName, loadedCustomer.get(0).getPurchases().get(0).getProductName());
358-
Assertions.assertEquals(0, loadedCustomer.get(0).getPurchases().get(0).getId());
393+
Assertions.assertEquals(1, loadedCustomer.get(0).getPurchases().get(0).getId());
359394
}
360395
);
361396
}
@@ -377,8 +412,8 @@ void testAutoIdWithTwoSubnodeWithId(
377412
final List<CustomerWithPurchase> loadedCustomer =
378413
TestUtil.iterableToList(customerRepository.findAll());
379414
Assertions.assertEquals(2, loadedCustomer.get(0).getPurchases().size());
380-
Assertions.assertEquals(0, loadedCustomer.get(0).getPurchases().get(0).getId());
381-
Assertions.assertEquals(1, loadedCustomer.get(0).getPurchases().get(1).getId());
415+
Assertions.assertEquals(1, loadedCustomer.get(0).getPurchases().get(0).getId());
416+
Assertions.assertEquals(2, loadedCustomer.get(0).getPurchases().get(1).getId());
382417
}
383418
);
384419
}
@@ -405,13 +440,13 @@ void testAutoIdWithTwoSameSubnodesWithSameIdDifferentNod(
405440
final CustomerWithPurchase loadedCustomer1 =
406441
CustomerWithPurchase.getCustomerWithLastName(loadedCustomer, TestData.LAST_NAME);
407442
Assertions.assertEquals(1, loadedCustomer1.getPurchases().size());
408-
Assertions.assertEquals(0, loadedCustomer1.getPurchases().get(0).getId());
443+
Assertions.assertEquals(1, loadedCustomer1.getPurchases().get(0).getId());
409444

410445
TestUtil.iterableToList(customerRepository.findAll());
411446
final CustomerWithPurchase loadedCustomer2 =
412447
CustomerWithPurchase.getCustomerWithLastName(loadedCustomer, TestData.LAST_NAME_ALTERNATIVE);
413448
Assertions.assertEquals(1, loadedCustomer2.getPurchases().size());
414-
Assertions.assertEquals(0, loadedCustomer2.getPurchases().get(0).getId());
449+
Assertions.assertEquals(1, loadedCustomer2.getPurchases().get(0).getId());
415450
}
416451
);
417452
}
@@ -433,8 +468,8 @@ void testAutoIdWithTwoSameSubnodesWithSameIdSameNode(
433468
TestUtil.iterableToList(customerRepository.findAll());
434469

435470
Assertions.assertEquals(2, loadedCustomer.get(0).getPurchases().size());
436-
Assertions.assertEquals(0, loadedCustomer.get(0).getPurchases().get(0).getId());
437-
Assertions.assertEquals(0, loadedCustomer.get(0).getPurchases().get(1).getId());
471+
Assertions.assertEquals(1, loadedCustomer.get(0).getPurchases().get(0).getId());
472+
Assertions.assertEquals(1, loadedCustomer.get(0).getPurchases().get(1).getId());
438473
}
439474
);
440475
}

spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/id/model/CustomerWithIdInt.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public CustomerWithIdInt(final String firstName, final String lastName)
4242
this.lastName = lastName;
4343
}
4444

45+
public CustomerWithIdInt(final int id, final String firstName, final String lastName)
46+
{
47+
this.id = id;
48+
this.firstName = firstName;
49+
this.lastName = lastName;
50+
}
51+
4552
public String getFirstName()
4653
{
4754
return this.firstName;

0 commit comments

Comments
 (0)