gorm

Can you combine controller list params with Hibernate criteria?

。_饼干妹妹 提交于 2019-12-08 02:27:29
问题 I have noticed that you can pass "params" straight in to the boilerplate code below: [fooInstanceList: Foo.list(params), fooInstanceTotal: Foo.count()] Is it possible to pass "params" in as part of a Hibernate criteria for example the one below? def c = Foo.createCriteria() def results = c { not { eq("bar","test") } } [fooInstanceList: results, fooInstanceTotal: results.size()] I am looking to use the "max" and "offset" params so I can use it for paging for example. I would also like to use

How to convert javassist instance into instance of a specific domain

泄露秘密 提交于 2019-12-08 02:14:51
问题 I would like to intersect two lists of instances of a specific domain class using grails 1.3.7. The problem is, that the instances of one list are created by javasisst so that the result of the intersection is always an empty list. Here are my domains: class User { ... static hasMany = [foos : Foo] ... } class Foo { ... static hasMany = [bars : Bar] ... } class Bar { ... static hasMany = [localizedTitles : LocalizedTitle] ... } I get the list of all Bar instances of the user like this: def

Renaming composite foreign keys in GORM

独自空忆成欢 提交于 2019-12-08 02:03:58
问题 I have the following classes: class Catalog { static mapping = { id composite:['name', 'manufacturer'] columns { name column:'cat_name' manufacturer column:'manuf_id' } } String name Manufacturer manufacturer } class Order { static mapping = { columns { // How to rename foreign keys as cat_name, manuf_id? } } Catalog catalog // creates catalog_name, catalog_manufacturer_name } Presently, an Order table is generated with the attributes catalog_name and catalog_manufacturer_name (which

Grails domain class beforeDelete is not working as transactional

穿精又带淫゛_ 提交于 2019-12-08 02:01:48
问题 I have an user domain class with attributes like username, fullName, ... etc. and a UserRole association class. In my domain class I have the following code on the beforeDelete method def beforeDelete() { UserRole.removeAll(this); } In the UserRole class I have the removeAll method like this: static void removeAll(User user) { executeUpdate 'DELETE FROM UserRole WHERE user=:user', [user: user] } The call to the delete method is done in my UserService class def delete(User userInstance){

How to change a Domain Class model object from one derived class to another in Grails/GORM

筅森魡賤 提交于 2019-12-08 01:59:24
问题 Given the following Grails GORM Domain Classes and using table-per-hierarchy inheritance: class Book { static belongsTo = [ parent: Parent ] String title } abstract class Parent { static hasMany = [ books: Book ] } class A extends Parent { String asset } class B extends Parent { String asset } Say I have retrieved an instance of class A from the database. I want to convert it to an instance of class B. What is the grails idiomatic way to do this? Without the hasMany relation, I would just

Grails 3 and Java 8 time support

折月煮酒 提交于 2019-12-08 01:34:07
问题 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? 回答1: Edit : Grails 3.1.1 has hibernate 5 so this is not

Grails, GORM, relationship. Optional child records

荒凉一梦 提交于 2019-12-07 23:13:38
问题 For example, I've parent class Cafee: class Cafee { String name static hasMany = [ admin: Person ] } and a child class Person: class Person { String name static belongsTo = [cafee: Cafee] } I've done some records to Cafee using: def user = new Person(name: "Andrew") def a = new Cafee(name: "Tarelka") .addToAdmin(user) .save() Adding child to parent works fine, but when I trying to create Person-instance separately, for example: def visitor = new Person(username: 'testerUser', password:

Grails “max” subquery with an association, to get only the latest of a hasMany

拥有回忆 提交于 2019-12-07 22:58:26
问题 The simplified domain model: 'Txn' (as in Transaction) hasMany 'TxnStatus'. TxnStatus has a dateTime This is a legacy mapping so I cant change the DB, the mapping on Txn: static mapping = { txnStatus column: 'MessageID', ignoreNotFound: true, fetch: 'join' } I need to get Txns based on a number of dynamically built criteria, currently using GORM's 'where' query, it works well; BUT I need to also get only the latest txnStatus. Tried: def query = Txn.where { txnStatus { dateTime == max(dateTime

Using traits for horizontal domain class reuse in Grails, a good idea?

早过忘川 提交于 2019-12-07 21:46:49
问题 So I want to create 3 plugins which include domain classes and a restful service, and who each build on top of each other. Conceptually, they would "inherit" the base model this way: Record > Person > User However I have Read From The Friendly Manual that inheritance may cause some performance issues. Then it crossed my mind that since Groovy has horizontal reuse capabilities (i.e. traits), I may very well just define everything in the trait and then implement the trait in the domain class.

org.hibernate.LazyInitializationException in a Quartz job

十年热恋 提交于 2019-12-07 17:13:58
问题 I can use dynamic finders on my domain classes in my Quartz job, but get org.hibernate.LazyInitializationException when accessing relationships. I thought they would either both work, or none. class MyJob { def author = Author.list().first() // fine def book = Book.get(1) // fine println author.books // lazy exception } Any idea why this might be happening? According to the Quartz plugin documentation each job thread gets a Hibernate session, yet I'm running into this problem. Grails 2.1.1 ,