Search code examples
grailsgrails-ormgrails-3.0grails-3.0.10

How do I detect a Composite Primary Key in Grails 3.0?


I am currently migrating a Grails 2.4 project to Grails 3.0.10 or 3.1.0.M3.

In Grails 2.4, the following method is a workaround that allows me to detect whether a Domain Class features a composite primary key:

void isCompositePrimaryKey(GrailsDomainClass targetClass) {
    def binder = new GrailsDomainBinder()
    def idMapping = binder.getMapping(targetClass).identity
    return idMapping instanceof org.codehaus.groovy.grails.orm.hibernate.cfg.CompositeIdentity
}

I cannot find a way to detect this in the Public API.

Though GrailsDomainClass still exists in the implementation source code, I cannot seem to access it from my project nor can I find the old CompositeIdentity.

An alternative strategy could be via targetClass.getIdentifier().getType() but I cannot find any documentation on how to detect a composite key using the identity type.


Solution

  • Solved this simply by adding an additional explicit (non-transitive) dependency to build.gradle for the Hibernate 4 GORM implementation package.

    (I determined the package and version by looking in the local gradle files cache but gradle dependencies would also have worked.)

    Added to build.gradle:

    compile 'org.grails:grails-datastore-gorm-hibernate4:5.0.0.RC2'`
    

    This allows access to the internal API, then the above method still works.

    NB. The GORM developers also advise that there is existing GORM API for this, via the PersistentEntity and MappingContext classes without using GrailsDomainClass.