Skip to content

Commit

Permalink
Merge branch '7.0.x' into 7.3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
puneetbehl committed Dec 23, 2022
2 parents d9dde28 + 6190cc8 commit 59d21e2
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package grails.gorm.hibernate

import groovy.transform.CompileStatic
import groovy.transform.Generated
import org.grails.datastore.gorm.GormEnhancer
import org.grails.datastore.gorm.GormEntity
import org.grails.orm.hibernate.AbstractHibernateGormStaticApi
Expand All @@ -21,6 +22,7 @@ trait HibernateEntity<D> extends GormEntity<D> {
*
* @return The object
*/
@Generated
static List<D> findAllWithSql(CharSequence sql) {
currentHibernateStaticApi().findAllWithSql sql, Collections.emptyMap()
}
Expand All @@ -31,6 +33,7 @@ trait HibernateEntity<D> extends GormEntity<D> {
* @param sql The sql query
* @return The entity
*/
@Generated
static D findWithSql(CharSequence sql) {
currentHibernateStaticApi().findWithSql(sql, Collections.emptyMap())
}
Expand All @@ -42,6 +45,7 @@ trait HibernateEntity<D> extends GormEntity<D> {
*
* @return The object
*/
@Generated
static List<D> findAllWithSql(CharSequence sql, Map args) {
currentHibernateStaticApi().findAllWithSql sql, args
}
Expand All @@ -52,10 +56,12 @@ trait HibernateEntity<D> extends GormEntity<D> {
* @param sql The sql query
* @return The entity
*/
@Generated
static D findWithSql(CharSequence sql, Map args) {
currentHibernateStaticApi().findWithSql(sql, args)
}

@Generated
private static AbstractHibernateGormStaticApi currentHibernateStaticApi() {
(AbstractHibernateGormStaticApi)GormEnhancer.findStaticApi(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ abstract class AbstractHibernateGormInstanceApi<D> extends GormInstanceApi<D> {
setObjectToReadOnly target
if (entity) {
for (Association association in entity.associations) {
if (association instanceof ToOne) {
if (association instanceof ToOne && !association instanceof Embedded) {
if(proxyHandler.isInitialized(target, association.name)) {
def bean = new BeanWrapperImpl(target)
def propertyValue = bean.getPropertyValue(association.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1516,7 +1516,9 @@ else if(tablePerConcreteClass) {
if (m.getDynamicInsert()) {
subClass.setDynamicInsert(true);
}


subClass.setCached(parent.isCached());

subClass.setAbstract(sub.isAbstract());
subClass.setEntityName(fullName);
subClass.setJpaEntityName(unqualify(fullName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.grails.orm.hibernate.compiler
import grails.gorm.dirty.checking.DirtyCheckedProperty
import groovy.transform.CompilationUnitAware
import groovy.transform.CompileStatic
import org.apache.groovy.ast.tools.AnnotatedNodeUtils
import org.codehaus.groovy.ast.*
import org.codehaus.groovy.ast.stmt.BlockStatement
import org.codehaus.groovy.ast.stmt.IfStatement
Expand Down Expand Up @@ -124,6 +125,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
returnS(varX(interceptorField))
)
classNode.addMethod(getInterceptorMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, getInterceptorMethod)
staticCompilationVisitor.visitMethod(getInterceptorMethod)

// add method: void $$_hibernate_setInterceptor(PersistentAttributeInterceptor interceptor)
Expand All @@ -137,6 +139,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
assignS( varX(interceptorField), varX(p1) )
)
classNode.addMethod(setInterceptorMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, setInterceptorMethod)
staticCompilationVisitor.visitMethod(setInterceptorMethod)

// add method: Object $$_hibernate_getEntityInstance()
Expand All @@ -149,6 +152,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
returnS(varX("this"))
)
classNode.addMethod(getEntityInstanceMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, getEntityInstanceMethod)
staticCompilationVisitor.visitMethod(getEntityInstanceMethod)


Expand All @@ -162,6 +166,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
returnS(varX(entityEntryHolderField))
)
classNode.addMethod(getEntityEntryMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, getEntityEntryMethod)
staticCompilationVisitor.visitMethod(getEntityEntryMethod)

// add method: void $$_hibernate_setEntityEntry(EntityEntry entityEntry)
Expand All @@ -175,6 +180,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
assignS( varX(entityEntryHolderField), varX(entityEntryParam) )
)
classNode.addMethod(setEntityEntryMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, setEntityEntryMethod)
staticCompilationVisitor.visitMethod(setEntityEntryMethod)

// add method: ManagedEntity $$_hibernate_getPreviousManagedEntity()
Expand All @@ -187,6 +193,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
returnS(varX(previousManagedEntityField))
)
classNode.addMethod(getPreviousManagedEntityMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, getPreviousManagedEntityMethod)
staticCompilationVisitor.visitMethod(getPreviousManagedEntityMethod)

// add method: ManagedEntity $$_hibernate_getNextManagedEntity() {
Expand All @@ -199,6 +206,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
returnS(varX(nextManagedEntityField))
)
classNode.addMethod(getNextManagedEntityMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, getNextManagedEntityMethod)
staticCompilationVisitor.visitMethod(getNextManagedEntityMethod)

// add method: void $$_hibernate_setPreviousManagedEntity(ManagedEntity previous)
Expand All @@ -212,6 +220,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
assignS( varX(previousManagedEntityField), varX(previousParam) )
)
classNode.addMethod(setPreviousManagedEntityMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, setPreviousManagedEntityMethod)
staticCompilationVisitor.visitMethod(setPreviousManagedEntityMethod)

// add method: void $$_hibernate_setNextManagedEntity(ManagedEntity next)
Expand All @@ -225,6 +234,7 @@ class HibernateEntityTransformation implements ASTTransformation, CompilationUni
assignS( varX(nextManagedEntityField), varX(nextParam) )
)
classNode.addMethod(setNextManagedEntityMethod)
AnnotatedNodeUtils.markAsGenerated(classNode, setNextManagedEntityMethod)
staticCompilationVisitor.visitMethod(setNextManagedEntityMethod)

List<MethodNode> allMethods = classNode.getMethods()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package grails.gorm.tests

import grails.gorm.transactions.Rollback
import groovy.transform.Generated
import org.grails.orm.hibernate.HibernateDatastore
import org.springframework.transaction.PlatformTransactionManager
import spock.lang.AutoCleanup
import spock.lang.IgnoreIf
import spock.lang.Shared
import spock.lang.Specification

@Rollback
class HibernateEntityTraitGeneratedSpec extends Specification {

@Shared @AutoCleanup HibernateDatastore datastore = new HibernateDatastore(Club)

void "test that all HibernateEntity trait methods are marked as Generated"() {
// Unfortunately static methods have to check directly one by one
expect:
Club.getMethod('findAllWithSql', CharSequence).isAnnotationPresent(Generated)
Club.getMethod('findWithSql', CharSequence).isAnnotationPresent(Generated)
Club.getMethod('findAllWithSql', CharSequence, Map).isAnnotationPresent(Generated)
Club.getMethod('findWithSql', CharSequence, Map).isAnnotationPresent(Generated)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package grails.gorm.tests.validation

import grails.gorm.annotation.Entity
import grails.gorm.transactions.Rollback
import grails.validation.ValidationException
import org.grails.orm.hibernate.HibernateDatastore
import spock.lang.AutoCleanup
import spock.lang.Issue
import spock.lang.Shared
import spock.lang.Specification

class EmbeddedWithValidationExceptionSpec extends Specification {
@Shared @AutoCleanup HibernateDatastore hibernateDatastore = new HibernateDatastore(DomainWithEmbedded)

@Rollback
@Issue("https://github.com/grails/gorm-hibernate5/issues/110")
void "test validation exception with embedded in domain"() {
when:
new DomainWithEmbedded(
foo: 'not valid',
myEmbedded: new MyEmbedded(
a: 1,
b: 'foo'
)
).save(failOnError: true)

then:
thrown(ValidationException)
}
}

@Entity
class DomainWithEmbedded {
MyEmbedded myEmbedded
String foo

static embedded = ['myEmbedded']

static constraints = {
foo(validator: { val, self ->
return 'not.valid.foo'
})
}
}

class MyEmbedded {
Integer a
String b

static constraints = {
a(nullable: true)
b(nullalbe: true)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.grails.orm.hibernate.compiler

import groovy.transform.Generated
import org.hibernate.engine.spi.EntityEntry
import org.hibernate.engine.spi.ManagedEntity
import org.hibernate.engine.spi.PersistentAttributeInterceptable
import org.hibernate.engine.spi.PersistentAttributeInterceptor
Expand Down Expand Up @@ -153,5 +155,16 @@ class MyEntity {

then:"The value is changed"
myEntity.name == 'changed'

and: "by transformation added methods are all marked as Generated"
cls.getMethod('$$_hibernate_getInterceptor').isAnnotationPresent(Generated)
cls.getMethod('$$_hibernate_setInterceptor', PersistentAttributeInterceptor).isAnnotationPresent(Generated)
cls.getMethod('$$_hibernate_getEntityInstance').isAnnotationPresent(Generated)
cls.getMethod('$$_hibernate_getEntityEntry').isAnnotationPresent(Generated)
cls.getMethod('$$_hibernate_setEntityEntry', EntityEntry).isAnnotationPresent(Generated)
cls.getMethod('$$_hibernate_getPreviousManagedEntity').isAnnotationPresent(Generated)
cls.getMethod('$$_hibernate_getNextManagedEntity').isAnnotationPresent(Generated)
cls.getMethod('$$_hibernate_setPreviousManagedEntity', ManagedEntity).isAnnotationPresent(Generated)
cls.getMethod('$$_hibernate_setNextManagedEntity', ManagedEntity).isAnnotationPresent(Generated)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DataSourceConnectionSourceFactorySpec extends Specification {
when:
DataSourceConnectionSourceFactory factory = new DataSourceConnectionSourceFactory()
Map config = [
'dataSource.url':"jdbc:h2:mem:grailsDB;LOCK_TIMEOUT=10000",
'dataSource.url':"jdbc:h2:mem:dsConnDsFactorySpecDb;LOCK_TIMEOUT=10000",
'dataSource.dbCreate': 'update',
'dataSource.dialect': Oracle8iDialect.name,
'dataSource.properties.dbProperties': [useSSL: false]
Expand Down

0 comments on commit 59d21e2

Please sign in to comment.