Java JPA/Hibernate“Composite-id 类不会覆盖 equals()”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22646456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:04:14  来源:igfitidea点击:

JPA/Hibernate "Composite-id class does not override equals()"

javajpa

提问by John Humphreys - w00te

I'm using JPA and getting the following warning. I've researched this, and understand why I need to override it and how Hibernate uses these methods. I still have a question though:

我正在使用 JPA 并收到以下警告。我对此进行了研究,并了解为什么我需要覆盖它以及 Hibernate 如何使用这些方法。不过我还有一个问题:

Exception:

例外:

Composite-id class does not override equals()

Composite-id 类不会覆盖 equals()

Question:

题:

Why does Hibernate only care about classes that don't have composite IDs? Does it by default compare on the @Idfield if there is only one present, or is there something more complex going on here?

为什么 Hibernate 只关心没有复合 ID 的类?默认情况下,@Id如果只有一个存在,它是否会在字段上进行比较,或者这里是否有更复杂的事情发生?

采纳答案by JB Nizet

Because when entities don't have a composite ID, they have a single one, of one of the basic supported types (Integer, Long, String, etc.), and those classes already have a well-defined equals()(and hashCode()) method.

因为当实体没有复合 ID 时,它们只有一个,一种基本支持的类型(Integer、Long、String 等),并且这些类已经有一个明确定义的equals()(和hashCode())方法。

回答by Koitoer

Using JPA when you use composite key, you should use either IdClassor EmbeddedIdusing any of them you need to create an own class that act as a composite key, in order to be able to compare objects using this composite key which is required by several operations within the EntityManagerthat key classes must to override equalsand hashCode.

在使用复合键时使用 JPA,您应该使用IdClassEmbeddedId使用它们中的任何一个,您需要创建一个自己的类作为复合键,以便能够使用此复合键来比较对象,这是其中的多个操作所需的在EntityManager这关键的类必须重写equalshashCode

Taking from specs:

从规格中获取:

A composite primary key must correspond to either a single persistent field or property or to a set of such fields or properties as described below. A primary key class must be defined to represent a composite primary key. Composite primary keys typically arise when mapping from legacy databases when the database key is comprised of several columns. The EmbeddedIdor IdClassannotation is used to denote a composite primary key.

复合主键必须对应于单个持久字段或属性,或者对应于如下所述的一组此类字段或属性。必须定义主键类来表示复合主键。当数据库键由多个列组成时,从旧数据库映射时通常会出现复合主键。的EmbeddedIdIdClass注解用于表示一个复合主键。

And when using composite primary keys must follow.

并且在使用复合主键时必须遵循。

  1. The primary key class must be public and must have a public no-arg constructor.
  2. The primary key class must be serializable.
  3. The primary key class must define equalsand hashCodemethods. The semantics of value equality for these methods must be consistent with the database equality for the database types to which the key is mapped.
  1. 主键类必须是公共的,并且必须有一个公共的无参数构造函数。
  2. 主键类必须是可序列化的。
  3. 主键类必须定义equalshashCode方法。这些方法的值相等的语义必须与键映射到的数据库类型的数据库相等一致。