[英]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: 问题是:
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>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.