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
}
Related
I have a large number of Grails 2.5 applications that I want to upgrade to Grails 5, but have been unable to get the configuration to work. In particular, I want my plugin to set up the data source and Spring Security configuration as it did in Grails 2.5.
In my Grails 2.5 applications, I was able to add files to the configuration by adding this code to the top of Config.groovy.
if (!grails.config.location || !(grails.config.location instanceof List)) {
grails.config.location = []
}
grails.config.location << ["classpath:jcc-server-config.properties"]
grails.config.location << ["classpath:SecurityConfig.groovy"]
But this doesn't work in Grails 5. I've tried adding an application.groovy file, but everything defined in the application.yml seems to be set in stone. Has anybody found a way to add a Groovy file to the Grails configuration that will override or add to the settings in application.yml? YAML will not do because I have logic embedded in the configuration to make it work correctly in different environments.
Thanks.
Did you remember to include the external-config dependency? i.e.
implementation 'dk.glasius:external-config:3.0.0'
Re' your question on accessing config values this way, there should be no difference, in my apps I get to the config either via grailsApplication.config, or if grailsApplication isn't immediately available(e.g. classes under src), then with Holders, i.e. Holders.grailsApplication.config.
I am using log4j 1.2 with commons-logging. Now I am trying to upgrade it to log4j2.
But how to use log4j2 with commons-logging to initialize log4j2.
I tried to initialize commons logging in the below way. Its working fine
**Statement1**: static Log log = new Log4JLogger(Logger.getLogger(Example.class));
**Statement2**:log.debug("debug statement");
Here I am using object of type org.apache.commons.logging.Log initialized with object of org.apache.log4j.Logger.(org.apache.log4j.Logger is the class from log4j 1.2 where as from log4j2 is changed to org.apache.logging.log4j.Logger)
Now after I upgrade to log4j2, Statement1 will not work as Log4JLogger() constructor expects argument of type org.apache.log4j.Logger type.
So, how do I use commons logging with Log4j2?
You need to add the log4j-jcl-2.7 dependency to your classpath.
See the "which jars" question in the FAQ.
In your code, use
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MyClass {
private Log log = LogFactory.getLog(MyClass.class);
...
You should not explicitly use Log4JLogger.
Also, be aware that Log4j2 is different from Log4j 1 in that it offers a clean separation between its API and its implementation. So the benefits of using a wrapper library are much less now than they were 10 years ago with Log4j 1.
Consider using the Log4j2 API directly: it gives you the same separation between API and implementation and is more feature rich than commons logging or slf4j.
Note that there is little risk in using the Log4j2 API directly: the log4j-to-slf4j-2.x module is always there in case you change your mind and decide to use Logback (or another slf4j implementation) with an application that directly uses the Log4j2 API.
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
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" )
//...