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

Commit 6ffcbc5

Browse files
Fixes composite identifier not visible to JPA
I am not sure if this fix is correct. The hibernate documentation [1] says that there are the following ways to define a composite identity: - @EmbeddedId - @IdClass - Composite identifiers with associations => Use @id multiple times I am not sure what the binding by `bindCompositeId` corresponds to. I just know that in the 3rd case hibernate does set the `identifierMapper`. I am not sure if there is valid in the other cases. [1]: https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#identifiers-composite
1 parent 337debd commit 6ffcbc5

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
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
@@ -1828,6 +1828,7 @@ protected void bindCompositeId(PersistentEntity domainClass, RootClass root,
18281828
Component id = new Component(metadataBuildingContext, root);
18291829
id.setNullValue("undefined");
18301830
root.setIdentifier(id);
1831+
root.setIdentifierMapper(id);
18311832
root.setEmbeddedIdentifier(true);
18321833
id.setComponentClassName(domainClass.getName());
18331834
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+

0 commit comments

Comments
 (0)