Grails Domain Classes Primary Key - grails

class Ctr_country {
String ctr_name
String ctr_abrev
String toString(){
ctr_abrev
}
}
I am using SQLS Express as database. I want the primary key of this class (table) to be ctr_abrev.
a) How can i do so ?

static mapping = {
id generator:'assigned', name:'ctr_abrev'
}
Grails Docs GORM id

Related

Grails Domain Object (GORM) inheritance

I have 4 Grails domain classes ( Domain1, Domain2, Domain3, Domain4). These domain objects map to legacy tables with exactly same set of columns. To be more precise, all of these tables have personFirstName, personLastName and personPhoneNumber columns.
The only thing different between these 4 domain classes is the table name they refer to.
Instead of copy/pasting same piece of code in four different places and just modifying this:
static mapping = {
table name:"legacy_table_name_A" <====== (only difference)
firstName column: "personFirstName"
lastName column: "personLastName"
phoneNumber column:"personPhoneNumber"
}
I was wondering if there is a way to do this all in one abstract class called MyAbstractDomainClass and have my Domain1, Domain2... extend it like this:
class MyAbstractDomainClass {
String firstName
String lastName
String phoneNumber
static mapping = {
firstName column: "personFirstName"
lastName column: "personLastName"
phoneNumber column:"personPhoneNumber"
}
}
class Domain1 extends MyAbstractDomainClass {
static mapping = {
}
}
How do I implement the static mapping for each Domain classes to have a different table name?
I am using Grails 2.4.2

Creating domain classes with foreign key constraints in groovy

Hi I am trying to create a domain class with foreign key constraint to another existing domain class
New domain class is ArEntitlement defined as below
package ars
import gra.Resources
class ArEntitlement {
long id
String entitlementname
String entitlementdesc
String entitlementcomm
Integer departmentid
Resources resource
Integer version
static belongsto =[resource : Resources]
static mapping={
table 'ar_entitlement'
id(generator:'increment')
column{
id(column:'id')
}
}
}
Resource domain class is defined as follows (it was created before)
package gra
import com.gra.transaction.UserTransaction
class Resources {
Long id=1
String resourceName
Double resourceType
String resourceOwner
Double riskScore
Integer decommissioned
String resourceClass
Long resourceSegment
String targetIp
String resCriticality
Long resourceGroup
Integer disabled
static hasMany=[userTransactions:UserTransaction]
static mapping = {
table 'resource'
version false
id(generator:'increment')
column{
id(column:'id') }
}
static constraints=
{
resourceName(nullable:true,blank:false)
resourceType(nullable:true,blank:false)
resourceOwner(nullable:true,blank:false)
riskScore(nullable:false,blank:false)
decommissioned(nullable:false,blank:false)
resourceClass(nullable:false,blank:false)
resourceSegment(nullable:false,blank:false)
targetIp(nullable:false,blank:false)
resCriticality(nullable:true,blank:false)
resourceGroup(nullable:false,blank:false)
disabled(nullable:false,blank:false)
}
}
The resulting tables created dont have a foreign key mapping of ar_entitlement table to resource table, it does create a column called 'resource_id' but without foreign key constraint.
I need it to be pointing to id column of resource table
I tried several options, not having the belongsto specifier, using hasOne (in both domain classes) but not getting required foreign key relationship.
Any Idea what the issue is here?
log error i get is
2011-12-21 19:50:17,258 [main] ERROR hbm2ddl.SchemaUpdate - Unsuccessful: alter table ar_entitlement add index FKDCD6161FEFD54E5E (resourceid_id), add constraint FKDCD6161FEFD54E5E foreign key (resourceid_id) references resource (id)
Remove 'Resources resource' row from ArEntitlement.
static belongsTo = [resource : Resources]
Should cover that and having it defined twice might be the issue. Also you shouldn't need to specify the table names, the id generator or id column as it appears you are using the GORM defaults. Try removing these as well.
Here is the modified code...
package ars
import gra.Resources
class ArEntitlement {
String entitlementname
String entitlementdesc
String entitlementcomm
Integer departmentid
Integer version
static belongsTo = [resource : Resources]
static mapping = {
}
}

Grails: master domain field by detail domain field [hasMany]

There are domains:
class Order {
static hasMany = [execDepartments: Department]
String orderNo
//boolean signature
...
}
class Department {
String name
...
}
I want to add boolean signature field for Order but by Department, so I need field signature by execDepartments for Order. Is it possible? Please help.
Is this an m:m relationship?
why not create another domain like
class Order {
static hasMany = [execDepartments: DepartmentOrder]
String orderNo
//boolean signature
...
}
class DepartmentOrder{
boolean signature
Order order
Department department
}
class Department {
String name
static hasMany = [execOrders: DepartmentOrder]
}
Then you can write the belongTo
What you need is a list of signatures in the Order domain.
Possibly an OrderSignature domain that maps signatures to orders.

Retrieving a list of Grails composite id properties?

How can I retrieve a list of Grails composite id properties?
I have tried the following but the id property is returning a Long:
Domain
class Domain implements Serializable {
String field1
String field2
static mapping = {
id composite: ['field1', 'field2']
}
}
Test
def d = DefaultGrailsDomainClass(Domain.class)
assert(d.identifier.type == java.lang.Long)
After deep diving GORM I found the solution:
GrailsDomainBinder.getMapping(Domain).identity.propertyNames

How do I create association by joining non primary key column

class Contact {
String name
String number
}
class Message {
String text
String number
Contact contactInfo //If any
}
I need to join on Message.number = Contact.number. Any thoughts on creating association in Grails/GORM with non primary key column?
I'm pretty sure this isn't possible in GORM, and I don't know if it's even possible in regular Hibernate. But you can fake it:
class Message {
String text
String number
static transients = ['contactInfo']
Contact getContactInfo() {
Contact.findByNumber(number)
}
void setContactInfo(Contact contact) {
number = contact.number
}
}
Burt, this is possible with hibernate using the property-ref attribute

Resources