0

I design domains classes as follow:

class Invoice {
   static hasMany = [invoiceOneLines: InvoiceLine, invoiceTwoLines: InvoiceLine]
}

class InvoiceLine {
   static belongsTo = [invoice: Invoice]
}

The invoice line has 2 types: invoiceOneLines and invoiceTwoLines. Then I create new invoice line: I did as follow:

def invoice = new Invoice().save(flush:true)
invoice.invoiceOneLines.add(new InvoiceLine().save(flush:true))
invoice.invoiceTwoLines.add(new InvoiceLine().save(flush:true))

I created 2 invoice lines as invoiceOneLines and 3 invoice lines as invoiceTwoLines. However, when I displayed it in view:

invoice.invoiceOneLines.each{}
invoice.invoiceTwoLines.each{}

It showed invoiceOneLines was corrected but in invoiceTwoLines, it showed all(both invoiceOneLines and invoiceTwoLines).

I'm not sure do I design correct relationship. I think it should a temporary table(like many to many relationship).

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
kucku13
  • 7
  • 2

1 Answers1

0

Look at using mappedBy. An InvoiceLine is associated with an Invoice, but in what context - as an invoiceOneLine or an invoiceTwoLine? As it stands, from the perspective of Invoice, there's no way to know. mappedBy eliminates this ambiguity.

Hope this helps.

Dan Fischer
  • 3,769
  • 1
  • 14
  • 10