gorm

Hibernate Criteria Question

I'm working on a Grails project using Hibernate (GORM). I have the following Domain Models ClientContact{ static hasMany = [owners: Person] static belongsTo = [Person] } Person{ static hasMany = [clientContacts:ClientContact] } When I try to retrieve all the ClientContacts with a specific owner (Person), I'm running into...

Gorm main window does not accept drag-and-drop.

When I try to add any widget (label, button ...) to main application Window in Gorm, the Window does not accept the widget. I try to drag and drop but nothing wants to 'stick'. When I select and left click on button widget in palette window the curson changes to '2 green squares'. When I start dragging it changes to '2 black squares'. A...

How can fields in Grails represented by a combobox be made optional?

I'm doing my first experiments with Grails and am looking for a way to have fields represented by a combobox (such as one-to-one domain associations and numbers with a narrow range constraint) to be optional, i.e. there should be an empty entry in the combobox. How can this be achieved? I've tried both adding a nullable:true constraint ...

Controllers in Grails

I'm writing a small webapp in Grails and I have the following question regarding best practices for controller design and using GORM: I'm storing the user object in session.user. Currently all my action methods start with the following code to make sure a valid user is logged in and that the user object is fresh: class FooController { ...

GORM in Grails and StaleObjectStateException

I'm writing a small Grails app, and I keep on getting StaleObjectStateException:s for about 1/10:th of the calls to "createfoo" when running the following rather simple code. Most probably I'm missing out on the best way to use GORM. This is the code: def viewfoo = { session.user.refresh() // ... } def createfoo = { session.user...

Specify order of fields in DDL generated from GORM classes?

I use GORM to generate my database's DDL from groovy classes. Which is great. However, the order of fields in the generated SQL is not the same as the order of fields in the class. For example, if I create the class class Person { String firstName String lastName String address String email } the following SQL is generated ...

Setting Grails domain id in Bootstrap.groovy

Is it possible to explicitly set the id of a domain object in Grails' Bootstrap.groovy (or anywhere, for that matter)? I've tried the following: new Foo(id: 1234, name: "My Foo").save() and: def foo = new Foo() foo.id = 1234 foo.name = "My Foo" foo.save() But in both cases, when I print out the results of Foo.list() at runtime, I ...

Limiting no of outputs in Hibernate query

I have a hibernate query in grails Book.findAllByRating(4) In the above query i want only 5 number of outputs.How do I limit the output to 5? ...

Defining default sort-order in Grails/GORM

Let's say I have definied a User object using GORM. Each user can have zero or more Login:s. Each Login has a timestamp. When retrieving user.logins I want the logins to be sorted based on the value of login.date. What is the correct Grails way to achieve this? Example: I want the following code to list all the user's logins in ascendin...

Storing Grails/GORM domain objects in the session - why not?

I'm learning Grails/GORM and as I've understood it the current best practice is not to store domain objects in the session (see http://jira.codehaus.org/browse/GRAILS-978 for a potential fix). The workaround is simple; simply store the reference id for the domain object in the session, and then re-retrieve the object using on the next r...

o.errors.allErrors.each { println it } by default when failing to save a domain object

When persisting domain objects using Grails/GORM I frequently find myself wondering why a save() call fails. This can easily be solved by adding the logic: if (!o.save()) { o.errors.allErrors.each { println it } } However, adding this everywhere I do a .save() adds a lot of duplicate code. In the spirit of DRY I'd like to configu...

Concurrency when using GORM in Grails

Let's say I have a counter function which updates a counter using raw SQL: public void updateCounter() { executeSql("UPDATE counter SET count_value = count_value + 1 WHERE id = 1;"); } The database would make sure that two concurrent calls to the counter are handled as expected - that all calls would update the counter with one i...

Legacy mapping in Grails/GORM: One domain class and two tables in a 1:N-relationship

Let's say I have two tables employee and salary with a 1:N relationship (one salary can be associated with many employees). In plain SQL the tables would be joined with: SELECT e.id, e.name, s.salary FROM employee e, salary s WHERE s.id = e.salary_id AND e.id = 12345; Assuming the following GORM-powered domain class how do I map the ...

Grails/GORM default fetch strategy: When to set fetchMode to "eager"? (eager vs. lazy)

What are some general guidelines on when to set fetchMode to "eager" in a domain class? Pros and cons of fetchMode "eager" vs. the default "lazy"? Please include some specific examples/use-cases showing when to use "eager" (fetchMode=eager), and when not to (fetchMode=lazy). ...

Grails/GORM: The meaning of belongsTo in 1:N relationships

In an ordinary one-to-many mapping the "one"-side is the owner of the association. Why would anyone use the belongsTo-mapping for such a mapping? Am I missing some side-effect of specifying belongsTo? In other words: what are the effects of specifying a belongsTo-mapping in GORM vs. not specifying it? ...

"public class Foo" v.s. "class Foo" in Groovy domain classes

The following Groovy code creates a GORM-persisted domain class called Foo when written to grails-app/domain/Foo.groovy: class Foo { String someField } However, if I instead write "public class Foo" the class does NOT get GORM-persisted (i.e. no save() method injected, no database table created, etc.): public class Foo { String s...

Composite foreign key columns in GORM

Hi, I need to customize the column names for composite foreign keys in GORM, and I didn't find any document that shows how to do it. I know how to customize PK columns, and how to customize a single-column FK, but not multi-column FK. Is it possible at all? Thanks. ...

Modeling many-to-many relationship in Grail on top of a legacy database

Hi I have a simple ticket logging application build on LAMP. I am currently playing around with grails. I want to build a demo app that uses the existing MySql database without changing the database too much. There is a many-to-many relationship in the database: 'client' table is mapped to the 'user' table through the 'cliet_contact' ...

Grails query association problem

Hi, I'm having trouble writing a query for the following domain classes: class Person { static hasMany = [memberships: Membership] } class Membership { static belongsTo = [person: Person, group: Group] Date joinDate = new Date(); Group group; Person person; } class Group { static hasMany = [memberships: Member...

Grails GORM MissingMethodException with 1:N relationship

Hi guys, I have such domain classes: class ServicesGroup { Long id String name String description String toString(){ return name } static mapping = { version false table 'root.services_groups' id column:'group_id' name column:'group_name' description column:'gr...