Grails 2.1: setting sessionFactory and dataSource from Spring user configuration

I have a complex, customizable Hibernate setup in Spring (including JPA objects, a factory session, and data source definitions) that I want to use in Grails 2.1.0. Because of this, I want to give Grails a reference to sessionFactory and dataSource , which I already have it. Therefore, I do not want (and actually can not) use hibernate.cfg.xml , which is placed in conf/ , and I do not want to use DataSource.groovy , since all the complicated configuration is already processed by the tested and working code we already have and all spring.

So, I was able to configure my Spring configuration to load on grails run-app (via importBeans() in resources.groovy .) In the logs, I see a db connection, Spring config and Hibernate start is just fine, so beans to sessionFactory are created at sessionFactory and dataSource . Now, how do you configure Grails to use them, rather than trying to create your own?

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

I saw it too:

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

but it doesn't work (not sure if it has ever been.)

Any help would be greatly appreciated ... I spent the last 10 hours trying to get this to work to no avail. I don't mind if I lose some Grails features while it works. The immediate goal is to get GORM to view (~ 200) objects that we already have, and create some forests :)

I also know that entities are not visible to Grails, because I added the following to BootStrap.groovy:

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

And he prints [].

If a patch is required, just give me a rough idea of ​​where to start, and I'll see ... I just want it to work.

Thanks!

Update 1:

I tried several spells 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 xyzClass. It is not a domain class! 

(the log line has been edited: replaced the actual class name with xyzClass .) As I said, the list of entities is empty, and I see no way to configure the Hibernate sessionFactory - for example

 sessionFactory = ref('sessionFactory') 

Does not work.

Update 2:

Using beans and entities downloaded from Spring but not used or noticed by GORM, I was able to force the objects to be converted using the utility built into Grails and a new bean configured with resources.groovy this way:

 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' } } 

Entities now see Grails, but scaffolds do not work, because extended domain objects do not seem to have GORM methods (.list (), etc.). You expect GrailsHibernateUtil.configureHibernateDomainClasses() to add these methods when it creates all the GrailsHibernateDomainClass classes, but either it fails or I am missing something (maybe it doesn't work early enough, not sure.) Any help very much appreciated.

+6
source share
1 answer

Have you tried the other way around using the db-reverse-engineer plugin? We had great success when porting a rather complicated Spring application to grails (about 90 entities).

+1
source

Source: https://habr.com/ru/post/924643/


All Articles