本文介绍了多态属于Grails中的多对多映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道这是可能的使用超类,但是,这是非常限制的灵活性。所以我的问题是,我可以使用接口吗?

 界面Taggable {
/ *添加标签并返回当前设置的标签列表* /
列表< String> addTags(String ...标签)
/ *删除标签并返回当前设置的标签列表* /
List< String> removeTags(String ... tag)
}

class用户实现Taggable {
字符串用户名
static hasMany = [tags:Tag]
}

类标记{
字符串名称

静态hasMany = [引用:Taggable]
static belongsTo = Taggable

静态约束= {
name(可空:假,空白:假,唯一:真)
}
}

我对带有以下标记的对象的引用感兴趣。但是这个对象不能扩展一个具体的类。这就是为什么即时通讯想知道这是否可以用接口来完成。



所以,可以这样做吗?

$ b $ Hibernate可以映射一个接口 - 。我怀疑Grails是否支持按惯例映射 - 但您可以尝试使用映射 from above example,or XML config。

edit :回答评论问题:

在数据库级别,必须为 Taggable Tag.References 引用一个外键。


  1. Discriminator will not如果它是自动添加的 - 例如,在每个层次的表映射中,Hibernate / Gorm会添加一个 class 字段,以便在读取对象时找到具体的类如果你将 Taggable s映射到两个表 - Taggable 部分为 Taggable 以及其他所有内容添加到特定表格,参考1:1 - 所有鉴别器都可以工作应该由Hibernate为您完成。

  2. $ c>字段非常长 - 它包含完全限定的类名称。。

    编辑2
    无论哪种方式,它变得非常复杂,我个人会采用我在另一个问题中建议的方法:




    • 动态查询所有带有Taggable接口的类,其中 hasMany = [tags:Tag] 属性; li>
    • ,或者不太可取的是有一个手工制作的子表和一个鉴别符。


    So i know this is possible using a superclass, however, this is very limiting in flexibility. So my question is then, can i use an interface? Something ala.

    interface Taggable {
      /*Adds tag(s) and returns a list of currently set tags*/
      List<String> addTags(String ... tag)
      /*Removes tag(s) and returns a list of currently set tags*/
      List<String> removeTags(String ... tag)
    }
    
    class User implements Taggable {
      String username
      static hasMany = [tags:Tag]
    }
    
    class Tag {
      String name
    
      static hasMany = [references:Taggable]
      static belongsTo = Taggable
    
      static constraints = {
        name(nullable: false, blank: false, unique: true)
      }
    }
    

    Im interested in a reference back to the object who has the following tag. This object however can't extend a concrete class. Thats why im wondering if this can be done with an interface instead.

    So, can it be done?

    解决方案

    Hibernate can map an interface - see example. I doubt if Grails supports this in by-convention mapping - but you can try using the mapping annotations from example above, or XML config.

    edit: answering a comment question:

    On a database level, you have to have a Taggable table for Tag.References to reference with a foreign key.

    1. Discriminator will NOT defeat polymorphism, if it's added automatically - for instance, in table-per-hierarchy mapping, Hibernate/Gorm adds a class field in order to find out a concrete class when reading object from db.

    2. If you map your Taggables to two tables - Taggable part to Taggable and everything else to specific table, referenced 1:1 - all the discriminator work should be done for you by Hibernate.

    BTW class field is pretty long - it contains fully qualified class name.

    edit 2:Either way, it's getting pretty complex, and I'd personally go with the approach I suggested in another question:

    • dynamically query all the classes with Taggable interface for hasMany=[tags:Tag] property;
    • or, less preferable - to have a hand-crafted child table and a discriminator.

    这篇关于多态属于Grails中的多对多映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 08:37