Java 如何在没有类的情况下使用 Hibernate 复合 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21175959/
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 07:07:51  来源:igfitidea点击:

How to use Hibernate composite-id without class?

javahibernateormhibernate-mapping

提问by orbfish

I am trying to map a key in xml as follows:

我试图在 xml 中映射一个键,如下所示:

    <composite-id>
        <key-property name="userId" column="USER_ID" />
        <key-property name="passwordsBack" column="PASSWORDS_BACK" />
    </composite-id>

I have seen this construction, without class="BlahPK", in the documentation, in "Hibernate in Action," and elsewhere. When I try to fetch, though, I get:

我已经在文档、“Hibernate in Action”和其他地方看到了这种没有 class="BlahPK" 的构造。但是,当我尝试获取时,我得到:

Initial SessionFactory creation failed.org.hibernate.MappingException: composite-id class must implement Serializable: MyClass

This is a very simple data class, it's not mutable, I don't need a key and certainly don't want to remodel my object and define a separate public class just to read it with Hibernate. Currently, I have it kludged to use rowid as the id, but I'd rather have the mapping cleanly reflect how the table and object are used.

这是一个非常简单的数据类,它不是可变的,我不需要密钥,当然也不想重构我的对象并定义一个单独的公共类只是为了用 Hibernate 读取它。目前,我不得不使用 rowid 作为 id,但我宁愿让映射清楚地反映表和对象的使用方式。

Disclaimer: I searched StackOverflow and all I found was how to handle composite key hibernate, which just says "don't do it but you can."

免责声明:我搜索了 StackOverflow,我发现的只是 如何处理复合键 hibernate,它只是说“不要这样做,但你可以”。

采纳答案by RP-

You can define multiple @Idproperties in the entity class. like

您可以@Id在实体类中定义多个属性。喜欢

@Entity 
class User implements Serializable {
  @Id
  private String userId;

  @Id
  private String passwordsBack;
..
}

This will only be supported by Hibernate not with JPA. While you try to load this into session, you need to create an instance of User and set the id properties and the call the session.load(User.class, userinstance)

这将仅由 Hibernate 支持,而不由 JPA 支持。当您尝试将其加载到会话中时,您需要创建一个 User 实例并设置 id 属性并调用session.load(User.class, userinstance)

I found this hibernate documentation which will help understanding this. http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-identifierSection you need to look for is 2.2.3.2.2. Multiple @Id properties.

我找到了这个 hibernate 文档,这将有助于理解这一点。 http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-identifier您需要查找的部分是 2.2.3.2.2。多个@Id 属性。

EDIT: Just realized that you need the xml description and not the annotation based solution. There might be something similar to this approach. Let me see if I can get you a reference.

编辑:刚刚意识到您需要 xml 描述而不是基于注释的解决方案。可能有类似于这种方法的东西。让我看看能不能给你一个参考。

Update: If you can change your class MyClassto implement Serializable, that will fix the problem and no need to have another public PK class. You need to have the equalsand hashCodemethods which will use you id fields.

更新:如果你可以改变你的类MyClass来实现Serializable,那将解决这个问题,不需要另一个公共 PK 类。您需要使用equalshashCode方法来使用您的 id 字段。