gorm

How to fix StaleObjectStateException with JPA and Hibernate

こ雲淡風輕ζ 提交于 2019-12-03 06:01:49
Controller Logic: def updateObject() { Object o = Object.get(params.id as Long) o.otherObjects.clear() objectDataService.saveObject(o.id) OtherObject newObject = new OtherObject; o.addToOtherObjects(newObject) objectDataService.saveObject(o.id) } ServiceLogic def saveObject(long profileId) { o.save(flush:true) } what happens in 90% of the cases this will just work. problems ERROR errors.GrailsExceptionResolver - StaleObjectStateException occurred when processing request: [GET] /controller/updateObject - parameters: stuff[]: data Row was updated or deleted by another transaction (or unsaved

criteria uses “inner join” instead “left join” approach by default making my query work not the way I planned

若如初见. 提交于 2019-12-03 05:46:03
问题 The question is: how do I make GORM generate left joins instead of inner joins in this particular example? Testbed: Given classes A, B and C: class A{ B someObject } class B{ C importantObject } class C{ boolean interestingFlag } I want to list all the elements of A class that: their B.C object is null OR their B.C object interestingFlag value is false What I tried so far: This approach produces correct list of A where B.C is null (conditional 2 commented out) OR correct list of A where B.C

Grails multi column indexes

半城伤御伤魂 提交于 2019-12-03 05:22:36
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 highly appreciated! The solution that has worked for me for multi-column indexes is: class ClassName {

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

人盡茶涼 提交于 2019-12-03 05:15:12
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? jamesallman 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 JamesA's answer will work, but only if table name if defined explicitly, like in the question. If you wish

Overriding dateCreated for testing in Grails

霸气de小男生 提交于 2019-12-03 04:55:15
Is there any way I can override the value of dateCreated field in my domain class without turning off auto timestamping? I need to test controller and I have to provide specific domain objects with specific creation date but GORM seems to override values I provide. Edit My classes look like this: class Message { String content String title User author Date dateCreated Date lastUpdated static hasMany = [comments : Comment] static constraints = { content blank: false author nullable: false title nullable: false, blank: false } static mapping = { tablePerHierarchy false tablePerSubclass true

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

旧街凉风 提交于 2019-12-03 04:06:58
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 assume that it is not an option to change the givent SQL statement. Gorm requires an id field to work. You

Grails update instead of delete

随声附和 提交于 2019-12-03 03:38:27
Is there an easy way in Grails to not allow deleting for any Domain Class? And rather have a delete flag in each domain which gets updated whenever something is deleted. Also, in effect all the list/show methods should not show objects where delete flag is true. I know I can do that by manually editing all my CRUD methods in all the controllers but that seems a little bit too much work when working with Grails where everything can be done by changing some flag somewhere!! My usual list method looks like following, almost all the list methods in my project lets user access things which only

withCriteria two level deep association eager fetch grails

不羁岁月 提交于 2019-12-03 03:11:20
I'd like to eager load a structure, two levels deep in an association chain. Something along the lines of: class TopLevel { String name LevelOne levelOne } class LevelOne { String name LevelTwo levelTwo } class LevelTwo { String name } I'd like to load the entire structure. Searching around I found this example, but it didn't work. The "println" generated a query to the LevelTwo table. def result = TopLevel.withCriteria { eq('name', 'test') fetchMode "levelOne", FetchMode.JOIN levelOne { fetchMode "levelTwo", FetchMode.JOIN } } println result.levelOne.levelTwo.name Appreciate any help! - Steve

Testing the Oracle to_date function

一曲冷凌霜 提交于 2019-12-03 02:59:18
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 works in a test and in live? As I only really care about the Month and Year one thought I had would be to

grails hasOne vs direct member variable

耗尽温柔 提交于 2019-12-03 02:42:41
Let's say I have a grails domain class that looks like class Person { Address address } I could also declare it as class Person { static hasOne = [address:Address] } The second way would move the foreign key to the Address table rather than the person table. What are the practical benefits (or disadvantages) of doing this one way vs the other? As far as I understand, they will both use foreign keys, it's just a matter of where the foreign key lives. If the foreign key exists on the address table, then that address can only have one person. If the foreign key is on the person table, multiple