gorm

Grails: Raw SQL query in the current transaction

左心房为你撑大大i 提交于 2019-12-03 17:22:38
I am trying to execute a raw sql query using something similar to def dataSource; Sql sql = new Sql(dataSource); But, it seems that this runs in a separate transaction of its own. It therefore misses all the (uncommitted) changes done before it in the service method. What is the best way to run a raw sql query in the current transaction? The above code would create a new connection, hence a new transaction. You can use the current Hibernate session(injecting sessionFactory) to execute a raw sql in the current transaction as below. def session = sessionFactory.getCurrentSession() def results =

How to fix StaleObjectStateException with JPA and Hibernate

给你一囗甜甜゛ 提交于 2019-12-03 16:58:45
问题 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

How do I search for elements whose collection contains another element in Grails?

时光怂恿深爱的人放手 提交于 2019-12-03 16:38:55
问题 Let's say I have a domain class called "User" which can follow other "User" objects. It does so having a field specified as: def hasMany=[followedUsers:User] I need to do the reverse (find all User objects that follow a specific User object) without setting up the reverse relationship, since it is not a use case performed often. I tried to do something like this, using closures: User.findAll { it.followedUsers.contains(userInstance) } but this always returns all users in the database,

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

做~自己de王妃 提交于 2019-12-03 16:34:24
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 only use one belongsTo/association at a time, is this pretty "standard" usage, or do I need to switch this

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

假如想象 提交于 2019-12-03 16:15:21
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.NullPointerException: Cannot invoke method add() on null object So basically it seems that I have to

Overriding dateCreated for testing in Grails

我是研究僧i 提交于 2019-12-03 15:14:02
问题 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

How to ensure data integrity when using Table Per Subclass?

☆樱花仙子☆ 提交于 2019-12-03 14:45:14
I am using the table per subclass strategy in Grails by setting the tablePerHierarchy property of the static mapping field in my superclass to false. This way, Grails creates one table for my superclass and one additional table for each of my subclasses. However, while the superclass and subclass records share the same ID (primary key), there are no foreign key constraints to keep them consistent, i.e. it is possible to delete the superclass record, leaving the subclass record in an invalid state. I want to know if there is a setting/property to make GORM address this in some way, e.g. through

Is there a 'not in' equivalent in GORM?

孤街浪徒 提交于 2019-12-03 14:37:52
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? thanks Sammyrulez for the code. got an idea from that. tested it but it didn't work. i fixed it and here's the final working code: def ids = [14400 as long, 14401 as long] def c

Grails - mapping a many-to-many parents/children relation to a single join table

女生的网名这么多〃 提交于 2019-12-03 13:58:41
问题 My question is based on the following (simplified) Grails domain class class Dimension { String name static hasMany = [ children: Dimension, parents: Dimension ] } Is there a way to map the many-to-many parents/children relationship to a single join table? 回答1: As far as I know, the only way to do that is to create another domain class that represents a parent-child relationship. class DimensionDependency { Dimension parent Dimension child static belongsTo = Dimension } class Dimension {

Grails update instead of delete

感情迁移 提交于 2019-12-03 13:40:49
问题 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