我应该在Grails中使用复合主键吗? [英] Should I use composite primary keys in Grails?

查看:249
本文介绍了我应该在Grails中使用复合主键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Grails文档不鼓励使用组合主键,但在此视频(26:00 - 29:00)@BurtBeckwith 使用复合主键,因为他谈到了将连接表映射到域类而不是使用集合的性能优势。这提出了几个问题:
$ b $ ol

  • 为什么Grails文档不鼓励使用组合主键?

  • 为什么Burt甚至使用复合键?我尝试没有一个,一切似乎都很好。我也没有重写 hashcode equals

  • Burt正在使用Grails 1.3在制作视频时是否对收藏仍然有效?我可以通过打开SQL日志来自己测试,但我还没有完成。
  • Hibernate更喜欢一个简单的主键,即使有一个自然的唯一键(例如User表中的用户名),因为自动递增长整型值更容易用作外键。它当然支持其他方法,GORM也是如此。



    我在UserRole表中使用组合PK的原因是为了保持它与隐式创建的联接表相同当你在GORM中使用多对多时你会得到。我提出的方法与数据库相同,但是性能更高,并且您还可以更多地控制定制连接表。但可以随意将复合PK更改为简单的两个外键(理想情况下具有唯一索引)并添加常规单列主键。只要您不打算将其用作GORM多对多连接表,这没什么。

    The Grails docs discourage the use of composite primary keys, but in this video (26:00 - 29:00) @BurtBeckwith uses composite primary keys as he speaks about the performance benefits of mapping of join tables to domain classes as opposed to using collections. This raises several questions:

    1. Why do the Grails docs discourage the use of composite primary keys?
    2. Why is Burt even using a composite key? I tried without one and everything seems to work fine. I also didn't override hashcode or equals.
    3. Burt was using Grails 1.3 when that video was made, are his performance concerns regarding collections still valid? I can test this myself by turning on SQL logging, but I haven't done it yet.

    解决方案

    Hibernate prefers a simple primary key, even if there's a natural unique key (e.g. username in a User table) since auto-incrementing long values are simpler to use as foreign keys. It supports other approaches of course, and so does GORM.

    The reason I use a composite PK in the UserRole table is to keep it the same as the implicitly created join table that you get when you use a many-to-many in GORM. The approach I proposed was intended to be the same as far as the database is concerned, but more performant, and you also have more control over customizing the join table. But feel free to change the composite PK to simply two foreign keys (ideally with a unique index) and to add a regular single-column primary key. This is fine as long as you don't plan on using it as a GORM many-to-many join table.

    这篇关于我应该在Grails中使用复合主键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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