Table of contents
Grails Notes
Access to session and hibernate
getting session
def sessionFactory
def session = sessionFactory?.getCurrentSession()
RequestContextHolder.currentRequestAttributes().getSession()
get hibernate datastore in session
Get hibernatedatasource
public class HibernateExample { @Autowired HibernateDatastore hibernateDatastore; @Autowired DatabaseProvisioningService databaseProvisioningService; @Listener(User) void onUserPostInsertEvent(PostInsertEvent event) { String username = event.getEntityAccess().getPropertyValue("username"); DatabaseConfiguration databaseConfiguration = databaseProvisioningService.findDatabaseConfigurationByUsername(username); hibernateDatastore.getConnectionSources().addConnectionSource(databaseConfiguration.dataSourceName, databaseConfiguration.configuration); } }
get table columns
hibernateDatastore.getSessionFactory().getClassMetadata(GroupCompare).getProperties().sort()
ctx.sessionFactory.getClassMetadata(Team).attributes.collect { it.name }
get data bindings/properties/class/domain table/declared fields
def mapping = org.grails.orm.hibernate.cfg.GrailsDomainBinder.getMapping(UserGroup)
def mapping = org.grails.orm.hibernate.cfg.GrailsDomainBinder.getMapping(UserGroup)
org.grails.orm.hibernate.cfg.GrailsDomainBinder.getMapping(groupCompare.class).class.declaedFields
Get A Service
public class HibernateExample { @Autowired HibernateDatastore hibernateDatastore; UserDataService userDataService; UserService(HibernateDatastore hibernateDatastore) { this.userDataService = hibernateDatastore.getService(UserDataService); } }
(YourService)Holders.grailsApplication.mainContext["yourService"];
applicationContext."${yourServiceName}".serviceMethod();
ctx.getBean("userGroupService");
Holders.applicationContext.getBean("myService");
class HibernateExample {
ApplicationContext ctx = (ApplicationContext) ServletContextHolder
.getServletContext()
.getAttribute(GrailsApplicationAttributes
.APPLICATION_CONTEXT)
def statisticsService = (StatisticsService).ctx.getBean("statisticsService ")
}