gorm

Grails - sort by the domain relation attribute (using createCriteria())

落爺英雄遲暮 提交于 2019-12-05 21:46:17
I have two domain classes with 1:n relationship: import Action class Task { Action actionParent String taskName } and class Action { String actionName } I have the list of Tasks where I have the column "Action name", I would like to sort this column by Action.actionName. Now I'm using the createCriteria() method [I need to use it because I have more logic for filtering and sorting...], but I'm able to sort only by "Action.id". This method looks like: def criteria = Task.createCriteria(); taskList = criteria.list { if(parameters.max != null) maxResults(parameters.max) if(parameters.offset !=

Grails findAll with sort, order, max and offset?

陌路散爱 提交于 2019-12-05 19:01:46
问题 I want to integrate sort, order, max and offset in a findAll query. The following works fine: def books = Book.findAll("from Book as b where b.approved=true order by b.dateCreated desc", [max: max, offset: offset]) But what I want is: def books = Book.findAll("from Book as b where b.approved=true", [sort: 'dateCreated', order: 'desc', max: max, offset: offset]) This does not work. How do I have to rewrite this? 回答1: HQL doesn't support sort and order as parameters, so you need to include the

Handling Grails transactions programmatically

心不动则不痛 提交于 2019-12-05 18:13:51
问题 When I need to save a list of objects, and each object should be saved in it's own transaction (so that if one fails they don't all fail), I do it like this: List<Book> books = createSomeBooks() books.each { book -> Book.withNewSession { Book.withTransaction {TransactionStatus status -> try { book.save(failOnError: true) } catch (ex) { status.setRollbackOnly() } } } } I use Book.withNewSession because if one book fails to save and the transaction is rolled back, the session will be invalid

Grails global constraints

我的未来我决定 提交于 2019-12-05 14:44:20
In version 1.2, Grails introduced global constraints. I tried adding the following to Config.groovy grails.gorm.default = { constraints { notBlank(nullable:false, blank:false) } } Then using it in one of my domain classes static constraints = { email(email: true, unique: true, shared: 'notBlank') } But when I save a user with a null e-mail address, no errors are reported, why? Thanks, Don I've never tried to make global constraints, but I can tell you that if you want to mark a field as not blank and not nullable you don't need to create a new constraint at all, just add this to your domain

Grails groupProperty and order. How it works?

大城市里の小女人 提交于 2019-12-05 14:28:24
I have this domain : class Participation { ParticipationStatus status } class ParticipationStatus{ String name Date creationDate } I create a query : Participation.createCriteria().list{ createAlias("status","statusAlias") order "statusAlias.creationDate" projections{ groupProperty "id" } } But I received an error : Caused by: java.sql.SQLException: ORA-00979: N'est pas une expression GROUP BY I 'm working 2 days ago on this query grrrr ! ;-) Thanks a lot Every field you use in aggregate queries (the one using projections ) should be either a groupProperty , or only an aggregate function

Defining default sort-order in Grails/GORM

∥☆過路亽.° 提交于 2019-12-05 14:17:38
问题 Let's say I have definied a User object using GORM. Each user can have zero or more Login:s. Each Login has a timestamp. When retrieving user.logins I want the logins to be sorted based on the value of login.date. What is the correct Grails way to achieve this? Example: I want the following code to list all the user's logins in ascending order. <g:each var="login" in="${user.logins}"> <tr> <td>${login.date}</td> </tr> </g:each> These are the referenced classes: class User { ... def hasMany =

Grails: field access with GORM

亡梦爱人 提交于 2019-12-05 13:52:23
Hibernate uses method calls to get the values of domain class properties by default. How can I configure direct field access with GORM? It's not directly supported but will be in 1.4. For now you can enable it with a custom Configuration subclass as described at http://grails.1312388.n4.nabble.com/GORM-setting-access-quot-field-quot-td1592837.html#a1594428 I did a small post about subclassing Configuration with links to specific examples at http://burtbeckwith.com/blog/?p=465 I suggest this is due to how groovy implements Beans convention therefore not something that can be configured for GORM

GORM: What is reference:true in Grails domain class mapping block?

落爺英雄遲暮 提交于 2019-12-05 13:08:19
public class Address { static mapWith = "mongo" Region region; Zone zone; static mapping = { id generator: 'identity' region reference:true zone reference:true } } I'm interested in knowing what reference:true does. In my experience, leaving it off gives exactly the same result except there's no DBRef in the actual mongo document. It looks like reference controlls how documents are linked. When true , the related documents are referenced by db-refs , if false , GORM inserts simple id , aka Manual references in mongo This means that those properties will be stored on your Address record by

Grails GORM Criteria Query Eager Fetching

[亡魂溺海] 提交于 2019-12-05 10:36:06
I have written a criteria query in a Grails service class where I expect an eager join to be performed, and to avoid lazy loading of child objects when displaying my results either as a JSON response or in my GSP. The query executed as expected (setting my hibernate.show_sql=true in my DataSource.groovy I can see the query), but when I crawl the association in my GSP, I can see that Hibernate is executing subsequent queries as if it were lazily loading the associations. I'm not convinced that the eager loading is actually working. I do not want to set lazy:false within my domain class for

Case-insensitive unique constraint in Grails

偶尔善良 提交于 2019-12-05 10:17:31
How can I basically carry out a unique constraint on a string data-type field. class User{ String username String Email static hasMany = [roles:Roles] static constraints = { Email(email:true) username(unique:true) } } Is there any simple way to implement username(unique: true) Or must I manually check the database using methods like .findByNameLike ? The username should be unique, but the uniqueness is should be case-insensitive. So, if you want to have unique and case insensitive usernames, there are two possible approaches. The simple one: Store them upper or lower case and use the unique