Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GORM for Hibernate in Groovy Script
@Grab("org.grails:grails-datastore-gorm-hibernate4:3.0.0.RELEASE")
@Grab("org.grails:grails-spring:2.3.6")
@Grab("com.h2database:h2:1.3.164")
import grails.orm.bootstrap.*
import grails.persistence.*
import org.springframework.jdbc.datasource.DriverManagerDataSource
import org.h2.Driver
init = new HibernateDatastoreSpringInitializer(Person)
def dataSource = new DriverManagerDataSource(Driver.name, "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE", 'sa', '')
init.configureForDataSource(dataSource)
println "Total people = " + Person.count()
@Entity
class Person {
String name
static constraints = {
name blank:false
}
}
@tbarker9

This comment has been minimized.

Show comment
Hide comment
@tbarker9

tbarker9 Jan 30, 2014

Nice. I presume this is not available yet?

Nice. I presume this is not available yet?

@graemerocher

This comment has been minimized.

Show comment
Hide comment
@graemerocher

graemerocher Feb 4, 2014

Nope not yet

Owner

graemerocher commented Feb 4, 2014

Nope not yet

@tbarker9

This comment has been minimized.

Show comment
Hide comment
@tbarker9

tbarker9 Feb 4, 2014

Looking forward to it! This was my number 1 gripe with grails.... I couldn't use it for scripting or other apps! Will this be in 2.4?

tbarker9 commented Feb 4, 2014

Looking forward to it! This was my number 1 gripe with grails.... I couldn't use it for scripting or other apps! Will this be in 2.4?

@alvarosanchez

This comment has been minimized.

Show comment
Hide comment
@alvarosanchez

alvarosanchez Feb 18, 2014

Will this work with JPA / Hibernate entities? Or not that lucky?

Will this work with JPA / Hibernate entities? Or not that lucky?

@quintonm

This comment has been minimized.

Show comment
Hide comment
@quintonm

quintonm Mar 16, 2014

It is available now... Released s part of grails 2.3.6.

http://grails.org/2.3.6+Release+Notes

It is available now... Released s part of grails 2.3.6.

http://grails.org/2.3.6+Release+Notes

@pinkymanga

This comment has been minimized.

Show comment
Hide comment
@pinkymanga

pinkymanga Mar 25, 2014

somebody knows how to use it into a dropwizard project? following the sample of tomas lin in restful web services with dropwizard and groovy

https://github.com/tomaslin/dropwizard-gradle-groovy

somebody knows how to use it into a dropwizard project? following the sample of tomas lin in restful web services with dropwizard and groovy

https://github.com/tomaslin/dropwizard-gradle-groovy

@woodmawa

This comment has been minimized.

Show comment
Hide comment
@woodmawa

woodmawa Mar 31, 2014

ok having a problem with this

you indicate that you can package binary grails plugin - i tried that and generated a couple of domain classes and generated a jar.

copyied that into gradel script project - and included the jar into the classpath

when you try and use the domain classes though i get this error

Caught: java.lang.NoClassDefFoundError: Lorg/codehaus/groovy/grails/plugins/web/api/ControllersDomainBindingApi;
java.lang.NoClassDefFoundError: Lorg/codehaus/groovy/grails/plugins/web/api/ControllersDomainBindingApi;
at com.softwood.TestScript.run(TestScript.groovy:28)
Caused by: java.lang.ClassNotFoundException: org.codehaus.groovy.grails.plugins.web.api.ControllersDomainBindingApi
... 1 more
presumably because the plugin includes all sorts of controller/web stuff - which to use the domain classes i dont need
without having to put all your entity models outside of grails, how can you bootstrap grails from scripts in a grails project? when you try and run a script - it says your outside of grails and wont let you use the domain classes in your project without starting the web server.

i'd like to use the gorm elements without loading the full grails env runtime

how do i do that?

ok having a problem with this

you indicate that you can package binary grails plugin - i tried that and generated a couple of domain classes and generated a jar.

copyied that into gradel script project - and included the jar into the classpath

when you try and use the domain classes though i get this error

Caught: java.lang.NoClassDefFoundError: Lorg/codehaus/groovy/grails/plugins/web/api/ControllersDomainBindingApi;
java.lang.NoClassDefFoundError: Lorg/codehaus/groovy/grails/plugins/web/api/ControllersDomainBindingApi;
at com.softwood.TestScript.run(TestScript.groovy:28)
Caused by: java.lang.ClassNotFoundException: org.codehaus.groovy.grails.plugins.web.api.ControllersDomainBindingApi
... 1 more
presumably because the plugin includes all sorts of controller/web stuff - which to use the domain classes i dont need
without having to put all your entity models outside of grails, how can you bootstrap grails from scripts in a grails project? when you try and run a script - it says your outside of grails and wont let you use the domain classes in your project without starting the web server.

i'd like to use the gorm elements without loading the full grails env runtime

how do i do that?

@graemerocher

This comment has been minimized.

Show comment
Hide comment
@graemerocher

graemerocher Apr 4, 2014

You can compile the domain classes separately in a Gradle or Maven project. See for example

https://github.com/spring-guides/gs-accessing-data-gorm/tree/master/complete

Note that the project uses Spring Boot, but you can omit the boot parts if you don't want it

Owner

graemerocher commented Apr 4, 2014

You can compile the domain classes separately in a Gradle or Maven project. See for example

https://github.com/spring-guides/gs-accessing-data-gorm/tree/master/complete

Note that the project uses Spring Boot, but you can omit the boot parts if you don't want it

@nvinet

This comment has been minimized.

Show comment
Hide comment
@nvinet

nvinet Jul 10, 2014

Hi guys,

I'm giving a go using GORM outside Grails and the documentation is quite sparse, especially when not using Spring-Boot and around the unit testing. Grails has tons of test mixins around GORM. Is there a strategy to adopt when testing GORM outside Grails?

nvinet commented Jul 10, 2014

Hi guys,

I'm giving a go using GORM outside Grails and the documentation is quite sparse, especially when not using Spring-Boot and around the unit testing. Grails has tons of test mixins around GORM. Is there a strategy to adopt when testing GORM outside Grails?

@rodrigorodriguescosta

This comment has been minimized.

Show comment
Hide comment
@rodrigorodriguescosta

rodrigorodriguescosta Feb 11, 2015

OK, it works like we have in this script, but if I increment a little bit the example something does not work as expected, example, the GORM does not inject addTo method to the class, see

@entity
class Person {
String name
Profile profile

static hasMany = [contacts:Contacts]

static constraints = {
    name blank:false
}

}

@entity
class Profile {
String name
}

@entity
class Contacts {
static belongsTo = [person: Person]
String name
}

Profile profile = new Profile(name: "Profile1")
profile.save(flush: true)

Person p = new Person(name: "RODRIGO", profile: profile)

p.addToContacts(name: "contact1")

if(!p.save()){
puts p.errors
}

println "Total people = " + Person.count()

it reproduces the message : groovy.lang.MissingMethodException: No signature of method: Person.addToContacts() is applicable for argument types: (java.util.LinkedHashMap) values: [[name:contact1]]
Possible solutions: getContacts()

See? is there any workaround?

OK, it works like we have in this script, but if I increment a little bit the example something does not work as expected, example, the GORM does not inject addTo method to the class, see

@entity
class Person {
String name
Profile profile

static hasMany = [contacts:Contacts]

static constraints = {
    name blank:false
}

}

@entity
class Profile {
String name
}

@entity
class Contacts {
static belongsTo = [person: Person]
String name
}

Profile profile = new Profile(name: "Profile1")
profile.save(flush: true)

Person p = new Person(name: "RODRIGO", profile: profile)

p.addToContacts(name: "contact1")

if(!p.save()){
puts p.errors
}

println "Total people = " + Person.count()

it reproduces the message : groovy.lang.MissingMethodException: No signature of method: Person.addToContacts() is applicable for argument types: (java.util.LinkedHashMap) values: [[name:contact1]]
Possible solutions: getContacts()

See? is there any workaround?

@ysb33r

This comment has been minimized.

Show comment
Hide comment
@ysb33r

ysb33r Jan 1, 2016

There is quite a number of failures with this if trying to use it with 5.0.0.M1

ysb33r commented Jan 1, 2016

There is quite a number of failures with this if trying to use it with 5.0.0.M1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment