如何在 Grails 中使用 gorm 级联更新?

发布于 2024-12-18 12:55:09 字数 1230 浏览 1 评论 0原文

我的域之间的关系:

class Cartridge
{ ...
  static hasMany = [cartridgeLanes: CartridgeLane]
  static belongsTo = [request:Request]
}

class CartridgeLane 
{...
  CartridgeLaneSampleAttributes cartridgeLaneSampleAttributes
  static belongsTo = [cartridge:Cartridge]
}

class CartridgeLaneSampleAttributes
{...
  static belongsTo = CartridgeLane
}

级联更新代码:

  def sampleAttributes = new CartridgeLaneSampleAttributes()
        sampleAttributes.sampleId = sample.get("ID")
        ...

  def cartridgeLane = new CartridgeLane()
        cartridgeLane.laneId = lane.get("ID") as Integer
        ...
        cartridgeLane.cartridgeLaneSampleAttributes = sampleAttributes

  def cartridge = Cartridge.findByCartridgeID(cartridgeId)
  cartridge.addToCartridgeLanes(cartridgeLane)
  cartridge.save()

首先,我将 sampleAttributescartridgeLane 链接,然后我正在找到我想要更新的墨盒(链接到cartridgeLane),最后我正在保存。

此代码仅INSERT数据(例如:将sampleAttributes和cartridgeLane数据添加到cartridge)。
我的问题是我正在寻找更新(而不是插入)。

如何管理级联更新?
gorm 如何区分更新和插入?
有没有更有效的级联更新方法?

Relationship between my domains :

class Cartridge
{ ...
  static hasMany = [cartridgeLanes: CartridgeLane]
  static belongsTo = [request:Request]
}

class CartridgeLane 
{...
  CartridgeLaneSampleAttributes cartridgeLaneSampleAttributes
  static belongsTo = [cartridge:Cartridge]
}

class CartridgeLaneSampleAttributes
{...
  static belongsTo = CartridgeLane
}

Code for cascade update :

  def sampleAttributes = new CartridgeLaneSampleAttributes()
        sampleAttributes.sampleId = sample.get("ID")
        ...

  def cartridgeLane = new CartridgeLane()
        cartridgeLane.laneId = lane.get("ID") as Integer
        ...
        cartridgeLane.cartridgeLaneSampleAttributes = sampleAttributes

  def cartridge = Cartridge.findByCartridgeID(cartridgeId)
  cartridge.addToCartridgeLanes(cartridgeLane)
  cartridge.save()

First, I'm linking sampleAttributes with cartridgeLane, then I'm finding the cartridge I want to update (linking to cartridgeLane), and finally I'm saving.

This code only INSERT data (eg : add sampleAttributes and cartridgeLane data to cartridge).
My problem is I'm looking for an UPDATE (not an INSERT).

How can I manage cascading update ?
How gorm differentiate an update from an insert ?
Is there a more efficient way for cascading update ?

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

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

发布评论

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

评论(2

陌伤浅笑 2024-12-25 12:55:09

根据您调用更新方法/闭包的方式,您可以利用 Grails 数据绑定

顺便说一句,您的领域模型可能会混淆应用程序中的逻辑。如果没有更多细节,很难看出,但看起来您正在尝试使用 Map 或其他东西将属性动态绑定到域类。这种类型的操作将需要您自己编写大量逻辑,并使您的模型变得复杂。您可能需要重新考虑设计。

Depending on how you're calling your update method/closure you may be able to leverage Grails data binding.

As an aside, your domain model may be obfuscating the logic in your application. Hard to see without more detail, but it looks like you're trying to have dynamically bound attributes to a domain class using a Map or something. This type of manipulation will require you to write a lot of the logic yourself, and complicate your model. You may want to reconsider the design.

尐偏执 2024-12-25 12:55:09

此级联更新有效:

def cartridge = Cartridge.findByCartridgeID(cartridgeId).id as Long
def cartridgeLane = CartridgeLane.findCartridgeLanesByCartridge(cartridge).findByLaneId(laneId)  

// Update cartridgeLane domain
cartridgeLane.laneId = lane.get("ID") as Integer
...

// update cartridgeLaneSampleAttributes domain
cartridgeLane.cartridgeLaneSampleAttributes.sampleId = sample.get("ID")
...

cartridgeLane.save()

This cascading update works :

def cartridge = Cartridge.findByCartridgeID(cartridgeId).id as Long
def cartridgeLane = CartridgeLane.findCartridgeLanesByCartridge(cartridge).findByLaneId(laneId)  

// Update cartridgeLane domain
cartridgeLane.laneId = lane.get("ID") as Integer
...

// update cartridgeLaneSampleAttributes domain
cartridgeLane.cartridgeLaneSampleAttributes.sampleId = sample.get("ID")
...

cartridgeLane.save()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文