I'm running a Grails 2.2.4 application with multiple data sources. One requirement is to provide auditing with Hibernate Envers. I did the following:
Domain classes are annotated with org.hibernate.envers.Audited
org.hibernate:hibernate-envers:3.6.10.Final is in the classpath
Hibernate event listeners are defined as follows. This should work with all defined data sources.
beans {
auditEventListener(AuditEventListener)
hibernateEventListeners(HibernateEventListeners) {
listenerMap = [
'post-insert': auditEventListener,
'post-update': auditEventListener,
'post-delete': auditEventListener,
'pre-collection-update': auditEventListener,
'pre-collection-remove': auditEventListener,
'post-collection-recreate': auditEventListener
]
}
}
However, no audit entries are inserted into the revision tables. Has anyone a hint how to fix this? Is this still an issue with the latest Grails version? I could invest the effort to upgrade. Note, I am using just Hibernate Envers. I do not use any Grails plugin. There is a post on nabble.com, but this did not work form.
You can use audit log plugin for this. Audit log plugin in grails
Also. see the following for multiple datasource click me
Or try to use
Grails Envers plugin
Related
In grails 3 application, I see that new java.time classes are persisted to database. Dynamic queries and create criteria works. However, I am wondering if there is some better way how to store these time data to database. When I look to database I see some binary data, it seems it just serialize the time objects.
Is there any similar plugin as joda-time-plugin or is there any way how to configure database mapping for java.time classes?
Edit: Grails 3.1.1 has hibernate 5 so this is not an issue anymore...
Grails
I finally found a solution. First of all hibernate 5 implements persistent for java 8 time (https://hibernate.atlassian.net/browse/HHH-8844). However, grails 3 uses hibernate 4 and I haven't found any plans to upgrade hibernate in grails.
There is an implementation for java 8 time hibernate user types (jadira extended package) which can be used similarly as in joda time plugin (see http://gpc.github.io/joda-time/guide/persistence.html). For me only version 3.1.0.GA and lower worked.
build.gradle
dependencies {
compile "org.jadira.usertype:usertype.extended:3.1.0.GA"
}
application.groovy
grails.gorm.default.mapping = {
"user-type" type: org.jadira.usertype.dateandtime.threeten.PersistentInstantAsMillisLong, class: java.time.Instant
}
I know there is a Flyway2 plugin. However im not satisfied since it seems fitted for working by console commands. What i want is to integrate Flyway in a programatic way so:
1st Integration tests use flyway to handle db schema with H2 database
2nd Flyway get's triggered on tomcat deployment and handles also the environment database (maybe through running it from bootstrap?)
Does anyone has experienced with this?
EDIT after some discussion:
In order to use the plugin i would need to get a fully configured instance of GFlyway from spring context. This becomes difficult since the bean only property is def config from where it will read all the required properties. The question is how to replicate this behavior within the resources.groovy ... how to provide the application config as a parameter to the bean.
As we have been discussing in the comments, the correct way to configure this as a bean would be:
// Resources.groovy
beans {
grailsApplication = ref('grailsApplication')
gFlyaway(gflyway2.GFlyway) {
config = grailsApplication.config
}
}
Configure the settings as usual within your Config.groovy per the documentation of the plugin.
That should get you closer, if not all the way there.
First of all, I am new to both Grails and Vaadin, I'm trying to make it work according to docs. I am using Grails 2.2.4 and Vaadin 7.1.8.2. I installed the plugin using:
$ grails install-plugin vaadin 7.1.8.2
and put the plugin into BuildConfig.groovy:
compile ':vaadin:7.1.8.2'
I have made a basic Vaadin UI that works (even generated from Eclipse designer - great stuff), I can modify it and it auto-reloads. Fine. I got the URL mapping set in VaadinConfig.groovy so that I can use the traditional Grails CRUD controllers as well as my Vaadin UI:
mapping = [
"/vaadin": "app.MyUI"
]
contextRelativePath = "/vaadin"
I was hoping I could use the Vaadin-style CRUD scaffolding of domain classes as in http://mckenfra.github.io/grails-vaadin-plugin/source-code/docs/ref/Command%20Line/generate-vaadin-all.html but I can't generate it:
$ grails generate-vaadin-all "*"
| Script 'GenerateVaadinAll' not found, did you mean:
1) GenerateAll
2) GenerateViews
3) GenerateController
4) DbmGenerateChangelog
5) DbmGenerateGormChangelog
I'm getting the same if I try generate-vaadin-views or generate-vaadin-controllers. What could I be doing wrong ?
Thanks
You can try RefreshDependencies before GenerateVaadinAll.
I run my Grails application using ehcache for my 2nd level Cache and it works. I installed the ehcache plugin + cache plugin and then it doesn't. I tried almost all solutions from the internet and found no solution I keep getting Another unnamed CacheManager already exists in the same VM.
One of the possible solutions is to set p:shared=true in the EhCacheManagerFactoryBean, this works if I use an old plugin "springcache plugin from grails" but with the new plugin they use a modified version of this manager and the property shared is not available.
I tried defining a new ehcache.xml file but still I can not put inside a new name for this cache manager.
I tried changing the cache.provider class inside my DataSource.groovy to use one another EhCacheProvider such as a Singleton.
Needless to say, I tested putting a different name using DSL in different places but still no luck.
At the end I'm using the old plugin for spring cache which is deprecated. Can anybody help?
I'm using Grails 2.0.3 and ehcache-core:2.5.3.
In the hibernate section of DataSource.groovy, make sure your cache.provider.class is up to date:
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = true
cache.region.factory_class = 'grails.plugin.cache.ehcache.hibernate.BeanEhcacheRegionFactory' // For Hibernate before 4.0
cache.region.factory_class = 'grails.plugin.cache.ehcache.hibernate.BeanEhcacheRegionFactory4' // For Hibernate before 4.0 and higher
}
I had the same problem because my cache.region.factory_class was outdated: net.sf.ehcache.hibernate.EhCacheProvider.
See http://grails-plugins.github.io/grails-cache-ehcache/guide/usage.html
For those having this error with Grails 2.5.x, just add this to Config.groovy :
beans {
cacheManager {
shared = true
}
}
This solved the problem for me.
source : https://github.com/grails/grails-core/releases/tag/v2.5.0
Try to use cache & cache-ehcache plugins, it works for me with some limitations. But for 2ndlevel Cache it work correctly
For people arriving here getting the same error as OP might consider that error might be caused by an update of a domain class at runtime (hot code swap), which is not done nicely in alle versions of Grails.
I hit this bug with Grails 2.5.4, yet with the application restart button as the only option to solve.
Update: as of Grails 1.3.6 one has access to the full domain from Gant scripts.
From the Grails 1.3.6 release notes:
You can now run one or more Groovy scripts from the commandline using the run-script command, e.g.
grails run-script [path-to-script-1] [path-to-script-2]...[path-to-script-n]
This works around the issue in Gant scripts where you can't conveniently access application classes since they're not available in the classpath when the scripts start.
Hi all,
I am new to using Grails (in a real project) and I have a one-off script I need to execute that reads a file and then populates my database.
I wanted the script to run in the context of my grails app, so I used the create-script command. I now understand that makes it a 'Gant' script. The reason for doing so was that I thought it would allow me easy access to all the grails domain good-ness, so that i would be able to do something like this easily:
Car car = new Car(model: 'bar', brand: 'Ford')
car.save()
Here, Car is one of my domain classes and the strings 'bar' and 'Ford' I have retrieved from my file.
The start of my script looks like this:
import com.foo.Car
grailsHome = Ant.project.properties."environment.GRAILS_HOME"
includeTargets << new File ( "${grailsHome}/scripts/Bootstrap.groovy" )
target(main: "a script for storing cars") {
depends(bootstrap, classpath) // code dealing with the file with cars follows
Surprisingly, groovy gives me a java.lang.NoClassDefFoundError: com.foo.Car when I execute the script with the command grails LoadCars
Am I taking the wrong approach, or is there something more simple I am doing wrong?
Any help is appreciated
i know the scripts are useful, and I will probably get hate mail for even suggesting it, but I have just incorporating this kinda of stuff directly into my application in the past.
I have a flag set in my configuration which indicates if the data should be bootstrapped, if so, the bootstrap code looks for a comma delimited file at startup and calls a service method to load up the data.
I've updated the grails run-script Gant script (referred to by Jared above) to work with grails 1.3.5. I'd been meaning to do it for a while, but this question nudged me into finally getting around to it).
Just download the script described in the post, save it in your grails "scripts" directory and you can then run your own groovy script to bootstrap data with:
grails run-script script-path/boostrapMyDataIntoApp.groovy
I've had to do this and you have to create a special script to allow you to access GORM from a standard grails script. See this question for more info. I'm not sure what the current status of the script is under grails 1.3 but the author of the script posted in the comments.
Hans, there are several choices here, assuming you are not out to polish the GANT scripting chops 8^)
So assume that you are doing some integration-mode TDD, correct?
Have you looked into the db-stuff plugin? Actually that one leverages the open source package (extension of the JUnit project) called dbUnit, which is also an outstanding choice, for both Java and Groovy projects.
*db-stuff <0.3.0> -- db schema managment and data import/export. Generate generic schema files and import or export base/seed/test data into your database.
I have traditionally done this as well in the BootStrap depending on the environment - and I try to never let those domain assumptions / constraints get too far out of synch. with my schema.
Here's the canon I'm talking about :
class BootStrap {
def init = { servletContext ->
if (GrailsUtil.environment.equals( GrailsApplication.ENV_DEVELOPMENT )) {
log.info( "Loading sample data for 2010 models..." );
new Car( manufacturer: new Manufacturer( name: "Toyota" ), model: "Prius" )
new Car( manufacturer: new Manufacturer( name: "GM" ), model: "Volt" )
//...