2

I have a class hirarchy :

class Item {}
class Participation extends Item{}
class Contribution extends Participation{}
class Question extends Participation{}

I would like to have a table per class so, I add tablePerHierarchy false in Item

I need a discrimator to implements a query : where class = "Contribution"

I try a lot of implementation but it's not working.

How to do that ?

Thanks

Jonathan Lebrun
  • 1,462
  • 3
  • 20
  • 42
  • Have you find any solution on this? I have exactly the same problem, and ataylor's answer is not doing what I need. – Lrrr Jul 02 '14 at 05:07

1 Answers1

7

Do you want table per hierarchy or table per class? It's not clear in your question.

With the following domain objects, you can do it either way:

// Item.groovy
class Item {
    String x
}

// Participation.groovy
class Participation extends Item {                   
     String y
}

Using the default, table per hierarchy strategy, just one table will be used to store Items and all the subclasses of Items too. The default discriminator column is called class, which grails will use automatically. The schema generated by grails schema-export looks like this:

create table item (
    id bigint generated by default as identity (start with 1), 
    version bigint not null, 
    x varchar(255) not null,
    class varchar(255) not null,
    y varchar(255),
    primary key (id)
);

There's just one table for both classes which contains all the fields declared in every class in the hierarchy plus the discriminator column class. If you do a query like Participation.list(), the SQL grails generates looks like this:

select
    this_.id as id1_0_,
    this_.version as version1_0_,
    this_.x as x1_0_,
    this_.y as y1_0_
from
    item this_
where
    this_.class='Participation'

By changing the inheritance strategy to table per class with static mapping { tablePerHieracrchy false } in Item.groovy, grails will generate a table for each of classes in the hierarchy. Each table stores only the fields declared in each class, so a Participation object would be represented by a row in both the Item table and the Participation table. The schema looks like this:

create table item (
    id bigint generated by default as identity (start with 1),
    version bigint not null,
    x varchar(255) not null,
    primary key (id)
);

create table participation (
    id bigint not null,
    y varchar(255) not null,
    primary key (id)
);

And the SQL for Participation.list() changes to:

select
    this_.id as id1_0_,
    this_1_.version as version1_0_,
    this_1_.x as x1_0_,
    this_.y as y2_0_
from
    participation this_
inner join
    item this_1_
        on this_.id=this_1_.id
ataylor
  • 64,891
  • 24
  • 161
  • 189
  • With class Participation extends Item, I tried and it's clear but my question is more on the next inheritance 'Contribution extends Participation' and 'Question extends Participation'. The compiler request a discriminator on Question and only on question not on contribution. – Jonathan Lebrun Mar 05 '11 at 10:26
  • atayar could you take a look at my question : http://stackoverflow.com/questions/24523408/how-to-make-tableperhierarchy-false-and-use-discriminator – Lrrr Jul 06 '14 at 12:21