@@ -3,6 +3,7 @@ package io.micronaut.data.jdbc.h2.embeddedAssociation
3
3
import io.micronaut.context.ApplicationContext
4
4
import io.micronaut.data.annotation.*
5
5
import io.micronaut.data.annotation.repeatable.JoinSpecifications
6
+ import io.micronaut.data.connection.jdbc.advice.DelegatingDataSource
6
7
import io.micronaut.data.jdbc.annotation.JdbcRepository
7
8
import io.micronaut.data.jdbc.h2.H2DBProperties
8
9
import io.micronaut.data.jdbc.h2.H2TestPropertyProvider
@@ -11,6 +12,7 @@ import io.micronaut.data.model.Pageable
11
12
import io.micronaut.data.model.Sort
12
13
import io.micronaut.data.model.query.builder.sql.Dialect
13
14
import io.micronaut.data.repository.CrudRepository
15
+ import io.micronaut.data.repository.GenericRepository
14
16
import io.micronaut.data.repository.jpa.JpaSpecificationExecutor
15
17
import io.micronaut.data.repository.jpa.criteria.PredicateSpecification
16
18
import io.micronaut.data.tck.entities.Order
@@ -21,6 +23,8 @@ import spock.lang.Specification
21
23
22
24
import jakarta.inject.Inject
23
25
26
+ import javax.sql.DataSource
27
+
24
28
@MicronautTest
25
29
@H2DBProperties
26
30
class EmbeddedAssociationJoinSpec extends Specification implements H2TestPropertyProvider {
@@ -40,6 +44,29 @@ class EmbeddedAssociationJoinSpec extends Specification implements H2TestPropert
40
44
@Inject
41
45
OneMainEntityEmRepository oneMainEntityEmRepository = applicationContext. getBean(OneMainEntityEmRepository )
42
46
47
+ @Shared
48
+ @Inject
49
+ MyMainEntityRepository myMainEntityRepository = applicationContext. getBean(MyMainEntityRepository )
50
+
51
+ void setup () {
52
+ def dataSource = DelegatingDataSource . unwrapDataSource(applicationContext. getBean(DataSource ))
53
+ def connection = dataSource. connection
54
+ connection. prepareStatement(" DROP TABLE IF EXISTS `my_main_entity`" ). execute()
55
+ connection. prepareStatement("""
56
+ CREATE TABLE `my_main_entity` (
57
+ `id` bigint primary key not null,
58
+ `value` text,
59
+ `example` text,
60
+ `part_text` text);
61
+ """ ). execute()
62
+ }
63
+
64
+ void cleanup () {
65
+ def dataSource = DelegatingDataSource . unwrapDataSource(applicationContext. getBean(DataSource ))
66
+ def connection = dataSource. connection
67
+ connection. prepareStatement(" DROP TABLE IF EXISTS `my_main_entity`" )
68
+ }
69
+
43
70
void ' test one-to-one update' () {
44
71
given :
45
72
ChildEntity child = new ChildEntity (name : " child" )
@@ -122,6 +149,41 @@ class EmbeddedAssociationJoinSpec extends Specification implements H2TestPropert
122
149
oem. id. one. em. assoc[0 ]. name == " C"
123
150
oem. id. one. em. assoc[1 ]. name == " D"
124
151
}
152
+
153
+ void ' test save/update embedded with @GeneratedValue' () {
154
+ when :" should not update field 'example'"
155
+ myMainEntityRepository. save(new MyMainEntity (id : 1L , example : " Test" , value : " Val" ))
156
+ def persistedEntity = myMainEntityRepository. findById(1L ). orElse(null )
157
+ then :
158
+ persistedEntity
159
+ persistedEntity. value == " Val"
160
+ ! persistedEntity. example
161
+ when :
162
+ myMainEntityRepository. update(new MyMainEntity (id : 1L , example : " Changed" , value : " Val-Changed" ))
163
+ def updatedEntity = myMainEntityRepository. findById(1L ). orElse(null )
164
+ then :
165
+ updatedEntity
166
+ updatedEntity. value == " Val-Changed"
167
+ ! updatedEntity. example
168
+
169
+ when :" should not update field 'part_text'"
170
+ myMainEntityRepository. save(new MyMainEntity (id : 2L , value : " Val1" , part : new MyPart (text : " Test" )))
171
+ persistedEntity = myMainEntityRepository. findById(2L ). orElse(null )
172
+ then :
173
+ persistedEntity
174
+ persistedEntity. value == " Val1"
175
+ ! persistedEntity. part. text
176
+ when :
177
+ myMainEntityRepository. update(new MyMainEntity (id : 2L , value : " Val2" , part : new MyPart (text : " Changed" )))
178
+ updatedEntity = myMainEntityRepository. findById(2L ). orElse(null )
179
+ then :
180
+ updatedEntity
181
+ updatedEntity. value == " Val2"
182
+ ! updatedEntity. part. text
183
+
184
+ cleanup :
185
+ myMainEntityRepository. deleteAll()
186
+ }
125
187
}
126
188
127
189
@JdbcRepository (dialect = Dialect .H2 )
@@ -218,3 +280,35 @@ class MainEntityAssociation {
218
280
Long id
219
281
String name
220
282
}
283
+
284
+ @MappedEntity (" my_main_entity" )
285
+ class MyMainEntity {
286
+
287
+ @Id
288
+ Long id
289
+
290
+ @GeneratedValue
291
+ String example
292
+
293
+ String value
294
+
295
+ @Relation (value = Relation.Kind .EMBEDDED )
296
+ MyPart part = new MyPart ()
297
+ }
298
+
299
+ @Embeddable
300
+ class MyPart {
301
+ @GeneratedValue
302
+ String text
303
+ }
304
+
305
+ @JdbcRepository (dialect = Dialect .H2 )
306
+ interface MyMainEntityRepository extends GenericRepository<MyMainEntity , Long > {
307
+ Optional<MyMainEntity > findById (Long id )
308
+
309
+ MyMainEntity save (MyMainEntity entity )
310
+
311
+ MyMainEntity update (MyMainEntity entity )
312
+
313
+ void deleteAll ()
314
+ }
0 commit comments