【问题标题】:How do I create association by joining non primary key column如何通过加入非主键列来创建关联
【发布时间】:2011-07-31 06:05:19
【问题描述】:
class Contact {
String name
String number
}

class Message {
String text
String number   
Contact contactInfo //If any
}

我需要加入 Message.number = Contact.number。关于在 Grails/GORM 中创建与非主键列的关联有什么想法吗?

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    我很确定这在 GORM 中是不可能的,我不知道在常规的 Hibernate 中是否可能。但你可以伪造它:

    class Message {
       String text
       String number
    
       static transients = ['contactInfo']
    
       Contact getContactInfo() {
          Contact.findByNumber(number)
       }
       void setContactInfo(Contact contact) {
          number = contact.number
       }
    }
    

    【讨论】:

    • 悲伤...这在 EF 中很容易实现。不知道为什么这是不可能的。我可以根据联系人姓名订购结果吗?即分页和排序功能?
    • 不幸的是,2 年后我仍然为这个问题找到了唯一的答案 - jira.grails.org/browse/GRAILS-7964 仍未标记为已解决:(
    【解决方案2】:

    Burt,这可以通过使用 property-ref 属性的 hibernate 来实现

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-23
      • 2021-09-22
      • 2017-07-21
      相关资源
      最近更新 更多