Search code examples
hibernategrailsgrails-orm

grails hasOne but does not belong to


I'm working on a project and have an Invoice domain class that currently hasOne=[billingAddress:Address] When I try to launch my server, I get the following error:

hasOne property [Invoice.billingAddress] is not bidirectional. Specify the other side of the relationship!

I don't want to assign the other side of the relationship though... Invoices have a billing address but Addresses do not belong to Invoices. Addresses belong to Users!!!

What is the proper way to handle this situation?


Solution

  • It sounds like you just need a normal association rather than a hasOne:

    class Invoice {
      // other properties
    
      Address billingAddress
    }
    

    The hasOne mechanism is a way to change the database representation for an association, with a conventional Address billingAddress you'll end up with a billing_address_id column in the invoice table, whereas with hasOne the association is instead represented by a foreign key in the address table - this representation only allows one Invoice for each Address, which is why the association has to be bidirectional.