Quantcast

many-to-many with existing database

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

many-to-many with existing database

meena
I have many-to-many between two existing tables(Error and Attribute) with the joint table(Error_Attribute_Map) in database. I am trying to map them using domain classes. Problem I am facing is when I ask for Attribute from Error GORM query does not include Joint Table hence I dont get back any Attribute Objects for a given Error Object. Please let me know If I am missing any mapping.

Error domain class


  class Error {
       Long id;
       String comment;
       static hasMany = [attributes:Attribute]

       static constraints = {
              id()  
              comment()
       }
       
       static mapping = {
              table('error')
              id(generator:'identity',name:'id',column:'error_id')
              attributes(joinTable:[name:'ERROR_ATTRIBUTE_MAP',key:'error_id', column:'attribute_id'])              
       }
}


Attribute domain class


class Attribute {

       Long id;

       String attribute;

       static hasMany = [errors:Error]


       static constraints = {

       }
       
       static mapping = {
              table('ATTRIBUTE')
              id(generator:'identity',name:'id',column:'attribute_id')
             
              errors(joinTable:[name:'ERROR_ATTRIBUTE_MAP',key:'attribute_id', column:'error_id'])
             
       }
}


Error_Attribute_Map table in Database


34 CREATE TABLE `ERROR_ATTRIBUTE_MAP` (
  `error_id` int(10) unsigned NOT NULL default '0',
  `attribute_id` int(10) unsigned NOT NULL default '0',
  KEY `error_id` (`error_id`),
  KEY `attribute_id` (`attribute_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8


when I ask
 

error.attributes


query made by Grails is


select
        attributes0_.error_id as error6_3_1_,
        attributes0_.attribute_id as attribute1_1_,
        attributes0_.attribute_id as attribute1_2_0_,
        attributes0_.version as version2_0_,
        attributes0_.attribute as attribute2_0_,
    from
        ATTRIBUTE attributes0_
    where
        attributes0_.error_id=?

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

Re: many-to-many with existing database

meena
Any suggestions /  ideas?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: many-to-many with existing database

burtbeckwith
In reply to this post by meena
A Grails many-to-many needs a hasMany on each side but one of them also needs a belongsTo to define the owning side. Ordinarily if you omit the belongsTo Grails complains at startup but it doesn't in this case; I'm assuming it's because as of 2.0 there is a real "errors" property (the validation Errors instance) in each domain class, so it's not considering this a proper many-to-many. If you change "errors" to "errs" you will see the startup message.

When trying to work with legacy database it's convenient to run 'grails schema-export' and look at the generated target/ddl.sql file. Keep tweaking the mappings and comparing the generated DDL to the real database until they're equivalent.

Btw - you should remove the semicolons from your code, and also the "Long id" fields since Grails adds those for you.

Burt

meena wrote
I have many-to-many between two existing tables(Error and Attribute) with the joint table(Error_Attribute_Map) in database. I am trying to map them using domain classes. Problem I am facing is when I ask for Attribute from Error GORM query does not include Joint Table hence I dont get back any Attribute Objects for a given Error Object. Please let me know If I am missing any mapping.

Error domain class


  class Error {
       Long id;
       String comment;
       static hasMany = [attributes:Attribute]

       static constraints = {
              id()  
              comment()
       }
       
       static mapping = {
              table('error')
              id(generator:'identity',name:'id',column:'error_id')
              attributes(joinTable:[name:'ERROR_ATTRIBUTE_MAP',key:'error_id', column:'attribute_id'])              
       }
}


Attribute domain class


class Attribute {

       Long id;

       String attribute;

       static hasMany = [errors:Error]


       static constraints = {

       }
       
       static mapping = {
              table('ATTRIBUTE')
              id(generator:'identity',name:'id',column:'attribute_id')
             
              errors(joinTable:[name:'ERROR_ATTRIBUTE_MAP',key:'attribute_id', column:'error_id'])
             
       }
}


Error_Attribute_Map table in Database


34 CREATE TABLE `ERROR_ATTRIBUTE_MAP` (
  `error_id` int(10) unsigned NOT NULL default '0',
  `attribute_id` int(10) unsigned NOT NULL default '0',
  KEY `error_id` (`error_id`),
  KEY `attribute_id` (`attribute_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8


when I ask
 

error.attributes


query made by Grails is


select
        attributes0_.error_id as error6_3_1_,
        attributes0_.attribute_id as attribute1_1_,
        attributes0_.attribute_id as attribute1_2_0_,
        attributes0_.version as version2_0_,
        attributes0_.attribute as attribute2_0_,
    from
        ATTRIBUTE attributes0_
    where
        attributes0_.error_id=?

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

Re: many-to-many with existing database

meena
Hi burtbeckwith

Thankyou very much for your input. Yes u were very right replacing errors by other name did the trick for me.

Thanks again, I was struggling for five days with this issue.

Meena
Loading...