等于持久对象
对于具有数据库管理 ID 的持久性对象实现 equals() (和 hashCode(),我将仅讨论 equals())存在众所周知的问题。 新对象不存储在数据库中,因此没有数据库标识,因此它的“id”字段为空(如果是原始类型则为 0)。
如果 equals 查看 id,它将认为所有新对象相等,并且一旦获得 id,哈希码就会更改,因此如果它已经在哈希敏感集合中,则将找不到它。
一种解决方案是使用业务密钥,但有时除了代理 ID 之外的所有内容都是可变的。 另一种解决方案是在创建对象时生成(再一个,或者也将其用作数据ID)代理ID。
我没有看到提到的方法是,在 equals 中使用 id 并在 id 为 null 时使 equals(和 hashCode())失败(抛出 IllegalStateException)。 (并记录此行为) 这样它仍然不能在哈希集合中,但也不能被意外地放在那里。为了在没有 id 的情况下将其放入集合中,可以使用一些包装器。 这是好/坏主意吗?是否有隐藏的问题?
正如 kan 所指出的,如果子对象应该放入 Set 属性并与其父对象一起持久化,则在持久化之前无法将对象放入 Set 中是个大问题(并且 TreeSet 没有帮助,因为它使用 equals(),即使它不使用 hashCode())。 我主要使用子实体列表,因此它不需要显现,但这绝对是问题。
There is well known problem with implementing equals() (and hashCode(), i will speak about equals() only) for persistance object with database managed id.
New object is not stored in database, therefore does not have database identity, therefore its "id" field is null (or 0 if it is primitive type).
If equals look at id, it will consider all new objects equal, and once it gets id, hash code change so if it already was in hash sensitive collection, it will not be found.
One solution is using business key, but sometimes everything except surrogate id is mutable.
Another solution is to generate (one more, or use it as databes id too) surrogate id when object is created.
Approach I haven't seen mentioned is, use id in equals and make equals (and hashCode()) fail (throw IllegalStateException) when id is null. (and document this behavior)
This way it still can't be in hash collections, but it cannot be accidentaly put there. And for puting it in collections when without id, some wrapper can be used.
Is it good/bad idea? Does it have hiddent problems?
As kan pointed out, if child objects should be put in Set property and persisted with their parent, inability to put objects in Set before they are persisted is big problem (and TreeSet does not help, because it uses equals(), even if it does not use hashCode()).
I mostly use list for child entities, so it does not need to manifest, but it definitely is problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用下一个代码。它涵盖了大多数情况,并且可以用于我认为使用 ORM 时可能发生的所有情况。
在分配 id 之前,您必须能够使用集合。如果您想创建一个带有
Set
属性的对象,其中包含其他对象包,则必须将它们作为元素添加到Set
中。I use the next code. It covers most cases and it could be used for all cases which I think could occur while using ORM.
You has to able to use collections before assigning the id. If you want create an object with a
Set
property which contains pack of other objects, you have to add them as elements to theSet
.我总是使用自动生成的 ID,从来没有遇到过问题。您可以在实例化时强制对象也使用服务层/工厂进行持久化。
我认为任何其他字段(组成 biz key)更改的可能性比在哈希图中使用非持久化对象然后同时持久化导致查找失败的可能性要大得多。
恕我直言,这个问题有点过度分析。自动生成的 id 通常是我想做的唯一测试是否相等,在很多情况下没有其他任何意义。我采取的方法是,如果使用/比较非持久对象,则问题出在业务逻辑中,而不是底层的 equals/hashcode 方法中。
为了具体回答非法状态异常的想法,当对象不相等和/或具有时抛出异常没有被坚持似乎相当戏剧化。
I always use the auto-generated id and have never had a problem. You could enforce an object when instantiated to also be persisted using your service layer/factory.
I think the likelihood of any other field (composing a biz key) changing is much more probable than using a non-persisted object in a hashmap and then persisting at the same time causing the look up to fail.
This problem, imho, is somewhat over-analysed. The auto-generated id is often the only test I want to do for equality, nothing else makes sense in a lot of cases. I take the approach that if a non-persisted object is being used/compared the problem is in the business logic and not the underlying equals/hashcode methods
To specifically answer the illegalstateexception idea, throwing an exception when objects are not equal and/or have not been persisted seems rather dramatic.