Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3785210
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:29:37+00:00 2026-05-19T11:29:37+00:00

I have two domain-classes. One is a Partner the other is a Customer. A

  • 0

I have two domain-classes. One is a “Partner” the other is a “Customer”. A customer can be a part of a Partner and a Partner can have 1 or more Customers:

class Customer {
    Integer id
    String name
    static hasOne = [partner:Partner]
    static mapping = {
        partner joinTable:[name:'PartnerMap',column:'partner_id',key:'customer_id']
    }
}

class Partner {
    Integer id
    static hasMany = [customers:Customer]
    static mapping = {
        customers joinTable:[name:'PartnerMap',column:'customer_id',key:'partner_id']
    }
}

However, whenever I try to see if a customer is a part of a partner, like this:

def customers = Customer.list()
customers.each {
     if (it.partner) {
          println "Partner!"
     }
}

I get the following error:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select this_.customer_id as customer1_162_0_, this_.company as company162_0_, this_.display_name as display3_162_0_, this_.parent_customer_id as parent4_162_0_, this_.partner_id as partner5_162_0_, this_.server_id as server6_162_0_, this_.status as status162_0_, this_.vertical_market as vertical8_162_0_ from Customer this_]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query

It looks as if Grails is thinking partner_id is a part of the Customer query, and it’s not… It is in the PartnerMap table, which is supposed to find the customer_id, then get the Partner from the corresponding partner_id.

Anyone have any clue what I’m doing wrong?

Edit: I forgot to mention I’m doing this with legacy database tables. So I have a Partner, Customer and PartnerMap table. PartnerMap has simply a customer_id and partner_id field.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T11:29:38+00:00Added an answer on May 19, 2026 at 11:29 am

    Given the way 1-many works when you want a join table, I think it’s not possible with standard GORM to make it bidirectional and access a Customer’s Partner. But you can map the join table with a domain class and access things that way:

    Customer:

    class Customer {
       String name
       def getPartner() {
          PartnerMap.findByCustomer(this)?.partner
       }
    }
    

    Partner:

    class Partner {
       String name
       def getCustomers() {
          PartnerMap.findAllByPartner(this)*.customer
       }
    }
    

    PartnerMap:

    import org.apache.commons.lang.builder.HashCodeBuilder
    
    class PartnerMap implements Serializable {
    
       Partner partner
       Customer customer
    
       boolean equals(other) {
          if (!(other instanceof PartnerMap)) {
             return false
          }
    
          other.partner?.id == partner?.id &&
             other.customer?.id == customer?.id
       }
    
       int hashCode() {
          def builder = new HashCodeBuilder()
          if (partner) builder.append(partner.id)
          if (customer) builder.append(customer.id)
          builder.toHashCode()
       }
    
       static PartnerMap get(long partnerId, long customerId) {
          find 'from PartnerMap where partner.id=:partnerId and customer.id=:customerId',
             [partnerId: partnerId, customerId: customerId]
       }
    
       static PartnerMap create(Partner partner, Customer customer, boolean flush = false) {
          new PartnerMap(partner: partner, customer: customer).save(flush: flush, insert: true)
       }
    
       static boolean remove(Partner partner, Customer customer, boolean flush = false) {
          PartnerMap instance = PartnerMap.findByPartnerAndCustomer(partner, customer)
          instance ? instance.delete(flush: flush) : false
       }
    
       static void removeAll(Partner partner) {
          executeUpdate 'DELETE FROM PartnerMap WHERE partner=:partner', [partner: partner]
       }
    
       static void removeAll(Customer customer) {
          executeUpdate 'DELETE FROM PartnerMap WHERE customer=:customer', [customer: customer]
       }
    
       static mapping = {
          id composite: ['customer', 'partner']
          version false
          table 'PartnerMap'
       }
    }
    

    Since you’re not using hasMany, you lose the addToXXX dynamic method, but you can call PartnerMap.create() to relate two instances. You also lose the collection and back-ref in the domain classes, but I added utility methods for those.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two grails domain classes Class MultipleChoiceQuestion { String question static constraints =
I have two domain-classes. class UsersAddThese { String someData } and class TheseAreConstantlyGenerated {
I have two domain classes: class Domain1 { String val11 String val12 Domain2 domain2
I have two domain classes: User : class User { String login String password
I have two domain classes: class Contract { String refNo } class Attachment {
I have the following relationship between two domain classes: class Emp { String name
I have a list like this Dim emailList as new List(Of String) emailList.Add(one@domain.com) emailList.Add(two@domain.com)
I have two domain classes: Contract and Orgainisation . A contract has one contractor
I have two Domain classes (Drives & Computer) class Computer { static hasMany =
I have two domain class : DaySchedule class DaySchedule { Date Todaysdate String startTime;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.