@Transient 属性应该在 equals/hashCode/toString 中使用吗?
我有 JPA 实体,其中一些属性用 @Transient
注释。
我应该在 equals/hashCode/toString 方法中使用这些属性吗?
我的第一个想法是不,但我不知道为什么。
- 尖端?
- 有想法吗?
- 解释?
I have JPA entities where some properties are annotated with @Transient
.
Should I use these properties in equals/hashCode/toString
methods?
My first thought is NO but I don't know why.
- Tips?
- Ideas?
- Explanations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
toString()
的情况有所不同,您可以使用toString()
执行任何您想要的操作,因此我将仅介绍equals()
(并且hashCode()
)。首先,规则:如果您想将对象存储在
List
、Map
或Set
中,那么它是要求实现equals
和hashCode
,以便它们遵守文档中指定的标准约定。现在,如何实现
equals()
和hashCode()
呢?一个“自然”的想法是使用映射为Id
的属性作为equals()
的一部分:不幸的是,这个解决方案有一个主要问题:当使用生成的标识符时,直到实体变得持久时才会分配值,因此如果瞬态实体在被添加到
Set
之前保存后,其哈希码在Set
中时会发生变化,这会破坏Set
的约定。因此,推荐的方法是使用属于业务密钥一部分的属性,即对于具有相同数据库标识的每个实例来说唯一的属性组合。例如,对于 User 类,这可能是用户名:
Hibernate 参考文档对此进行了如下总结:
那么,回到最初的问题:
@Transient
属性很可能不是此类键的一部分。List
、Map
、Set
之前获取分配的值。另请参阅
The case of
toString()
is different, you can do whatever you want withtoString()
so I will only coverequals()
(andhashCode()
).First, the rule: if you want to store an object in a
List
,Map
or aSet
then it is a requirement thatequals
andhashCode
are implemented so they obey the standard contract as specified in the documentation.Now, how to implement
equals()
andhashCode()
? A "natural" idea would be to use the properties mapped asId
as part of theequals()
:Unfortunately, this solution has a major problem: when using generated identifiers, the values are not assigned until an entity becomes persistent so if a transient entity is added to a
Set
before being saved, its hash code will change while it's in theSet
and this breaks the contract of theSet
.The recommended approach is thus to use the attributes that are part of the business key i.e. a combination of attributes that is unique for each instance with the same database identity. For example, for the User class, this could be the username:
The Hibernate Reference Documentation summarizes this as follow:
So, back to the initial question:
@Transient
attributes are very likely not part of such a key.List
,Map
,Set
.See also
据我所知,@Transient 和 Transient 的两种典型用法是将它们用于无法序列化/持久化的内容(例如远程资源) 句柄)或可以从其他属性重建的计算属性。
对于计算数据,在相等关系(
equals/hashCode
)中使用它们是没有意义的,因为它是多余的。该值是根据等式中已使用的其他值计算得出的。然而,在 toString 中打印它们仍然有意义(例如,使用基本价格和比率来计算实际价格)。对于不可序列化/可持久化的数据,这取决于情况。我可以想象一个不可序列化的资源的句柄,但您仍然可以比较该句柄代表的资源名称。对于
toString
也是如此,也许打印句柄资源名称很有用。这是我的 2 美分,但如果你解释一下 @Transient 的特殊用法,也许有人可以给出更好的建议。
The two typical usages of
@Transient
andtransient
that I'm aware of, are to use them either for stuff that can't be serialized/persisted (e.g. a remote resource handle) or computed properties which can be reconstructed from others.For computed data, it makes no sense to use them in the equality relationship (
equals/hashCode
), because it would be redundant. The value is computed out of other value which are already used in the equality. It can however still makes sense to print them intoString
(e.g. a base price and a ratio are used to compute the actual price).For not serializable/persitable data, it depends. I can imagine a handle to a resource that is not serializable, but you can still compare the resource name that the handle represent. Same for
toString
, maybe printing the handle resource name is useful.This was my 2 cent, but if you explain your particular usage of
@Transient
, someone can maybe give a better advice.异常可能来自于让它成为瞬态,同时您在处理它的地方提供了 writeObject() 和 readObject() 。
Exception maybe comes from letting it be
transient
and at the same time you providewriteObject()
andreadObject()
where you process it.