简体   繁体   English

在休眠状态下处理一对多关系

[英]working with one-to-many relationship in hibernate

I'm still learning what hibernate can do and this time i'm trying something that seems not to be working. 我仍在学习冬眠能做什么,这一次我正在尝试一些似乎不起作用的东西。 i have 2 tables users and contacts.as you can guess contacts hold the relationship by have user_id as foreign key. 我有2个表的用户和contacts.as,您可以猜测是否有user_id作为外键来保持关系。 here are snippet of the 2 mapping files. 这是2个映射文件的片段。 this first is from users.hbm.xml 首先是来自users.hbm.xml

<set name="contactsdetails">
  <key column="userId"/>
  <one-to-many class="Domain.Contacts"/>
</set>

and this one is from contacts.hbm.xml 这是来自contacts.hbm.xml

<many-to-one class="Domain.Users" name="userId"/>

while every thing is working i mean at configuration,mapping file side and inserting into users table from UsersDAO, i'll like to insert into users and contacts at the same. 当一切正常时,我的意思是在配置,映射文件侧并从UsersDAO插入到用户表中,我想同时插入到用户和联系人中。 Meaning i create my users object and assigning values to various properties, create one or array of contacts and assinging various properties to it to and finally add it to the contactdetails set property of users before i save the users objects.when i tried this i realised that it's kinda weird because the contact userId property is of the type users so i'll add user to contacts or each contacts object and then add the same contacts object to the contactsdetails property of users before i persist users objects.i'm sure i'm missing something and i'm having nullpointerexcpetion when i tried.Can you please show me how to do it? 意思是我创建了用户对象并为各种属性分配值,创建了一个或一组联系人并将其赋予各种属性,最后在保存用户对象之前将其添加到用户的contactdetails设置属性中。当我尝试这样做时,我意识到这有点奇怪,因为contact userId属性是用户类型的,因此我将用户添加到联系人或每个联系人对象中,然后在我坚持用户对象之前将相同的联系人对象添加到用户的contactdetails属性中。丢失了某些内容,尝试时出现了nullpointerexcpetion。请您告诉我该怎么做? thanks for reading 谢谢阅读

When you have entity A pointing at entity B, and entity B pointing at entity A, you have what hibernate calls a bi-directional mapping. 当具有指向实体B的实体A和指向实体A的实体B时,您具有休眠称为双向映射的功能。 These can be tricky, and you have to be careful, and also tell hibernate that one side "owns" the relationship. 这些可能很棘手,您必须小心,并告诉冬眠一方“拥有”这种关系。

See the Bidirectional associations section of the hibernate docs on how to manage these associations. 有关如何管理这些关联的信息,请参见休眠文档的“双向关联”部分

You need to tell Hibernate which side "owns" the relationship. 您需要告诉Hibernate哪一方“拥有”该关系。 Normally I find the many-to-one side is simplest. 通常,我发现多对一方面最简单。 To do this add inverse="true" to one side of the mapping. 为此,将inverse =“ true”添加到映射的一侧。

<set name="contactsdetails" <!---->inverse="true"<!---->>
  <key column="userId"/>
  <one-to-many class="Domain.Contacts"/>
</set>

From the documentation: 从文档中:

Changes made only to the inverse end of the association are not persisted. 仅对关联的反向端所做的更改不会保留。 This means that Hibernate has two representations in memory for every bidirectional association: one link from A to B and another link from B to A. This is easier to understand if you think about the Java object model and how a many-to-many relationship in Javais created: 这意味着对于每个双向关联,Hibernate在内存中都有两种表示形式:一个从A到B的链接,另一个从B到A的链接。如果考虑Java对象模型以及多对多关系,这将更容易理解。在Java中创建:

category.getItems().add(item);          // The category now "knows" about the relationship
item.getCategories().add(category);     // The item now "knows" about the relationship

session.persist(item);                   // The relationship won't be saved!
session.persist(category);               // The relationship will be saved

The non-inverse side is used to save the in-memory representation to the database. 非反面用于将内存中的表示形式保存到数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM