File tree 5 files changed +22
-9
lines changed
main/java/io/micronaut/data/hibernate
test/groovy/io/micronaut/data/hibernate/jakarta_data
jakarta-data-tck/support/src/main/java/io/micronaut/validation/tck
5 files changed +22
-9
lines changed Original file line number Diff line number Diff line change 22
22
import jakarta .data .exceptions .EntityExistsException ;
23
23
import jakarta .data .exceptions .OptimisticLockingFailureException ;
24
24
import jakarta .inject .Singleton ;
25
+ import jakarta .persistence .OptimisticLockException ;
25
26
import jakarta .persistence .PersistenceException ;
27
+ import org .hibernate .StaleStateException ;
26
28
import org .hibernate .exception .ConstraintViolationException ;
27
29
28
30
/**
@@ -41,6 +43,9 @@ public Exception convert(Exception exception) {
41
43
if (exception instanceof ConstraintViolationException ) {
42
44
throw new EntityExistsException (exception .getMessage (), exception );
43
45
}
46
+ if (exception instanceof StaleStateException || exception instanceof OptimisticLockException ) {
47
+ throw new EntityExistsException (exception .getMessage (), exception );
48
+ }
44
49
if (exception instanceof PersistenceException ) {
45
50
return new DataException (exception .getMessage (), exception );
46
51
}
Original file line number Diff line number Diff line change 51
51
import io .micronaut .data .model .runtime .QueryParameterBinding ;
52
52
import io .micronaut .data .model .runtime .RuntimeEntityRegistry ;
53
53
import io .micronaut .data .model .runtime .RuntimePersistentEntity ;
54
+ import io .micronaut .data .model .runtime .RuntimePersistentProperty ;
54
55
import io .micronaut .data .model .runtime .StoredQuery ;
55
56
import io .micronaut .data .model .runtime .UpdateBatchOperation ;
56
57
import io .micronaut .data .model .runtime .UpdateOperation ;
@@ -410,11 +411,17 @@ public <T> T persist(@NonNull InsertOperation<T> operation) {
410
411
return executeUpdate (operation , session , storedQuery );
411
412
}
412
413
T entity = operation .getEntity ();
413
- // if (persistOrMergeOnSave) {
414
- // entity = session.merge(entity);
415
- // } else {
414
+ if (persistOrMergeOnSave ) {
415
+ RuntimePersistentEntity <T > persistentEntity = getEntity (operation .getRootEntity ());
416
+ RuntimePersistentProperty <T > identity = persistentEntity .getIdentity ();
417
+ if (identity != null && identity .getProperty ().get (entity ) == null ) {
418
+ session .persist (entity );
419
+ } else {
420
+ entity = session .merge (entity );
421
+ }
422
+ } else {
416
423
session .persist (entity );
417
- // }
424
+ }
418
425
flushIfNecessary (session , operation .getAnnotationMetadata ());
419
426
return entity ;
420
427
});
Original file line number Diff line number Diff line change 59
59
* can run against a provider that supports any Entity type.
60
60
*/
61
61
//@Property(name = "jpa.default.properties.hibernate.show_sql", value = "true")
62
+ @ Property (name = "jpa.default.properties.uniqueResultOnFindOne" , value = "true" )
63
+ @ Property (name = "jpa.default.properties.persistOrMergeOnSave" , value = "true" )
62
64
@ H2DBProperties
63
65
@ MicronautTest (transactional = false )
64
66
public class EntityTests {
@@ -148,7 +150,6 @@ public void testBasicRepository() {
148
150
assertEquals (60L , numbers .findById (60L ).orElseThrow ().getId ());
149
151
}
150
152
151
- @ Disabled // Pending feature
152
153
@ Test
153
154
public void testBasicRepositoryBuiltInMethods () {
154
155
@@ -250,7 +251,6 @@ public void testBasicRepositoryBuiltInMethods() {
250
251
assertEquals (0 , boxes .findAll ().count ());
251
252
}
252
253
253
- @ Disabled // Pending feature
254
254
@ Test
255
255
public void testBasicRepositoryMethods () {
256
256
Original file line number Diff line number Diff line change 7
7
import jakarta .data .Sort ;
8
8
import jakarta .data .exceptions .EntityExistsException ;
9
9
import jakarta .data .exceptions .OptimisticLockingFailureException ;
10
- import jakarta .data .repository .Find ;
11
- import jakarta .data .repository .OrderBy ;
12
10
import jakarta .inject .Inject ;
13
11
import org .junit .jupiter .api .Disabled ;
14
12
import org .junit .jupiter .api .Test ;
31
29
* Execute tests with a Persistence specific entity with a repository that requires read and writes (AKA not read-only)
32
30
*/
33
31
//@Property(name = "jpa.default.properties.hibernate.show_sql", value = "true")
32
+ @ Property (name = "jpa.default.properties.uniqueResultOnFindOne" , value = "true" )
33
+ @ Property (name = "jpa.default.properties.persistOrMergeOnSave" , value = "true" )
34
34
@ H2DBProperties
35
35
@ MicronautTest (transactional = false )
36
36
public class PersistenceEntityTests {
Original file line number Diff line number Diff line change @@ -144,7 +144,8 @@ public ProtocolMetaData deploy(Archive<?> archive) {
144
144
145
145
ApplicationContext applicationContext = ApplicationContext .builder ()
146
146
.properties (Map .of (
147
- "jpa.default.properties.uniqueResultOnFindOne" , "true"
147
+ "jpa.default.properties.uniqueResultOnFindOne" , "true" ,
148
+ "jpa.default.properties.persistOrMergeOnSave" , "true"
148
149
))
149
150
.classLoader (classLoader )
150
151
.build ()
You can’t perform that action at this time.
0 commit comments