grails / gorm 支持 1 - 许多关联中的不同父实体

发布于 2024-12-11 16:42:49 字数 463 浏览 0 评论 0原文

我有以下“顶级”(父)域实体:

客户
公司
联系人

以及以下子实体:

地址

每个顶级域实体之间存在一对多关系:

客户 -> 客户地址
公司->地址
联系方式->地址

即客户、公司或联系人可以有一个或多个地址。

不幸的是我不知道如何在 grails / gorm 中对此进行建模。看来我只能在地址中定义一个父级或belongsTo声明,即我无法使用以下方式声明地址:

Address {
    Customer parent //??
    Company parent //??
    Contact parent //??
}

有人可以告诉我是否缺少某些内容或者是否可以以受支持的方式定义这种类型的关系?

谢谢,

考珀

I have the following "top-level" (parent) domain entities:

Customer
Company
Contact

And the following child entity:

Address

There is one to many relationship between each of the top level domain entities:

Customer -> Address
Company -> Address
Contact -> Address

i.e. a customer, company or contact can have one or more addresses.

Unfortunately I do not know how to model this in grails / gorm. It seems I can only define one parent or belongsTo declaration in address i.e. I was not able to declare Address using:

Address {
    Customer parent //??
    Company parent //??
    Contact parent //??
}

Can someone tell me if I am missing something or if it's possible to define this type of relationship in a supported way?

Thanks,

cowper

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-12-18 16:42:49

正如 Tim 指出的那样,您应该能够使用 belongsTo 的数组版本:

Address {
    static belongsTo = [Customer, Company, Contact]
}

如果实体可以共享公共地址,则可能会改变您配置删除的方式。

另一种选择是让这三个类 从超类继承属性,但无论这对您的情况是否有意义,我不知道(看起来不像)。

You should be able to use the array version of belongsTo as Tim pointed out:

Address {
    static belongsTo = [Customer, Company, Contact]
}

If entities can share a common address may change the way you configure deletes.

Another option is to have those three classes inherit the property from a superclass, but whether or not that makes sense in your case, I don't know (it kind of doesn't look like it).

星光不落少年眉 2024-12-18 16:42:49

在我们的应用程序中,我们有几个需要地址的实体。但我们选择以多对多关系对它们进行建模。

地址看起来像这样

class Address {

  // typical address properties

  Set<Company> getCompanies() {
    CompanyAddress.findAllByAddress(this).collect { it.company } as Set
  }

  static constraints = {
    // typical constraints
  }
}

对于每个“父母”,我们提供一个吸气剂。您可以在上面的代码中看到 getCompanies()。如果每个地址只有 1 个公司,那么只需让该 getter 返回 1 个公司而不是 Set。 Company 内部则相反,我们有一个 getAddresses()。

例如,公司地址看起来像这样......

class CompanyAddress implements Serializable{

  Address address
  Company company

  boolean equals(other) {

    if (this.is(other)){
      return true
    }

    if (!(other instanceof CompanyAddress)) {
      return false
    }

    other.address?.id == address?.id &&
        other.company?.id == company?.id
  }

  int hashCode() {
    def builder = new HashCodeBuilder()
    if (address) builder.append(address.id)
    if (company) builder.append(company.id)
    builder.toHashCode()
  }

  static CompanyAddress get(long companyId, long addressId) {
    find 'from CompanyAddress where address.id=:addressId and company.id=:companyId',
      [addressId: addressId, companyId: companyId]
  }

  static CompanyAddress create(Company company, Address address, boolean flush = false) {
    new CompanyAddress(address: address, company: company).save(flush: flush, insert: true)
  }

  static boolean remove(Company company, Address address, boolean flush = false) {
    CompanyAddress instance = CompanyAddress.findByAddressAndCompany(address, company)
    instance ? instance.delete(flush: flush) : false
  }

  static void removeAll(Address address) {
    executeUpdate 'DELETE FROM CompanyAddress WHERE address=:address', [address: address]
  }

  static void removeAll(Company company) {
    executeUpdate 'DELETE FROM CompanyAddress WHERE company=:company', [company: company]
  }

  static mapping = {
    id composite: ['address', 'company']
    version false
  }
}

In our application we have several entities that need addresses. But we've chosen to model them in a many-to-many relationship.

Address looks like this

class Address {

  // typical address properties

  Set<Company> getCompanies() {
    CompanyAddress.findAllByAddress(this).collect { it.company } as Set
  }

  static constraints = {
    // typical constraints
  }
}

And for each "parent" we provide a getter. You can see getCompanies() in the code above. If you're only every going to have 1 company per address, then simply have that getter return the 1 company instead of a Set. The inverse is true inside Company, we have a getAddresses().

Company Address, for example, looks like this...

class CompanyAddress implements Serializable{

  Address address
  Company company

  boolean equals(other) {

    if (this.is(other)){
      return true
    }

    if (!(other instanceof CompanyAddress)) {
      return false
    }

    other.address?.id == address?.id &&
        other.company?.id == company?.id
  }

  int hashCode() {
    def builder = new HashCodeBuilder()
    if (address) builder.append(address.id)
    if (company) builder.append(company.id)
    builder.toHashCode()
  }

  static CompanyAddress get(long companyId, long addressId) {
    find 'from CompanyAddress where address.id=:addressId and company.id=:companyId',
      [addressId: addressId, companyId: companyId]
  }

  static CompanyAddress create(Company company, Address address, boolean flush = false) {
    new CompanyAddress(address: address, company: company).save(flush: flush, insert: true)
  }

  static boolean remove(Company company, Address address, boolean flush = false) {
    CompanyAddress instance = CompanyAddress.findByAddressAndCompany(address, company)
    instance ? instance.delete(flush: flush) : false
  }

  static void removeAll(Address address) {
    executeUpdate 'DELETE FROM CompanyAddress WHERE address=:address', [address: address]
  }

  static void removeAll(Company company) {
    executeUpdate 'DELETE FROM CompanyAddress WHERE company=:company', [company: company]
  }

  static mapping = {
    id composite: ['address', 'company']
    version false
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文