Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails 2.1: Setting sessionFactory and dataSource from custom Spring configuration

I've got a complex, custom-configured Hibernate setup in Spring (including JPA entities, session factory and data source definitions) that I want to use in Grails 2.1.0. Because of that, I want to give Grails a reference to the sessionFactory and dataSource that I already have. So, i do not want (and in fact, can't) use the hibernate.cfg.xml that's placed in conf/ - nor do I want to use DataSource.groovy, as all the complex configuration is already handled by tested and working code we already have and is all Spring-based.

So, I have managed to get my custom Spring configuration to load on grails run-app (through importBeans() in resources.groovy.) In the logs, I can see the db connection, Spring config and Hibernate starting up just fine, so at runtime the beans to sessionFactory and to the dataSource are created. Now, How do i configure Grails to use those and not try to create its own?

Ideally something like dataSource = ref('myDataSource') somewhere would be great - and the same with sessionFactory = ref('sessionFactory') or similar. I've seen some people putting that in resources.groovy, but it just doesn't work.

I've seen this too:

eventDao(com.JavaClassRequiringDataSource) { dataSource = ref('dataSource') }

but it does not work either (not sure if it ever did.)

Any help would be enormously appreciated … i've spent the last 10 hours trying to get this to work to no avail. I don't mind if I lose some Grails features, as long as it works. The immediate objective is to get GORM to see the (~200) entities we already have and do some scaffolding :)

I also know the entities are not seen by Grails because I've added the following to BootStrap.groovy:

// ...
def grailsApplication
def init = { servletContext -> 
  println grailsApplication.domainClasses
}
// ...

And it prints [].

If a patch is required, just give me a general idea of where to start and I'll take a look... I just want to get this working.

Thanks!

Update 1:

I've tried several incantations of the resources.groovy file, and currently it looks like this:

beans = {
    importBeans('main-spring-file-for-the-rest.xml')
    dataSource = ref('dataSource')
}

But when trying to scaffold I still get:

Error 2012-09-06 00:02:00,768 [Thread-9] ERROR plugins.DefaultGrailsPlugin  - Cannot generate controller logic for scaffolded class x.y.z.Class. It is not a domain class!

(Log line edited: replaced the actual name of the class with x.y.z.Class.) As I've shown before, the list of entities is empty, and I can see no way of setting up the Hibernate sessionFactory - for example

sessionFactory = ref('sessionFactory')

Doesn't work.

Update 2:

With the beans and entities loading from spring but not being used or seen by GORM, I was able to force the conversion of the entities using a utility built into Grails and a new bean, configured from resources.groovy thusly:

public class TestFix implements ApplicationContextAware {
    SessionFactory sessionFactory
    ApplicationContext applicationContext
    GrailsApplication grailsApplication

    def init() {
        GrailsHibernateUtil.configureHibernateDomainClasses(sessionFactory, "sessionFactory", grailsApplication)

    }
}

beans = {
    importBeans('main-spring-file-for-the-rest.xml')

    myBean(TestFix) { bean ->
        sessionFactory = ref('sessionFactory')
        grailsApplication = ref(GrailsApplication.APPLICATION_ID)
        bean.initMethod = 'init'
    }
}

Now the entities are seen by Grails but scaffolding doesn't work because the augmented domain objects seem to lack the GORM methods (.list() and such.) You would expect GrailsHibernateUtil.configureHibernateDomainClasses() to add those methods in when it creates all the GrailsHibernateDomainClass classes, but either it's failing silently or I'm missing something (perhaps not running early enough? not sure.) Any help very appreciated.

like image 256
Andres Kievsky Avatar asked Sep 05 '12 14:09

Andres Kievsky


1 Answers

Have you tried the other way around by using the db-reverse-engineer plugin? We had great success on migrating a fairly complex Spring application to grails (approx. 90 Entities).

like image 81
Bertl Avatar answered Oct 09 '22 13:10

Bertl