如何在 Grails 中使用 gorm 级联更新?
我的域之间的关系:
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()
首先,我将 sampleAttributes 与 cartridgeLane 链接,然后我正在找到我想要更新的墨盒(链接到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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您调用更新方法/闭包的方式,您可以利用 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.
此级联更新有效:
This cascading update works :