简体   繁体   English

在JPA 2.0中维护关系

[英]Maintaining relationships in JPA 2.0

I've been using JPA 2.0 for a while but, sad to admit, I haven't had enough time to learn it properly. 我已经使用了JPA 2.0一段时间,但,伤心地承认,我还没有足够的时间好好学习。 It seems like I lack the basics of how to work with Entity Manager. 似乎我缺乏如何使用Entity Manager的基础知识。

Moving one step at a time, I'd like to first ask you about maintaining relationships between mapped entities. 一次移动一个步骤,我首先要问您有关维护映射实体之间关系的问题。 Of course I know how to create mappings between entities, different types of available associations ( OneToOne , etc.) and how databases work in general. 当然,我知道如何在实体,不同类型的可用关联( OneToOne等)之间创建映射,以及数据库通常如何工作。 I'm purely focused on maintaining it via Entity Manager , so please do not send me to any kind of general knowledge tutorial :-). 我只是专注于通过Entity Manager进行维护,所以请不要让我进入任何种类的常识教程:-)。

The questions are: 问题是:

  1. Am I right that as a programmer I'm responsible for maintaining (creating/updating/removing) relationships between instances of entities? 我是否正确,作为一名程序员,我负责维护(创建/更新/删除)实体实例之间的关系?
  2. Do I have to always update (set to null, remove from collection, etc.) instances by hand? 我是否必须始终手动手工更新(设置为null,从集合中删除等)实例?
  3. Plain SQL can set entities to NULL on deleting, but it seems like JPA can't do such a simple thing. 普通SQL可以在删除时将实体设置为NULL,但是JPA似乎无法做到这一点。 It also seems like a burden to do it manually. 手动执行似乎也很麻烦。 Is there a way to achieve that with JPA? JPA有办法实现吗?
  4. If I have OneToMany relationship and set to NULL the entity on the Many side of the relationship. 如果我具有OneToMany关系并将其设置为NULL,则该关系的Many端上的实体。 Then I persist the changes in a Set by saving the entity on the One side. 然后,我通过保存在实体坚持一个集变化One侧面。 Do I then have to update the entities in the Many side and set association to NULL in each instance? 然后,我是否必须更新Many端的实体,并在每个实例中将关联设置为NULL? Seems pure silliness for one-directional bindings! 似乎纯粹的傻瓜式单向绑定!

Thanks in advance! 提前致谢!

The main thing you need to investigate is the different options you have when mapping on entity. 您需要研究的主要内容是在实体上进行映射时具有的不同选项。 For example in the next piece of code the cascade all option will instruct jpa to delete the child list when the parent is deleted. 例如,在下一段代码中,层叠全部选项将指示jpa在删除父级列表时删除子级列表。

@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, mappedBy = "parent")
private Set<Child> events = new HashSet<Child>();
  1. Yes. 是。 You maintain the object tree and modify it to look like what you want. 您维护对象树并对其进行修改以使其看起来像您想要的。
  2. Yes and no. 是的,没有。 If you want the entity to reference null, then yes. 如果您希望实体引用null,则可以。 For instance, if you are removing one Entity, then you should clean up any references to it held by other entities that you are not removing. 例如,如果要删除一个实体,则应清除其他未被删除的实体对其的引用。 A practical example: its good practice to let an Employee know his/her Manager has been let go. 一个实际的例子:让员工知道他/她的经理已被放任的良好做法。 If the Employee is going to stay, it should either have its manager reference nulled out or set to a different manager, before the current manager can be removed. 如果该员工要留下,则应先删除其经理参考,或将其设置为其他经理,然后才能删除当前经理。
    If the employee is going to be removed as well, then cascade remove can cascade to all the Manager's subordinates, in which case you do not need to clean up their references to the manager - as they are going away too. 如果也要罢免员工,则级联删除可以级联到经理的所有下属,在这种情况下,您不需要清理他们对经理的引用-因为他们也要删除。
  3. I don't quite understand what SQL is setting to null. 我不太明白什么SQL设置为null。 Deleting removes the row in the database, so there isn't anything to set to null. 删除将删除数据库中的行,因此没有任何要设置为null的内容。 Cleaning up a reference shouldn't be that difficult in the object model, as JPA has a number of events to help such as preremove preupdate etc. In the end though, the problem is with your java objects. 在对象模型中清理引用并不是那么困难,因为JPA有许多事件可以帮助您进行处理,例如preremove preupdate等。最后,问题出在Java对象上。 They are just java objects, so if you want something done, your application will need to do it for the most part. 它们只是java对象,因此,如果您想完成某件事,那么您的应用程序将需要在大多数情况下完成它。 JPA handles building them and pushing them to the database, not changing the state for you. JPA处理构建它们并将它们推送到数据库,而不是为您更改状态。
  4. Yes and no. 是的,没有。 If you set up a bidirectional relationship, you must maintain both sides as mentioned above. 如果建立双向关系,则必须如上所述保持双方。 If you set the child's parent reference to null, you should let the parent know it no longer has a child, wouldn't you? 如果将孩子的父母引用设置为null,则应该让父母知道它不再有孩子了,不是吗? Your parent will continue to reference its child for as long as that Parent instance exists. 只要该父级实例存在,您的父级就会继续引用其子级。 So even though the database is updated/controlled through the side that owns a relationship, the object model will be out of synch with the database until it is refreshed or somehow reloaded. 因此,即使通过拥有关系的一方来更新/控制数据库,对象模型也将与数据库不同步,直到刷新或以某种方式重新加载为止。 JPA allows for multiple levels of caching, so it all depends on your provider setup how long that Parent instance will exist referencing a Child that no longer exists in the database. JPA允许多级缓存,因此这完全取决于您的提供程序设置,该父实例将引用数据库中不再存在的子代存在多长时间。

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

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