gorm

GORM list() to return superclass objects only

I have a simple object hierarchy, and I want to query each of the objects using list(). The problem is that because of polymorphism, Task.list() returns both instances of type Task and ComplexTask. class Task { } class ComplexTask extends Task { } I realize I can solve my problem by having a common abstract superclass, or filter r...

Avoiding table changes when mapping legacy database tables in Grails?

I have an applicaton that contains some tables that are auto-generated from Grails domain classes and one legacy table (say table legacy) that have been created outside of Grails but are being mapped by Grails domain classes. Mapping the columns in the legacy database is trivial, but I would like to disable the adding of extra fields and...

Accessing the join table in a hql query for a many-to-many relationship in grails

I have 2 domain classes with a many-to-many relationship in grails: decks and cards. The setup looks like this: class Deck { static hasMany = [cards: Card] } class Card { static hasMany = [decks: Deck] static belongsTo = Deck } After I delete a deck, I want to also delete all cards which no longer belong to a deck. The easiest way t...

removeFrom is deleting all of my many-to-many associations

I'm using Grails 1.2 and have the following many-to-many relationship setup: class Employee { static belongsTo = Store static hasMany = [stores:Store] } class Store { static hasMany = [employees:Employee] } I seed some data in Bootstrap.groovy: store1.addToEmployees(employee1).save() store1.addToEmployees(employee2).save...

setting up associations directly through ids in grails

I have 2 domain objects with a one-to-many relationship. Just as an example: Class Author{ static hasMany = [posts: Post] } Class Post{ Author author } In the database the post table has a field called author_id. In my application, I have a bunch of author ids that I want to associate with posts. I can do this by first querying th...

How do I clear and replace a collection in a one-to-many relationship in Grails/Groovy

This question is in two parts, first part is about clearing a list and second part is about assigning an owner to an object. I have a one-to-many relationship between two domain objects in my model in Grails. The relationship looks like this... class Person { static hasMany = [authorities: Role, locations: Location] } class Loca...

Grails limit database column size

Hi, In my Grails app I have a domain class that has a property SearchPrivacy searchPrivacy = SearchPrivacy.PUBLIC where SearchPrivacy is an enum enum SearchPrivacy { PRIVATE('pr'), PUBLIC('pu'); final String id SearchPrivacy(String id) { this.id = id } static getEnumFromId(String id) { values()...

Grails global constraints

Hi, In version 1.2, Grails introduced global constraints. I tried adding the following to Config.groovy grails.gorm.default = { constraints { notBlank(nullable:false, blank:false) } } Then using it in one of my domain classes static constraints = { email(email: true, unique: true, shared: 'notBlank') } But whe...

Query the other way across a one-to-many with Criteria Builder

Lets say I have the following one-to-many relationship: Site has many Users User belongs to one Site I've setup the relationship like this class Site { static hasMany = [users:User] ... } and class User { static belongsTo = [site:Site] int number String username ... } Basically I want to update the userna...

Can I call GORM "find" methods on an object instead of the class?

I'm trying to take advantage of the fact that groovy is more dynamic than java. I'd like to have a block of code that does TypeA type = //do something to build an object TypeA dbType = TypeA.findBySomethingAndSomething(something, somethingelse) if(dbType != null) type.id = dbType.id type.save() but that can work for multiple object...

Renaming composite foreign keys in GORM

I have the following classes: class Catalog { static mapping = { id composite:['name', 'manufacturer'] columns { name column:'cat_name' manufacturer column:'manuf_id' } } String name Manufacturer manufacturer } class Order { static mapping = { columns { // How to rename foreign keys as cat_n...

GORM Save cascade return collection with no ids

Hi, Using Grails (GORM) I'm populating a collection upon creating an object and I must return the newly populated collection with ids. class A { static hasMany = [bees:B] } class B { static belongsTo = [a:A] } As noticed, a one-to-many relationship when the many side belongs to the one side for full cascading If A is not saved befor...

Overriding setter on domain class in grails 1.1.2

I have following two domain classes in Grails 1.1.2: class A implements Serializable { MyEnumType myField Date fieldChanged void setMyField(MyEnumType val) { if (myField != null && myField != val) { myField = val fieldChanged = new Date() } } } class B extends A { List children void setMyField(MyEnumType val) { if (m...

How do you bulk delete records in Grails/GORM?

I have a table which has records that need to be periodically cleared according to a set of criteria. I was expecting that I could use the criteria builder to just delete the records, but that fails because there is no delete method on criteria... def c = Agency.createCriteria() c.delete { eq("agency", "XXX") } So I thought may...

Many-to-Many link tables in grails (GORM) / hibernate

Hi Guys, I'm playing aroud with Grails and am finding the ORM stuff tedious because I don't fully understand what I'm doing when it comes to domain classes. I'm hoping someone can put me back on track Consdier the the following Test Job One:Many Hardware Used on Job Many:One Physical Hardware ...this is analogous to the cla...

"version" lock value for populating tables

Hi, I need to populate a couple of tables from a db that was created when the grails app was run. I kept the "version" column for hibernate's locking strategy, so I need to know what value is safe to put in that field when importing the data to those tables. ...

Grails Domain Class Dynamic List As A property

Let's say I have a domain class called ShopCategoryPageTab. And I have a domain class called Product. I want ShopCategoryPageTab to have a list of Products. However, this list is not static, but determined by a formula. For example, I might want to have a "products" property which would list all products with criteria X, Y Z. So this...

Need help in transforming this SQL statement to GORM

select b.security_type, b.symbol, b.security_description, b.trade_date_qty as 'axys_qty', c.trade_date_qty as 'fidelity_qty', c.trade_date_qty - b.trade_date_qty as 'qty_diff', b.cost_basis as 'axys_cost', c.cost_basis as 'fidelity_cost', c.cost_basis - b.cost_ba...

Three domain classes relationship in GORM

Hi, is there a special way with gorm to map a three domain classes relationship like this: 1 person belongs to N companies with M given roles (one or more roles for a given company) Thanks in advance. ...

TablePerHierarchy always false for abstract classes?

According to the Grails GORM guide, subclasses of domain classes share the same table as the parent class unless tablePerHierarchy is set to false. I cannot find information on whether the following mapping statement is ignored because of the "abstract" keyword abstract class Item implements Comparable{ static mapping = { tablePe...