I've been using JPA 2.0 for a while but, sad to admit, I haven't had enough time to learn it properly. It seems like I lack the basics of how to work with 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. I'm purely focused on maintaining it via Entity Manager
, so please do not send me to any kind of general knowledge tutorial :-).
The questions are:
OneToMany
relationship and set to NULL the entity on the Many
side of the relationship. Then I persist the changes in a Set by saving the entity on the One
side. Do I then have to update the entities in the Many
side and set association to NULL in each instance? 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.
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, mappedBy = "parent")
private Set<Child> events = new HashSet<Child>();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.