gorm

List of Strings in a Grails domain class

故事扮演 提交于 2019-12-10 10:45:03
问题 I am trying to have a List of String that works in MySql in a Grails domain class. I have tried the following: class Catalogue { List books String book static hasMany = [books: book] } and class Catalogue { List books } and class Catalogue { String[] books } and class Catalogue { ArrayList<String> books = new ArrayList<String>() } The last three compiles but the entry is not present in MySQL. There is no table, or column to represent this data in MySQL and I have tried populating the array

Grails belongsTo cascade on delete when belongsTo specifies multiple classes?

放肆的年华 提交于 2019-12-10 10:03:25
问题 class Owner { static hasMany = Dog } class Sitter { static hasMany = Dog } class Dog { static belongsTo = [Owner, Sitter] } My question is: If I create a Dog instance D, a Owner instance O, a Sitter instance S and associate D with both O and S, what happens to O when S gets deleted? Would O still have D? Since it's a cascade-delete, both S and D would get deleted, right? When what happens to O? Would it still have D? 回答1: I have tested it, it follows the cascade rule: if you delete Owner, Dog

Difference between blank and null constraints

我的梦境 提交于 2019-12-10 01:47:43
问题 What is the difference between the blank and null constraints ? I have the following class class Task { String title String notes TekUser assignedTo Date dueDate TekEvent event static constraints = { title blank:false notes blank: true , maxSize: 5000 assignedTo nullable:true dueDate nullable:true } static belongsTo = TekEvent } and the mysql table created has the notes set to not null even though I specified notes blank : true What effect does blank : true have ? 回答1: blank:true means the

Group by week / month / year in GORM

风流意气都作罢 提交于 2019-12-09 23:43:50
问题 I have a domain class (minified) as :- class Expense { Date dateOfExpense int amount } I am trying to get sum of amount grouped by week/month/ year of expense date. Referring to 'sqlGroupProjection' method in grails doc http://grails.org/doc/latest/guide/GORM.html, I tried using following code:- def results = c { between("dateOfExpense", fromDate, toDate) projections { sqlGroupProjection 'dateOfExpense,sum(amount) as summed', 'MONTH(dateOfExpense)',['date','summed'],[DATE,NUMBER] } } Throws

Grails GORM: could not initialize proxy - no Session

依然范特西╮ 提交于 2019-12-09 14:54:37
问题 I have a method with the following structure: public void run(){ ... for (...) { //this part works correct User.withTransaction { User user = User.findByUsername(...); Position pos = Position.findByName(...) if(pos){ ... } else { ... try{ pos.save(flush:true); user.position = pos; } catch (Exception e){ ... } } ... try{ user.save(flush:true, failOnError: true); } catch (Exception e){ ... } } } //this part doesn't work User.findAll().each { ... if (...){ User.withTransaction{ ... //here the

How to override addTo* and RemoveFrom* GORM/Grails methods?

偶尔善良 提交于 2019-12-09 13:35:01
问题 I tried to override the dynamic method addTo* provided by Grails/GORM but it doesn't seem to work. Here is the code : class Match { static hasMany = [players: Player, matchPlayers: MatchPlayer] void addToPlayers(Player player) { if (players.add(player)) { MatchPlayer matchPlayer = new MatchPlayer(match: this, player: player) matchPlayers.add(matchPlayer) } } } ma = new Match().save() ma.addToPlayers(player1) The issue is that when calling addToPlayers I got the following exception: java.lang

Is there a 'not in' equivalent in GORM?

匆匆过客 提交于 2019-12-09 10:23:30
问题 Is this possible to convert in createCriteria()? SELECT * FROM node WHERE (node.type = 'act' AND nid NOT IN (SELECT nid FROM snbr_act_community)) LIMIT 10 I know there's a 'in' operator and here's what I have so far: def c = VolunteerOpportunity.createCriteria() def matchingActs = c.list { node { eq('type', 'act') } maxResults(10) } Just want to see if this is possible. Otherwise, I guess this is possible in HQL right? 回答1: thanks Sammyrulez for the code. got an idea from that. tested it but

Grails/GORM “in” criteria

怎甘沉沦 提交于 2019-12-09 07:56:58
问题 Is it possible to do an "in" criteria using the GORM criteria. I'm looking for the equivalent of the following SQL select * from Person where age in (20,21,22); If it was possible I guess the syntax would be something like: def results = Person.withCriteria { in "age", [20, 21, 22] } 回答1: The Grails createCriteria documentation includes an example of using the in clause: 'in'("holderAge",[18..65]) or not{'in'("holderAge",[18..65])} The documentation includes this note: Note: 'in' is a groovy

GORM prevent creation of a foreign key constraint for a Domain

早过忘川 提交于 2019-12-09 03:21:40
问题 I am developing a web based application in Grails. I have come across a situation where I would like to try and suppress GORM from creating a foreign key constraint on a field in a table. I have a domain class which is part of a class hierarchy. The domain class essentially acts as a link to a target domain. The target domain can be of different types and each of the subclasses of this link domain is designed to provide the link for each specific type of linked item. These linked items have

Grails projection on arithmetic expression with executeQuery()?

馋奶兔 提交于 2019-12-09 01:33:29
问题 I need to get a sum of all items sold per order per store. I am running a sum() on expression using executeQuery(). It works fine as shown below but I wanted to know if there is a better, groovier way to do it. StoreService { static transactional = false def getTotalOrders(def store) { return Store.executeQuery("select sum(a.soldQuantity * a.soldPrice) as total from OrderItem a inner join a.order b inner join b.store c where c= :store", [store: store]).get(0) } } Store { transient