gorm

Finding the first match - alternative to DomainClass.findAll()[0]

Is there a shorter/cleaner way than DomainClass.findAll()[0] to retrieve the first domain object in the set of domain objects that would normally be retrieved by findAll()? Ideally, I'd like DomainClass.find() but such a finder does not exist. ...

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

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: thi...

Select all events from a month in grails

I am new to groovy/grails, and I'm trying to to do a criteria search that finds all posts for a month, basically like this: def getUserMinutesForYear(User user, Date date){ Date firstDate = new GregorianCalendar(date.year, Calendar.JANUARY, 1, 0, 0, 0).time Date lastDate = new GregorianCalendar(date.year, Calendar.DECEMBER, 31,...

Grails multi column indexes

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 (w...

Hibernate transaction boundaries

Hi, I'm using Hibernate (in a Grails app) and the transactional boundaries are service methods, i.e. every time a service method is called a transaction starts, and every time a service call completes the transaction is either rolled back or committed. If one of the database operations causes a database trigger to fire, and this trigge...

In Grails, how do I declare a SQL Server Schema name for a Domain Class?

I have recently started reading up on Grails and would like to use SQL Server security schemas to group tables generated by GORM. However, I cannot seem to find a reference explaining how to perform this task. I am new to Hibernate as well and would like to know if this is possible. Thank you. ...

How To Make Transactions Work In Grails

Summary A parent can have many children. How do you write a service such that, if after adding a parent there is an error when adding a child, the entire transaction is rolled back. For example, add parent p1, successfully add child c1, then when adding child c2 an error occurs, both p1 and c1 should be rolled back. Detailed Problem In...

Using Grails GORM standalone

I'm currently wondering how it is possible to use the Groovy ORM Layer from Grails standalone outside of the Grails Framework. There is a Documentation Entry for doing so, but the ZIP file only links to an empty page. I downloaded Grails 1.2-M3 but I couldn't find anything in the docs either. Does anybody know what the current state is...

Bulk insert of composite Domain Objects

Have domain objects: Profile and ProfileProperty. ProfileProperty static belongsTo=Profile and profile static hasMany=[profileProperties:ProfileProperty]. Each Profile has dozen profileProperties. Need to bulk insert profiles with properties. Idea was to have Profile#extraProps of type of java.util.Map, marked as static transient =['ext...

Found shared references to a collection org.hibernate.HibernateException

Hi, I got this error message : error: Found shared references to a collection: Person.relatedPersons when I tried to save addToRelatedPersons(anotherPerson) : person.addToRelatedPersons(anotherPerson); anotherPerson.addToRelatedPersons(person); anotherPerson.save(); person.save(); my domain : Person { static hasMany = [relatedPe...

Hibernate/GORM: collection was not processed by flush()

Hi, I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate.AssertionFailure: collection [com.mycompany.facet.Facet.channels] was not processed by flush() at com.mycompany.member.MemberConn...

Groovy on Grails: Abstract Classes in GORM Relationships

Grails GORM does not persist abstract domain classes to the database, causing a break in polymorphic relationships. For example: abstract class User { String email String password static constraints = { email(blank:false, nullable:false,email:true) password(blank:false, password:true) } static hasMany = [membership:GroupMembers...

Sorting Objects Based on Custom Domain Class Methods

I have a domain class, in which I've defined some methods which give the object a score based on different algorithms (eg. popularity). I now want to retrieve a list of these objects sorted by one of these scores (eg. descending by popularity score). Is there a way to do with with GORM? Example class: class SomeObject { String ti...

Groovy on Grails: GORM and BitSets?

I don't see anything in the official documentation about unsupported persistence data types, so I'm working under the assumption that types available in the Groovy language should be handled. However, for the following domain class: class DocGroupPermissions { Workgroup workgroup; Document document; BitSet permissions = new BitSet(2) p...

Disabling locking for non-critical Grails/GORM domain classes

Assume the following code in a Grails controller: def action = { ClassName o = ClassName.findByFoo(params.foo) if (o) { o.counter += 1 } } By default Grails uses optimistic locking via the version column added by default to all GORM database tables. However, if a sufficiently large number of multiple concurrent requests are ...

Grails domain class relationship to itself

I need a way to be able to have a domain class to have many of itself. In other words, there is a parent and child relationship. The table I'm working on has data and then a column called "parent_id". If any item has the parent_id set, it is a child of that element. Is there any way in Grails to tell hasMany which field to look at ...

Grails GORM problem: Object references an unsaved transient instance

The Grails code below throws the following exception when trying to .save() the Foo object: org.hibernate.TransientObjectException/ org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: Bar I guess I'm missing out on some of the GORM...

Grails GORM Domain class relationship

Grails 1.1.1 Goovy 1.5.7 In a relationship such this: Author 1 -- n Book n -- 1 Publisher Defined in Grails: class Author { String firstName String lastName static hasMany = [books: Book] static constraints = { books(nullable: true) } } class Book { String title Author author Publisher ...

Grails GORM composition or hasOne?

I'm a bit confused about the differences between using the static hasOne map and composing objects in domain classes. What are the differences between the two? ie. class DegreeProgram { String degreeName Date programOfStudyApproval static hasOne = [committee:GraduateCommittee] } versus class DegreeProgram { String degreeName Date p...

Grails optimistic locking strange behaviour

I've been trying to make GORM throw an optimistic locking error in an integration test. It has been said before that it is not possible to test for concurrent update errors without resorting to multiple threads, but even so I find the behaviour of my test case surprising: void testOptimisticLocking() { new Widget(foo:"bar").save(flus...