Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gorm mapping for inheritance

I'm facing a issue regarding inheritance in Grails. I have a domain class Person.grooy:

class Person{
    String name
    String contactNumber
    Address address
} 

Now I'm extending Person.groovy for Employee and Customer like:

class Employee extends Person{
    String designation
}
class Customer extends Person{
    String interest
}

Now I want separate table in my database for Employee and Customer having columns of Person i.e name,contactNumber and associate address key. How could I achieve this. I searched every where but there is nothing for this aspect. Is this one approach is not possible in GORM. Please answer. Thanks Guys

like image 870
Yogesh Avatar asked Dec 30 '13 12:12

Yogesh


3 Answers

Finally I managed to get what I want just by placing a grails.persistence.Entity annotation to my child domain classes. I also make my parent i.e. Person.groovy abstract and place in src/groovy.

Now I have database hierarchy as I expected but some scaffold issues in controller still persist that will also sorted out with your help.

like image 139
Yogesh Avatar answered Nov 15 '22 07:11

Yogesh


You need to disable table-per-hierarchy, which is by default enabled in Grails

class Employee extends Person{
    String designation

static mapping = {
        tablePerHierarchy false
    }
}

table-per-hierarchy Ref

like image 4
dhamibirendra Avatar answered Nov 15 '22 07:11

dhamibirendra


If you put your Person class in src/java or src/groovy it won't be mapped to the db.

Remember to import it into your Employee and Customer classes

import com.yourPackage.Person

class Employee extends Person{

}
like image 4
Hernán Erasmo Avatar answered Nov 15 '22 07:11

Hernán Erasmo