如何防止不必要的更新持久对象? [英] How to prevent unwanted update of persisted objects?

查看:108
本文介绍了如何防止不必要的更新持久对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Load 类和一个货物类。 Load 的实例包含货物的实例。

  class Load {
货物货物
}

类货物{
字符串名称
BigDecimal成本
}

可以说名称是一个货物的实例是rock, cost 是11.11。现在我使用 Cargo 这个实例来创建一个 Load 的实例。既然已经创建了 Load 的实例,我不希望它的货物发生变化。例如,如果我将rock的价格更改为22.22,我不希望 Load 实例中货物的价格发生变化。什么是处理这种情况的最佳方式?

(按照您的示例),您需要将 cost 属性添加到 Load 。然后,您将需要在创建对象时复制成本。您可以通过Load对象中的重载方法来完成此操作,或者编写自己的方法将 Cargo 添加到 Load



如果您确实需要多个 Cargoes 每个加载,那么您需要一个中间对象来表示连接。该对象将存储它们关联时的价格。这可能看起来像:

  class Load {
LoadCargo loadCargo
} $ b $ class LoadCargo {
货物货物
BigDecimal成本
}
类货物{
字符串名称
BigDecimal成本
}



当然,由于与Cargo的间接关系,您的对象模型会更复杂一些。



Edit
处理多重相关情况的另一种方法是在每次更新成本时复制 Cargo 。如果您期望 cost 大部分是静态的,这可能会更有效率。我会通过添加一个字段来禁用物品来处理这个问题,例如:

  class Cargo {
String名称
BigDecimal花费
boolean active = true
}

I相信你可以像这样简单地克隆货物:

  oldCargo = Cargo.get(params.id)
newCargo = new Cargo()
newCargo.properties = oldCargo.properties
//现在设置新的开销等
newCargo.save()
oldCargo.active = false
oldCargo.save()

这只是一种猜测,它可能已被打破。


I have a Load class and a Cargo class. An instance of Load contains an instance of Cargo.

class Load {
    Cargo cargo
}

class Cargo {
    String name
    BigDecimal cost
}

Lets say that the name of an instance of Cargo is "rock" and the cost is "11.11". Now I use this instance of Cargo to create an instance of Load. Now that the instance of Load has been created I don't want it's "cargo" to change. For instance, if I change the price of "rock" to "22.22" I don't want the price of the "cargo" in my instance of Load to change. What is the optimal way to handle such a situation?

解决方案

Well, if you can only have one Cargo per Load (as per your example), you'll want to add a cost property to Load. Then you will need to copy over the cost at the time the objects are created. You might be able to do this via an overloaded method in the Load object, or just write your own method to add Cargo to Load.

If you actually need multiple Cargoes per Load, then you'll want an intermediate object to represent the connection. This object will store the price at the time they were associated. That might look like:

class Load {
    LoadCargo loadCargo
}
class LoadCargo {
    Cargo cargo
    BigDecimal cost
}
class Cargo {
    String name
    BigDecimal cost
}

Of course, your object model will be a bit more complicated, because of the indirect relationship to Cargo.

Edit Another way to handle the multiple-related situation is to duplicate the Cargo every time the cost is updated. This might be more efficient if you are expecting cost to be mostly static. I'd handle this by adding a field to "disable" items, like so:

class Cargo {
    String name
    BigDecimal cost
    boolean active = true
}

I believe you can simply clone the Cargo on update like this:

oldCargo = Cargo.get(params.id)
newCargo = new Cargo()
newCargo.properties = oldCargo.properties
// now set the new cost, etc.
newCargo.save()
oldCargo.active = false
oldCargo.save()

That's just a guess, it probably is broken.

这篇关于如何防止不必要的更新持久对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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