org.codehaus.groovy.grails.orm.hibernate.cfg
Class GrailsDomainBinder

java.lang.Object
  extended by org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder

public final class GrailsDomainBinder
extends java.lang.Object

Handles the binding Grails domain classes and properties to the Hibernate runtime meta model. Based on the HbmBinder code in Hibernate core and influenced by AnnotationsBinder.

Since:
0.1

Field Summary
static java.util.Map<java.lang.String,org.hibernate.cfg.NamingStrategy> NAMING_STRATEGIES
          Overrideable naming strategy.
 
Constructor Summary
GrailsDomainBinder()
           
 
Method Summary
static void bindClass(org.codehaus.groovy.grails.commons.GrailsDomainClass domainClass, org.hibernate.cfg.Mappings mappings, java.lang.String sessionFactoryBeanName)
          Binds a Grails domain class to the Hibernate runtime meta model
protected static void bindNumericColumnConstraints(org.hibernate.mapping.Column column, org.codehaus.groovy.grails.validation.ConstrainedProperty constrainedProperty)
          Interrogates the specified constraints looking for any constraints that would limit the precision and/or scale of the property's value.
static void bindRoot(org.codehaus.groovy.grails.commons.GrailsDomainClass domainClass, org.hibernate.cfg.Mappings mappings, java.lang.String sessionFactoryBeanName)
          Binds a root class (one with no super classes) to the runtime meta model based on the supplied Grails domain class
protected static void bindStringColumnConstraints(org.hibernate.mapping.Column column, org.codehaus.groovy.grails.validation.ConstrainedProperty constrainedProperty)
          Interrogates the specified constraints looking for any constraints that would limit the length of the property's value.
static void configureNamingStrategy(java.lang.Object strategy)
          Override the default naming strategy for the default datasource given a Class or a full class name.
static void configureNamingStrategy(java.lang.String datasourceName, java.lang.Object strategy)
          Override the default naming strategy given a Class or a full class name.
protected static void createClassProperties(org.codehaus.groovy.grails.commons.GrailsDomainClass domainClass, org.hibernate.mapping.PersistentClass persistentClass, org.hibernate.cfg.Mappings mappings, java.lang.String sessionFactoryBeanName)
          Creates and binds the properties for the specified Grails domain class and PersistentClass and binds them to the Hibernate runtime meta model
static org.codehaus.groovy.grails.orm.hibernate.cfg.Mapping evaluateMapping(org.codehaus.groovy.grails.commons.GrailsDomainClass domainClass)
          Evaluates a Mapping object from the domain class if it has a mapping closure
static org.codehaus.groovy.grails.orm.hibernate.cfg.Mapping evaluateMapping(org.codehaus.groovy.grails.commons.GrailsDomainClass domainClass, groovy.lang.Closure<?> defaultMapping)
           
static org.codehaus.groovy.grails.orm.hibernate.cfg.Mapping evaluateMapping(org.codehaus.groovy.grails.commons.GrailsDomainClass domainClass, groovy.lang.Closure<?> defaultMapping, boolean cache)
           
static org.codehaus.groovy.grails.orm.hibernate.cfg.Mapping getMapping(java.lang.Class<?> theClass)
          Obtains a mapping object for the given domain class nam
static org.codehaus.groovy.grails.orm.hibernate.cfg.Mapping getMapping(org.codehaus.groovy.grails.commons.GrailsDomainClass domainClass)
          Obtains a mapping object for the given domain class nam
static org.codehaus.groovy.grails.orm.hibernate.cfg.PropertyConfig getPropertyConfig(org.codehaus.groovy.grails.commons.GrailsDomainClassProperty property)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NAMING_STRATEGIES

public static java.util.Map<java.lang.String,org.hibernate.cfg.NamingStrategy> NAMING_STRATEGIES
Overrideable naming strategy. Defaults to ImprovedNamingStrategy but can be configured in DataSource.groovy via hibernate.naming_strategy = ....

Constructor Detail

GrailsDomainBinder

public GrailsDomainBinder()
Method Detail

configureNamingStrategy

public static void configureNamingStrategy(java.lang.Object strategy)
                                    throws java.lang.ClassNotFoundException,
                                           java.lang.InstantiationException,
                                           java.lang.IllegalAccessException
Override the default naming strategy for the default datasource given a Class or a full class name.

Parameters:
strategy - the class or name
Throws:
java.lang.ClassNotFoundException
java.lang.InstantiationException
java.lang.IllegalAccessException

configureNamingStrategy

public static void configureNamingStrategy(java.lang.String datasourceName,
                                           java.lang.Object strategy)
                                    throws java.lang.ClassNotFoundException,
                                           java.lang.InstantiationException,
                                           java.lang.IllegalAccessException
Override the default naming strategy given a Class or a full class name.

Parameters:
datasourceName - the datasource name
strategy - the class or name
Throws:
java.lang.ClassNotFoundException
java.lang.InstantiationException
java.lang.IllegalAccessException

getPropertyConfig

public static org.codehaus.groovy.grails.orm.hibernate.cfg.PropertyConfig getPropertyConfig(org.codehaus.groovy.grails.commons.GrailsDomainClassProperty property)

bindClass

public static void bindClass(org.codehaus.groovy.grails.commons.GrailsDomainClass domainClass,
                             org.hibernate.cfg.Mappings mappings,
                             java.lang.String sessionFactoryBeanName)
                      throws org.hibernate.MappingException
Binds a Grails domain class to the Hibernate runtime meta model

Parameters:
domainClass - The domain class to bind
mappings - The existing mappings
sessionFactoryBeanName - the session factory bean name
Throws:
org.hibernate.MappingException - Thrown if the domain class uses inheritance which is not supported

evaluateMapping

public static org.codehaus.groovy.grails.orm.hibernate.cfg.Mapping evaluateMapping(org.codehaus.groovy.grails.commons.GrailsDomainClass domainClass)
Evaluates a Mapping object from the domain class if it has a mapping closure

Parameters:
domainClass - The domain class
Returns:
the mapping

evaluateMapping

public static org.codehaus.groovy.grails.orm.hibernate.cfg.Mapping evaluateMapping(org.codehaus.groovy.grails.commons.GrailsDomainClass domainClass,
                                                                                   groovy.lang.Closure<?> defaultMapping)

evaluateMapping

public static org.codehaus.groovy.grails.orm.hibernate.cfg.Mapping evaluateMapping(org.codehaus.groovy.grails.commons.GrailsDomainClass domainClass,
                                                                                   groovy.lang.Closure<?> defaultMapping,
                                                                                   boolean cache)

getMapping

public static org.codehaus.groovy.grails.orm.hibernate.cfg.Mapping getMapping(java.lang.Class<?> theClass)
Obtains a mapping object for the given domain class nam

Parameters:
theClass - The domain class in question
Returns:
A Mapping object or null

getMapping

public static org.codehaus.groovy.grails.orm.hibernate.cfg.Mapping getMapping(org.codehaus.groovy.grails.commons.GrailsDomainClass domainClass)
Obtains a mapping object for the given domain class nam

Parameters:
domainClass - The domain class in question
Returns:
A Mapping object or null

bindRoot

public static void bindRoot(org.codehaus.groovy.grails.commons.GrailsDomainClass domainClass,
                            org.hibernate.cfg.Mappings mappings,
                            java.lang.String sessionFactoryBeanName)
Binds a root class (one with no super classes) to the runtime meta model based on the supplied Grails domain class

Parameters:
domainClass - The Grails domain class
mappings - The Hibernate Mappings object
sessionFactoryBeanName - the session factory bean name

createClassProperties

protected static void createClassProperties(org.codehaus.groovy.grails.commons.GrailsDomainClass domainClass,
                                            org.hibernate.mapping.PersistentClass persistentClass,
                                            org.hibernate.cfg.Mappings mappings,
                                            java.lang.String sessionFactoryBeanName)
Creates and binds the properties for the specified Grails domain class and PersistentClass and binds them to the Hibernate runtime meta model

Parameters:
domainClass - The Grails domain class
persistentClass - The Hibernate PersistentClass instance
mappings - The Hibernate Mappings instance
sessionFactoryBeanName - the session factory bean name

bindStringColumnConstraints

protected static void bindStringColumnConstraints(org.hibernate.mapping.Column column,
                                                  org.codehaus.groovy.grails.validation.ConstrainedProperty constrainedProperty)
Interrogates the specified constraints looking for any constraints that would limit the length of the property's value. If such constraints exist, this method adjusts the length of the column accordingly.

Parameters:
column - the column that corresponds to the property
constrainedProperty - the property's constraints

bindNumericColumnConstraints

protected static void bindNumericColumnConstraints(org.hibernate.mapping.Column column,
                                                   org.codehaus.groovy.grails.validation.ConstrainedProperty constrainedProperty)
Interrogates the specified constraints looking for any constraints that would limit the precision and/or scale of the property's value. If such constraints exist, this method adjusts the precision and/or scale of the column accordingly.

Parameters:
column - the column that corresponds to the property
constrainedProperty - the property's constraints