Inheritance in grails

0

I am developing an application in grails and I need to represent an inheritance between two entities. The problem is that when I establish the relationship:

class Documento {
static mapping = {
        tablePerHierarchy false
    }

class Tesis extends Documento{
}

When I generate the view of the thesis, it asks me to fill fields that belong to the Documents table and in the database I can not see the relationship between the two tables or I do not understand how that relationship is established.

    
asked by Paul 30.03.2016 в 23:28
source

3 answers

3

Grails by default uses the inheritance representation that is called TPH (table-per-hierarchy).

TPH uses a "discriminator" to determine which class is each tuple (column class ). As you can see it does not generate several tables for each class, but all the attributes are in the same table (the parent table in this case Persona ). For this reason, it is reasonable that certain fields remain empty, depending on which class is being inserted.

The bad thing about this representation is that it does not allow to have values not null . That is to say if we had, a class Profesor like the following:

class Profesor extends Persona {
    String materiaDictada
    // otras props...

    static constraints = {
    }
}

and subjectDate is not null , classes of the type person or student type could not be saved (since the INSERT would give an error). In this case you can use the TPS approach (table_per_subclass) that can be done through the ORM DSL .

The way to deactivate the default behavior of grails, would be what is mentioned in the initial question:

class Persona {
    ...
    static mapping = {
        tablePerHierarchy false
    }
}

I hope it works.

Greetings

    
answered by 30.04.2016 в 01:35
1

I help clarify the problem of friend Paul ... The theme is the following with another example a little more practical.

Domain:

package com.test

class Persona {
    String nombre
    String apellido
    Date fechaNacimiento

    static constraints = {
    }
}
class Alumno extends Persona{

    int legajo
    int curso
    int promedio

    static constraints = {
    }
}

In the database you can see the following when inserting 2 new students and 1 new person: (The following code is a SELECT * from PERSON)

# id, version, apellido, fecha_nacimiento, nombre, class, curso, legajo, promedio
1, 0, Argento, 2016-04-02 00:00:00, Pepe, com.test.Alumno, 3, 33333, 9
2, 0, Argento, 2016-04-02 00:00:00, Moni, com.test.Alumno, 1, 55555, 1
3, 0, Persona 1, 2016-04-02 00:00:00, Nombre Persona 1, com.test.Persona, null, null, null

There is no "STUDENT" table inside the BD

    
answered by 02.04.2016 в 21:15
1

Due to my own experience in several projects I have seen that it is not a very good idea to use inheritance, especially if you are working in Grails, the effects you could already check, that is, the database does not seem to be very consistent, that is why because in the relational model there is no way to face a situation of inheritance, therefore you can start to see tables with strange information / structure and even duplicate information, which is not a very good idea. Also at the time of developing unit tests / integration is a real headache trying to write the test code if the base code already contains cases of inheritance. I have personally developed several models in grails however instead of using inheritance relationships I have used compositional relationships, that has been great for me, please give a review to the following article.

link

Your model could be restructured to use a composition relationship as follows:

class Documento {
    static constraints {}
}

class Tesis {
    Documento documento
}

Said class model can be easily represented as 2 tables within the database.

    
answered by 01.12.2016 в 16:58