Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit c93af80

Browse files
authored
Merge pull request #236 from felixscheinost/feature/234-fix-composite-id-jpa
Fix composite ID not visible to JPA
2 parents 67926fb + 6ffcbc5 commit c93af80

File tree

4 files changed

+99
-17
lines changed

4 files changed

+99
-17
lines changed

grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,7 @@ protected void bindCompositeId(PersistentEntity domainClass, RootClass root,
18311831
Component id = new Component(metadataBuildingContext, root);
18321832
id.setNullValue("undefined");
18331833
root.setIdentifier(id);
1834+
root.setIdentifierMapper(id);
18341835
root.setEmbeddedIdentifier(true);
18351836
id.setComponentClassName(domainClass.getName());
18361837
id.setKey(true);
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package grails.gorm.tests.compositeid
2+
3+
import grails.gorm.annotation.Entity
4+
import grails.gorm.hibernate.mapping.MappingBuilder
5+
import grails.gorm.transactions.Rollback
6+
import org.grails.orm.hibernate.HibernateDatastore
7+
import spock.lang.AutoCleanup
8+
import spock.lang.Issue
9+
import spock.lang.Shared
10+
import spock.lang.Specification
11+
12+
@Rollback
13+
class CompositeIdCriteria extends Specification {
14+
15+
@Shared
16+
@AutoCleanup
17+
HibernateDatastore datastore = new HibernateDatastore(CompositeIdToMany, CompositeIdSimple, Author, Book)
18+
19+
@Issue("https://github.com/grails/gorm-hibernate5/issues/234")
20+
def "test that composite to-many properties can be queried using JPA"() {
21+
Author _author = new Author(name:"Author").save()
22+
Book _book = new Book(title:"Book").save()
23+
CompositeIdToMany compositeIdToMany = new CompositeIdToMany(author:_author, book:_book).save(failOnError:true, flush:true)
24+
25+
def criteriaBuilder = datastore.sessionFactory.criteriaBuilder
26+
def criteriaQuery = criteriaBuilder.createQuery()
27+
def root = criteriaQuery.from(CompositeIdToMany)
28+
criteriaQuery.select(root)
29+
criteriaQuery.where(criteriaBuilder.equal(root.get("author"), _author))
30+
def query = datastore.sessionFactory.currentSession.createQuery(criteriaQuery)
31+
32+
expect:
33+
query.list() == [compositeIdToMany]
34+
}
35+
36+
def "test that composite can be queried using JPA"() {
37+
CompositeIdSimple compositeIdSimple = new CompositeIdSimple(name:"name", age:2l).save(failOnError:true, flush:true)
38+
39+
def criteriaBuilder = datastore.sessionFactory.criteriaBuilder
40+
def criteriaQuery = criteriaBuilder.createQuery()
41+
def root = criteriaQuery.from(CompositeIdSimple)
42+
criteriaQuery.select(root)
43+
criteriaQuery.where(criteriaBuilder.equal(root.get("name"), "name"))
44+
def query = datastore.sessionFactory.currentSession.createQuery(criteriaQuery)
45+
46+
expect:
47+
query.list() == [compositeIdSimple]
48+
}
49+
50+
@Issue("https://github.com/grails/grails-data-mapping/issues/1351")
51+
def "test that composite to-many can be used in criteria"() {
52+
Author _author = new Author(name:"Author").save()
53+
Book _book = new Book(title:"Book").save()
54+
CompositeIdToMany compositeIdToMany = new CompositeIdToMany(author:_author, book:_book).save(failOnError:true, flush:true)
55+
56+
expect:
57+
CompositeIdToMany.createCriteria().list {
58+
author {
59+
eq('id', _author.id)
60+
}
61+
} == [compositeIdToMany]
62+
}
63+
}
64+
65+
@Entity
66+
class Author {
67+
String name
68+
}
69+
70+
@Entity
71+
class Book {
72+
String title
73+
}
74+
75+
@Entity
76+
class CompositeIdToMany implements Serializable {
77+
Author author
78+
Book book
79+
80+
static mapping = MappingBuilder.define {
81+
composite("author", "book")
82+
}
83+
}
84+
85+
@Entity
86+
class CompositeIdSimple implements Serializable {
87+
String name
88+
Long age
89+
90+
static mapping = MappingBuilder.define {
91+
composite("name", "age")
92+
}
93+
}
94+
95+

grails-datastore-gorm-hibernate5/src/test/groovy/grails/gorm/tests/compositeid/CompositeIdWithDeepOneToManyMappingSpec.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Child implements Serializable {
4343
static belongsTo= [parent: Parent]
4444

4545
static mapping = MappingBuilder.define {
46-
id(composite: ['parent', 'name'])
46+
composite('parent', 'name')
4747
}
4848
}
4949

@@ -56,7 +56,7 @@ class Parent implements Serializable {
5656
static hasMany= [children: Child]
5757

5858
static mapping= MappingBuilder.define {
59-
id(composite: ['grandParent', 'name'])
59+
composite('grandParent', 'name')
6060
}
6161
}
6262

@@ -69,6 +69,6 @@ class GrandParent implements Serializable {
6969
static hasMany= [parents: Parent]
7070

7171
static mapping= MappingBuilder.define {
72-
id(composite: ['name', 'luckyNumber'])
72+
composite('name', 'luckyNumber')
7373
}
7474
}

grails-datastore-gorm-hibernate5/src/test/groovy/org/grails/orm/hibernate/compiler/HibernateEntityTransformationSpec.groovy

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,4 @@ class MyEntity {
154154
then:"The value is changed"
155155
myEntity.name == 'changed'
156156
}
157-
}
158-
@grails.gorm.hibernate.annotation.ManagedEntity
159-
class MyTest {
160-
String name
161-
String lastName
162-
int age
163-
164-
String getLastName() {
165-
return this.lastName
166-
}
167-
168-
void setLastName(String name) {
169-
this.lastName = name
170-
}
171157
}

0 commit comments

Comments
 (0)