GORM/Grails - add extra column to a joinTable expression

亡梦爱人 提交于 2019-12-12 20:12:47

问题


I have a Domain Class setup similar to this

class NewsStory {
  String headline
  static hasMany = [channels:Channel]
  static mapping = {
      table 'NewsStory'
      addresses joinTable:[name:'Article_Channel', key:'ArticleId', column:'ChannelId']
  }
}

in the Article_Channel table i need to have an extra column populated called ArticleType say. Its value will always be the same e.g. 'news' for this domain class, but will be differnt for others e.g. 'blog' Channel is just something like 'Security' etc

Is there a way? Thanks


回答1:


One option would be be to create your own many-to-many mapping class and add the field in there.

http://grails.org/Many-to-Many+Mapping+without+Hibernate+XML

So, for example:

class ArticleChannel {
    NewsStory newsStory
    Channel channel
    String articleType
}

Then, your NewsStory and Channel classes would hasMany the ArticleChannel class.



来源:https://stackoverflow.com/questions/7867621/gorm-grails-add-extra-column-to-a-jointable-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!