Note that there are some explanatory texts on larger screens.

plurals
  1. POGrails Domain Create Association Table
    primarykey
    data
    text
    <p>I have a question about creating an associative table, in grails, to reconcile a many-to-many relationship. The setup is this: 1. Domain A (Client Profile) can have many Domain B (Friends) 2. Each Domain B (Friends) can have many Domain A (Client Profile) 3. To Resolve this, I need to create an Associative table (or domain) that has FK's from each table. This domain can be named Domain C (client_friend)</p> <p>Here is the code I have so far:</p> <pre><code>class DomainA{ String id String firstName String lastName static hasMany = [domainB: DomainB] static mapping = { cache true id generator: 'assigned' columns { firstName type:'text' lastName type:'text' alumConnections column: 'domaina_id', joinTable: 'a_b' } } static constraints = { firstName (nullable:true) lastName (nullable:true) } } </code></pre> <p>DomainB Code:</p> <pre><code> class DomainB{ String id String firstName String lastName static hasMany = [domainA:DomainA] static belongsTo = DomainA static mapping = { cache true id generator: 'assigned' columns { firstName type:'text' lastName type:'text' domainA column: 'domainb_id', joinTable: 'a_b' } } static constraints = { firstName (nullable:true) lastName (nullable:true) } } </code></pre> <p>Domain A_B code:</p> <pre><code> class AB{ Date dateCreated Date lastUpdated long version } </code></pre> <p>When I run this code, it seems to work. The tables, using MySQL, are created, FK seem to be in place. When I enter data into the DomainB class, data is entered and both PK's from DomainA and DomainB are inserted into A_B. But, the problems comes when I try to delete values from A_B. I've tried something like this:</p> <pre><code> AB results =AB.findByAIdAndBId('jIi-hRi4cI','2BYvuA2X14') </code></pre> <p>but get an error: InvalidPropertyException: No property found for name [a_id] for class [class mgr.AB]</p> <p>My question is this: first, have i set this up correctly? Second, if so, how then do i query the AB table who's PK is made up of a composite of DomainA and DomainB?</p> <p>Thanks for any help.</p> <p>jason</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload