如何在grails中实现带有区分符的tablePerHierarchy? [英] How to implements tablePerHierarchy with disciminator in grails?

查看:61
本文介绍了如何在grails中实现带有区分符的tablePerHierarchy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个阶级阶层:

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

我希望每个类都有一个表,因此,我在Item中添加了tablePerHierarchy false

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

我需要一个判别器来实现查询:where class ="Contribution"

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

我尝试了很多实施,但是没有用.

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

该怎么做?

谢谢

推荐答案

您要按层次结构使用表还是按类创建表?您的问题不清楚.

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
}

使用默认的每个层次结构表"策略,将仅使用一个表来存储Items和Items的所有子类.默认的鉴别符列称为class,grails将自动使用. grails schema-export生成的模式如下:

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)
);

两个类只有一个表,其中包含层次结构中每个类中声明的所有字段以及鉴别符列class.如果执行类似Participation.list()的查询,则SQL grails生成如下所示:

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'

通过在Item.groovy中使用static mapping { tablePerHieracrchy false }将继承策略更改为每个类的表,grails将为层次结构中的每个类生成一个表.每个表仅存储在每个类中声明的字段,因此,参与"对象将由项目"表和参与"表中的一行表示.模式如下:

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)
);

Participation.list()的SQL更改为:

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

这篇关于如何在grails中实现带有区分符的tablePerHierarchy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆