Skip to content

Commit 851ff87

Browse files
Added Auto UUID
1 parent 78d8d68 commit 851ff87

File tree

7 files changed

+237
-25
lines changed

7 files changed

+237
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# 2.1.0
22

3+
* Implemented auto-id-generation for UUIDs.
34
* Implemented composite primary keys.
45

56
# 2.0.1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
= IDs
2+
3+
{product-name} supports the following types with auto generating (``GenerationType.AUTO``) values:
4+
5+
* ``int`` / ``Integer``
6+
* ``long`` / ``Long``
7+
* ``String``
8+
* ``UUID``

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.lang.reflect.Field;
1919
import java.util.Objects;
20+
import java.util.UUID;
2021
import java.util.function.Supplier;
2122

2223
import jakarta.persistence.GeneratedValue;
@@ -26,6 +27,7 @@
2627
import software.xdev.spring.data.eclipse.store.repository.support.id.strategy.auto.AutoIntegerIdFinder;
2728
import software.xdev.spring.data.eclipse.store.repository.support.id.strategy.auto.AutoLongIdFinder;
2829
import software.xdev.spring.data.eclipse.store.repository.support.id.strategy.auto.AutoStringIdFinder;
30+
import software.xdev.spring.data.eclipse.store.repository.support.id.strategy.auto.AutoUUIDIdFinder;
2931

3032

3133
/**
@@ -56,6 +58,10 @@ else if(Long.class.isAssignableFrom(idField.getType()) || long.class.isAssignabl
5658
{
5759
return (IdFinder<ID>)new AutoLongIdFinder(lastIdGetter);
5860
}
61+
else if(idField.getType().equals(UUID.class))
62+
{
63+
return (IdFinder<ID>)new AutoUUIDIdFinder(lastIdGetter);
64+
}
5965
}
6066
throw new IdGeneratorNotSupportedException(String.format(
6167
"Id generator with strategy %s for type %s is not supported.",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.repository.support.id.strategy.auto;
17+
18+
import java.util.UUID;
19+
import java.util.function.Supplier;
20+
21+
22+
public class AutoUUIDIdFinder extends AbstractAutoIdFinder<UUID>
23+
{
24+
public AutoUUIDIdFinder(final Supplier<Object> idGetter)
25+
{
26+
super(() -> (UUID)idGetter.get());
27+
}
28+
29+
@Override
30+
protected UUID getNext(final UUID oldId)
31+
{
32+
return UUID.randomUUID();
33+
}
34+
35+
@Override
36+
public UUID getDefaultValue()
37+
{
38+
return null;
39+
}
40+
}

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

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.List;
2121
import java.util.Optional;
22+
import java.util.UUID;
2223

2324
import org.junit.jupiter.api.Assertions;
2425
import org.junit.jupiter.api.Test;
@@ -40,6 +41,8 @@
4041
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.model.CustomerWithIdLongRepository;
4142
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.model.CustomerWithIdString;
4243
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.model.CustomerWithIdStringRepository;
44+
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.model.CustomerWithIdUuid;
45+
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.model.CustomerWithIdUuidRepository;
4346
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.model.CustomerWithPurchase;
4447
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.model.CustomerWithPurchaseRepository;
4548
import software.xdev.spring.data.eclipse.store.integration.isolated.tests.id.model.Purchase;
@@ -60,7 +63,7 @@ public IdTest(final IdTestConfiguration configuration)
6063
}
6164

6265
@Test
63-
void testCreateSingleWithAutoIdInteger(@Autowired final CustomerWithIdIntegerRepository customerRepository)
66+
void createSingleWithAutoIdInteger(@Autowired final CustomerWithIdIntegerRepository customerRepository)
6467
{
6568
final CustomerWithIdInteger customer1 = new CustomerWithIdInteger(TestData.FIRST_NAME, TestData.LAST_NAME);
6669
customerRepository.save(customer1);
@@ -138,7 +141,7 @@ void saveBulkWithAutoIdIntAndHardcodedId(@Autowired final CustomerWithIdIntRepos
138141
* no previous method is called before the test.
139142
*/
140143
@Test
141-
void testSaveSingleWithoutAnyPreviousCall(@Autowired final CustomerWithIdIntegerRepository customerRepository)
144+
void saveSingleWithoutAnyPreviousCall(@Autowired final CustomerWithIdIntegerRepository customerRepository)
142145
{
143146
restartDatastore(this.configuration);
144147
final CustomerWithIdInteger customer1 = new CustomerWithIdInteger(TestData.FIRST_NAME, TestData.LAST_NAME);
@@ -155,7 +158,7 @@ void testSaveSingleWithoutAnyPreviousCall(@Autowired final CustomerWithIdInteger
155158
}
156159

157160
@Test
158-
void testCreateSingleWithAutoIdIntegerWorkingCopyIdSet(
161+
void createSingleWithAutoIdIntegerWorkingCopyIdSet(
159162
@Autowired final CustomerWithIdIntegerRepository customerRepository
160163
)
161164
{
@@ -166,7 +169,7 @@ void testCreateSingleWithAutoIdIntegerWorkingCopyIdSet(
166169
}
167170

168171
@Test
169-
void testCreateMultipleWithAutoIdInteger(@Autowired final CustomerWithIdIntegerRepository customerRepository)
172+
void createMultipleWithAutoIdInteger(@Autowired final CustomerWithIdIntegerRepository customerRepository)
170173
{
171174
final CustomerWithIdInteger customer1 = new CustomerWithIdInteger(TestData.FIRST_NAME, TestData.LAST_NAME);
172175
customerRepository.save(customer1);
@@ -186,7 +189,7 @@ void testCreateMultipleWithAutoIdInteger(@Autowired final CustomerWithIdIntegerR
186189
}
187190

188191
@Test
189-
void testCreateMultipleWithAutoIdIntegerSingleFinds(
192+
void createMultipleWithAutoIdIntegerSingleFinds(
190193
@Autowired final CustomerWithIdIntegerRepository customerRepository
191194
)
192195
{
@@ -208,7 +211,32 @@ void testCreateMultipleWithAutoIdIntegerSingleFinds(
208211
}
209212

210213
@Test
211-
void testCreateSingleWithAutoIdInt(@Autowired final CustomerWithIdIntRepository customerRepository)
214+
void createMultipleWithAutoIdUuidSingleFinds(
215+
@Autowired final CustomerWithIdUuidRepository customerRepository
216+
)
217+
{
218+
final CustomerWithIdUuid customer1 = new CustomerWithIdUuid(TestData.FIRST_NAME, TestData.LAST_NAME);
219+
customerRepository.save(customer1);
220+
final CustomerWithIdUuid customer2 =
221+
new CustomerWithIdUuid(TestData.FIRST_NAME_ALTERNATIVE, TestData.LAST_NAME_ALTERNATIVE);
222+
customerRepository.save(customer2);
223+
224+
final UUID generatedId1 = customerRepository.findAll().get(0).getId();
225+
final UUID generatedId2 = customerRepository.findAll().get(1).getId();
226+
227+
TestUtil.doBeforeAndAfterRestartOfDatastore(
228+
this.configuration,
229+
() -> {
230+
final Optional<CustomerWithIdUuid> loadedCustomer1 = customerRepository.findById(generatedId1);
231+
Assertions.assertEquals(customer1, loadedCustomer1.get());
232+
final Optional<CustomerWithIdUuid> loadedCustomer2 = customerRepository.findById(generatedId2);
233+
Assertions.assertEquals(customer2, loadedCustomer2.get());
234+
}
235+
);
236+
}
237+
238+
@Test
239+
void createSingleWithAutoIdInt(@Autowired final CustomerWithIdIntRepository customerRepository)
212240
{
213241
final CustomerWithIdInt customer1 = new CustomerWithIdInt(TestData.FIRST_NAME, TestData.LAST_NAME);
214242
customerRepository.save(customer1);
@@ -224,7 +252,7 @@ void testCreateSingleWithAutoIdInt(@Autowired final CustomerWithIdIntRepository
224252
}
225253

226254
@Test
227-
void testCreateSingleWithAutoIdString(@Autowired final CustomerWithIdStringRepository customerRepository)
255+
void createSingleWithAutoIdString(@Autowired final CustomerWithIdStringRepository customerRepository)
228256
{
229257
final CustomerWithIdString customer1 = new CustomerWithIdString(TestData.FIRST_NAME, TestData.LAST_NAME);
230258
customerRepository.save(customer1);
@@ -240,7 +268,25 @@ void testCreateSingleWithAutoIdString(@Autowired final CustomerWithIdStringRepos
240268
}
241269

242270
@Test
243-
void testSaveAfterRestartSingleWithAutoIdString(@Autowired final CustomerWithIdStringRepository customerRepository)
271+
void createSingleWithAutoIdUuid(@Autowired final CustomerWithIdUuidRepository customerRepository)
272+
{
273+
final CustomerWithIdUuid customer1 = new CustomerWithIdUuid(TestData.FIRST_NAME, TestData.LAST_NAME);
274+
customerRepository.save(customer1);
275+
276+
final UUID generatedId = customerRepository.findAll().get(0).getId();
277+
278+
TestUtil.doBeforeAndAfterRestartOfDatastore(
279+
this.configuration,
280+
() -> {
281+
final Optional<CustomerWithIdUuid> loadedCustomer = customerRepository.findById(generatedId);
282+
Assertions.assertTrue(loadedCustomer.isPresent());
283+
Assertions.assertEquals(customer1, loadedCustomer.get());
284+
}
285+
);
286+
}
287+
288+
@Test
289+
void saveAfterRestartSingleWithAutoIdString(@Autowired final CustomerWithIdStringRepository customerRepository)
244290
{
245291
final CustomerWithIdString customer1 = new CustomerWithIdString(TestData.FIRST_NAME, TestData.LAST_NAME);
246292
customerRepository.save(customer1);
@@ -263,7 +309,7 @@ void testSaveAfterRestartSingleWithAutoIdString(@Autowired final CustomerWithIdS
263309
}
264310

265311
@Test
266-
void testCreateMultipleWithAutoIdString(@Autowired final CustomerWithIdStringRepository customerRepository)
312+
void createMultipleWithAutoIdString(@Autowired final CustomerWithIdStringRepository customerRepository)
267313
{
268314
final CustomerWithIdString customer1 = new CustomerWithIdString(TestData.FIRST_NAME, TestData.LAST_NAME);
269315
customerRepository.save(customer1);
@@ -283,7 +329,7 @@ void testCreateMultipleWithAutoIdString(@Autowired final CustomerWithIdStringRep
283329
}
284330

285331
@Test
286-
void testCreateSingleWithAutoIdLong(@Autowired final CustomerWithIdLongRepository customerRepository)
332+
void createSingleWithAutoIdLong(@Autowired final CustomerWithIdLongRepository customerRepository)
287333
{
288334
final CustomerWithIdLong customer1 = new CustomerWithIdLong(TestData.FIRST_NAME, TestData.LAST_NAME);
289335
customerRepository.save(customer1);
@@ -300,7 +346,7 @@ void testCreateSingleWithAutoIdLong(@Autowired final CustomerWithIdLongRepositor
300346
}
301347

302348
@Test
303-
void testCreateMultipleWithAutoIdLong(@Autowired final CustomerWithIdLongRepository customerRepository)
349+
void createMultipleWithAutoIdLong(@Autowired final CustomerWithIdLongRepository customerRepository)
304350
{
305351
final CustomerWithIdLong customer1 = new CustomerWithIdLong(TestData.FIRST_NAME, TestData.LAST_NAME);
306352
customerRepository.save(customer1);
@@ -323,7 +369,7 @@ void testCreateMultipleWithAutoIdLong(@Autowired final CustomerWithIdLongReposit
323369
}
324370

325371
@Test
326-
void testCreateSingleWithNoAutoIdInteger(
372+
void createSingleWithNoAutoIdInteger(
327373
@Autowired final CustomerWithIdIntegerNoAutoGenerateRepository customerRepository)
328374
{
329375
final CustomerWithIdIntegerNoAutoGenerate customer1 =
@@ -341,7 +387,7 @@ void testCreateSingleWithNoAutoIdInteger(
341387
}
342388

343389
@Test
344-
void testCreateMultipleWithNoAutoIdInteger(
390+
void createMultipleWithNoAutoIdInteger(
345391
@Autowired final CustomerWithIdIntegerNoAutoGenerateRepository customerRepository)
346392
{
347393
final CustomerWithIdIntegerNoAutoGenerate customer1 =
@@ -364,7 +410,7 @@ void testCreateMultipleWithNoAutoIdInteger(
364410
}
365411

366412
@Test
367-
void testSaveSingleWithAutoIdInteger(
413+
void saveSingleWithAutoIdInteger(
368414
@Autowired final CustomerWithIdIntegerRepository customerRepository
369415
)
370416
{
@@ -385,7 +431,7 @@ void testSaveSingleWithAutoIdInteger(
385431
}
386432

387433
@Test
388-
void testSaveSingleWithNoAutoIdInteger(
434+
void saveSingleWithNoAutoIdInteger(
389435
@Autowired final CustomerWithIdIntegerNoAutoGenerateRepository customerRepository)
390436
{
391437
final CustomerWithIdIntegerNoAutoGenerate customer1 =
@@ -397,7 +443,7 @@ void testSaveSingleWithNoAutoIdInteger(
397443
}
398444

399445
@Test
400-
void testAutoIdWithSubnodeWithId(
446+
void autoIdWithSubnodeWithId(
401447
@Autowired final CustomerWithPurchaseRepository customerRepository)
402448
{
403449
final String purchaseName = "bag";
@@ -419,7 +465,7 @@ void testAutoIdWithSubnodeWithId(
419465
}
420466

421467
@Test
422-
void testAutoIdWithTwoSubnodeWithId(
468+
void autoIdWithTwoSubnodeWithId(
423469
@Autowired final CustomerWithPurchaseRepository customerRepository)
424470
{
425471
final String purchaseName = "bag";
@@ -442,7 +488,7 @@ void testAutoIdWithTwoSubnodeWithId(
442488
}
443489

444490
@Test
445-
void testAutoIdWithTwoSameSubnodesWithSameIdDifferentNod(
491+
void autoIdWithTwoSameSubnodesWithSameIdDifferentNod(
446492
@Autowired final CustomerWithPurchaseRepository customerRepository)
447493
{
448494
final Purchase purchase = new Purchase("bag");
@@ -475,7 +521,7 @@ void testAutoIdWithTwoSameSubnodesWithSameIdDifferentNod(
475521
}
476522

477523
@Test
478-
void testAutoIdWithTwoSameSubnodesWithSameIdSameNode(
524+
void autoIdWithTwoSameSubnodesWithSameIdSameNode(
479525
@Autowired final CustomerWithPurchaseRepository customerRepository)
480526
{
481527
final Purchase purchase = new Purchase("bag");
@@ -498,7 +544,7 @@ void testAutoIdWithTwoSameSubnodesWithSameIdSameNode(
498544
}
499545

500546
@Test
501-
void testReplaceWithId(@Autowired final CustomerWithIdIntegerNoAutoGenerateRepository customerRepository)
547+
void replaceWithId(@Autowired final CustomerWithIdIntegerNoAutoGenerateRepository customerRepository)
502548
{
503549
final CustomerWithIdIntegerNoAutoGenerate existingCustomer =
504550
new CustomerWithIdIntegerNoAutoGenerate(1, TestData.FIRST_NAME, TestData.LAST_NAME);
@@ -523,7 +569,7 @@ void testReplaceWithId(@Autowired final CustomerWithIdIntegerNoAutoGenerateRepos
523569
}
524570

525571
@Test
526-
void testReplaceWithAutoId(@Autowired final CustomerWithIdIntegerRepository customerRepository)
572+
void replaceWithAutoId(@Autowired final CustomerWithIdIntegerRepository customerRepository)
527573
{
528574
final CustomerWithIdInteger existingCustomer =
529575
new CustomerWithIdInteger(TestData.FIRST_NAME, TestData.LAST_NAME);
@@ -550,7 +596,7 @@ void testReplaceWithAutoId(@Autowired final CustomerWithIdIntegerRepository cust
550596
}
551597

552598
@Test
553-
void testReplaceWithIdSaveAll(@Autowired final CustomerWithIdIntegerNoAutoGenerateRepository customerRepository)
599+
void replaceWithIdSaveAll(@Autowired final CustomerWithIdIntegerNoAutoGenerateRepository customerRepository)
554600
{
555601
final CustomerWithIdIntegerNoAutoGenerate existingCustomer =
556602
new CustomerWithIdIntegerNoAutoGenerate(1, TestData.FIRST_NAME, TestData.LAST_NAME);
@@ -565,7 +611,7 @@ void testReplaceWithIdSaveAll(@Autowired final CustomerWithIdIntegerNoAutoGenera
565611
}
566612

567613
@Test
568-
void testAddTwoWithId(@Autowired final CustomerWithIdIntegerNoAutoGenerateRepository customerRepository)
614+
void addTwoWithId(@Autowired final CustomerWithIdIntegerNoAutoGenerateRepository customerRepository)
569615
{
570616
final CustomerWithIdIntegerNoAutoGenerate existingCustomer =
571617
new CustomerWithIdIntegerNoAutoGenerate(1, TestData.FIRST_NAME, TestData.LAST_NAME);
@@ -588,7 +634,7 @@ void testAddTwoWithId(@Autowired final CustomerWithIdIntegerNoAutoGenerateReposi
588634
}
589635

590636
@Test
591-
void testIdsInMultipleTransactions(
637+
void idsInMultipleTransactions(
592638
@Autowired final CustomerWithIdIntegerNoAutoGenerateRepository customerRepository,
593639
@Autowired final PlatformTransactionManager transactionManager
594640
)
@@ -626,7 +672,7 @@ void testIdsInMultipleTransactions(
626672
}
627673

628674
@Test
629-
void testIdsInSingleTransactions(
675+
void idsInSingleTransactions(
630676
@Autowired final CustomerWithIdIntegerNoAutoGenerateRepository customerRepository,
631677
@Autowired final PlatformTransactionManager transactionManager
632678
)

0 commit comments

Comments
 (0)