Quantcast

creating domain classes with one-to-one relationship issue

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

creating domain classes with one-to-one relationship issue

priyank15
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?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: creating domain classes with one-to-one relationship issue

priyank15
Hi here is the error I get from the log

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)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: creating domain classes with one-to-one relationship issue

Ingo Wiarda
In reply to this post by priyank15
Hi,

you can remove the "Resources resource", otherwise the relation is
defined twice (once by the belongsTo with map parameters, once by the
field definition), which may be the source of the problem.

Also, belongsto should probably be belongsTo.
http://www.grails.org/doc/latest/ref/Domain%20Classes/belongsTo.html

Regards,
Ingo


On 22.12.2011 04:47, priyank15 wrote:
>      Resources resource
>      Integer version
>
>      static belongsto =[resource : Resources]


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: creating domain classes with one-to-one relationship issue

ShatyUT
I believe the real issue is the incorrect case with belongsTo. As mentioned, you already get a property called resource with the following line:

static belongsTo = [resource: Resource]

but there's nothing wrong with also having

Resource resource

It's unnecessary but should not cause any problems.

Shaty
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: creating domain classes with one-to-one relationship issue

priyank15
Hi ,
The issue is partly solved,
The 'id' - primary key of 'resource' table is unsigned, and in my arEntitlement domain class where I have a field like "Resources resource" or even using the 'belongsTo = [resource:Resources]' clause it creates the resource_id field which is not unsigned, and thats why the next step of creating the foreign key constraint is failing ..
I manually changed resource_id field in the ar_entitlement table to be unsigned (after domain class was created) and then ran the foreign key constraint query from the error log directly in mysql then its successful.
I want to know now how to specify unsigned for resource field in the domain class itself ..
Regards
Priyank
Loading...