0

I have common table (entity_notes) to hold notes for all entities in my system, It had entity_id, entity_type_id columns to identify to which entity the notes belong to. So below are my grails domains.

class EntityType {
    String name
    String code
    Boolean active
}

class EntityNotes  {

    EntityType entityType
    Long entityId
    String notes
    NoteType type

    static mapping = {
        table 'Entity_Notes'
        id generator: 'identity'
        version false
        entityId column:'entity_id'
    }
 }

 class Customer {
      static hasMany = [entityNotes: EntityNotes]
      static mapping = [
         table 'Customer'
         id generator: 'identity'
         version false
         entityNotes column:'entity_id', joinTable: false
      ]
      static hibernateFilters = {
         notesFilter(condition: 'entity_type_id=1', collection:'entityNotes', default: true)
      }
 }

 class Order {
      static hasMany = [entityNotes: EntityNotes]
      static mapping = [
         table 'Order'
         id generator: 'identity'
         version false
         entityNotes column:'entity_id', joinTable: false
      ]
      static hibernateFilters = {
         notesFilter(condition: 'entity_type_id=2', collection:'entityNotes', default: true)
      }
 }

If there is only one hasMany to EntityNotes, its working fine. But when i have two Domains with same hasMany it is throwing a MappingException.

"org.hibernate.MappingException: Repeated column in mapping for entity: com.mycompany.domain.EntityNotes column: entity_id (should be mapped with insert="false" update="false")"
JChap
  • 1,421
  • 9
  • 23
  • It seems at first glance like the `column:'entity_id'` is not required in the `Order`/`Customer`/etc. classes. You already have this defined in the `EntityNotes` class. – tylerwal Dec 06 '15 at 02:03
  • Actually i started with that, but it was throwing an error com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'customer_entity_notes'. – JChap Dec 06 '15 at 02:09
  • Why are you setting the joinTable to false? – Emmanuel Rosa Dec 06 '15 at 02:14
  • @JavaChap, you are right, without the explicit `column` mapping then the default column would be `customer_id`, `order_id`. – tylerwal Dec 06 '15 at 02:28
  • @EmmanuelRosa Because there is no Join Table. – JChap Dec 06 '15 at 06:08
  • Since EntityNote is shared and therefore can't use belongsTo, how can the "many" aspect work without a join table? – Emmanuel Rosa Dec 06 '15 at 10:07
  • hasMany mapping adds column in EntityNotes without join table, you force this column to be entity_id, so it is not possible to have 3 columns with the same name. Database cannot use the same column for different foreign keys – practical programmer Dec 06 '15 at 11:24

0 Answers0