gorm

Grails group by date

倖福魔咒の 提交于 2019-12-21 16:18:21
问题 I have a domain class with a date-property. class Transaction { LocalDate time BigDecimal amount } How can I query for the sum of all transactions grouped by month? I can´t find any support for group by a date-range in GORM. 回答1: Add a formula based field to your domain class for the truncated date: class Transaction { LocalTime time BigDecimal amount String timeMonth static mapping = { timeMonth formula: "FORMATDATETIME(time, 'yyyy-MM')" // h2 sql //timeMonth formula: "DATE_FORMAT(time, '%Y-

Grails: Can I use domain objects when I don't want to save anything?

旧城冷巷雨未停 提交于 2019-12-21 08:49:12
问题 Some of my domain classes are getting rather rich: They implement an interesting comparable, might have plus, minus, multiply and div, many have some convenient getters which call services and determine complicated things. And most of all, they have the right properties. I am using these both for the normal "database transactions" and also in times when I just want an object that has all those methods but may not want to save it. My teammates are convinced that this is very bad, and advise

grails - using multiple belongsTo, but only one at a time

六眼飞鱼酱① 提交于 2019-12-21 05:22:29
问题 If I want to use a domain class, e.g. MoneyTransaction, for two entirely different purposes, i.e.: 1) when a customer places an order 2) when a member gets paid such that I have something like: class Order { static hasMany = [transactions: MoneyTransaction] } class Member { static hasMany = [payments: MoneyTransaction] } and class MoneyTransaction { static belongsTo = [order: Order, member: Member] static constraints = { order(nullable: true) member(nullable: true) } } and then in essence

GORM domain class properties default values

丶灬走出姿态 提交于 2019-12-21 03:31:44
问题 Maybe a silly question but where/how should I define default values for GORM domain class properties? For example when I'm creating a new Company object instance I want default value for property country to be "USA". I guess I could do it in create controller but it looks kinda dirty. Something like: def create = { def companyInstance = new Company() companyInstance.properties = params companyInstance.accepted = "USA" ... 回答1: Put it in the domain class itself class Company { String country =

Grails multi column indexes

萝らか妹 提交于 2019-12-20 18:01:52
问题 Can someone explain how to define multi column indexes in Grails? The documentation is at best sparse. This for example does not seem to work at all: http://grails.org/GORM+Index+definitions I've had some luck with this, but the results seems random at best. Definitions that works in one domain class does not when applied to another (with different names of course). http://www.grails.org/doc/1.1/guide/single.html#5.5.2.6%20Database%20Indices Some working examples and explanations would be

Grails multi column indexes

不问归期 提交于 2019-12-20 18:01:23
问题 Can someone explain how to define multi column indexes in Grails? The documentation is at best sparse. This for example does not seem to work at all: http://grails.org/GORM+Index+definitions I've had some luck with this, but the results seems random at best. Definitions that works in one domain class does not when applied to another (with different names of course). http://www.grails.org/doc/1.1/guide/single.html#5.5.2.6%20Database%20Indices Some working examples and explanations would be

How to get the name of the table GORM object is mapped to?

拟墨画扇 提交于 2019-12-20 17:38:34
问题 Say I have something like: class Foo { static mapping = { table 'foo_table' } } How can I get the name of foo_table if I have a reference to an instance of this object? 回答1: Import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder . To get the table name from the domain class: def tableName = GrailsDomainBinder.getMapping(Foo).table.name And to get the table name from an instance of the domain class: def tableName = GrailsDomainBinder.getMapping(foo.class).table.name 回答2: JamesA

Is it possible for a Grails Domain to have no 'id'?

人盡茶涼 提交于 2019-12-20 15:16:28
问题 Is it possible to create a table that has no 'id'? For example, this is my domain: class SnbrActVector { int nid String term double weight static mapping = { version false id generator: 'identity' } static constraints = { } } When I run this SQL statement, it fails: insert into snbr_act_vector values (5, 'term', 0.5) I checked the table and 'id' is already set to autoincrement. I'm thinking that another option is to remove the 'id' itself. Or is there another workaround for this? Please

Testing the Oracle to_date function

狂风中的少年 提交于 2019-12-20 14:19:33
问题 I'm writing an integration test in Grails using GORM. I want to do something like the following: delete from Statistic where stat_date = TO_DATE(:month_year, 'MON-YYYY') But I get the following error: java.sql.SQLException: Unexpected token: TO_DATE in statement [delete from statistics where stat_date=TO_DATE(?, 'MON-YYYY')] I think the error is caused by the in memory database used by GORM (is it H2?) not supporting the to_date function. Any ideas on how to write the delete SQL so that it

Grails: What is the difference between an unflushed session and a rolled back transaction?

笑着哭i 提交于 2019-12-20 12:31:19
问题 I am SO confused by sessions and transactions. I basically don't see what the point of having both is, and I am very confused when to use one or the other. What is the difference between an unflushed session and an uncommitted transaction? I don't even know how to ask what I don't know... is there a resource that gives good examples of common session and transaction situations so I can see the difference? 回答1: A transaction in Hibernate is pretty much the same as a transaction in JDBC in