JPA以编程方式定义级联选项 [英] JPA programmatically define cascading options

查看:85
本文介绍了JPA以编程方式定义级联选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置。

  @Table(name =CV,schema =recruiter)
public class CV extends AuditedEntity {
....
@OneToMany(cascade = {CascadeType.ALL})
private Set< Experience>经验;

@OneToMany(cascade = {CascadeType.ALL})
private Set< Education>的教育;

@OneToMany(cascade = {CascadeType.ALL})
private Set< Certification>认证;
....
}

所以你可以看到我只想要所有操作将级联。在选择表格中的所有CV之后,我将集合传递到将实体序列化为JSON的上层。用户将根据自己的情况修改实体值和子元素的值。

当实体返回时,它将从JSON反序列化并重新合并到会话中:

p>

  // cv是[{id:1,creationDate:1368230400000,creationUser...的反序列化与所有儿童实体(教育,经验和认证)
em.merge(cv);

现在,通常JPA(Hibernate)将只保留用户已更改的内容,所以我们没事在这一点上。但是如果最初序列化的CV不包含任何子属性会发生什么?如果这仅仅是这个JPA查询的结果:

  em.em.createNamedQuery(从CV中选择cv); //没有渴望加载没有什么。 

在这种情况下,CV的所有孩子(经历,教育,认证)都不会被填充。在执行merge()操作时,所有的子关系都将被删除。

这么长的故事,我可以在某些情况下级联(当我有整个图修改的对象),而不是在其他人中级联(当我刚刚有一个新的反序列化的简历列表,没有孩子)?

我不知道要这样做。如果你将一个应该有孩子的对象合并到存储器中,但这个对象没有这些子元素,那么它们将被删除。

你可以编写一个特定的 UPDATE 语句来匹配这种情况。根据你有多少值,你也可以从数据库中读取对象,并调用所有非子设置器。


I have the following setup.

@Table(name = "CV", schema = "recruiter")
public class CV extends AuditedEntity {
    ....
@OneToMany(cascade = { CascadeType.ALL})
private Set<Experience> experiences;

@OneToMany(cascade = { CascadeType.ALL})
private Set<Education> educations;

@OneToMany(cascade = { CascadeType.ALL})
private Set<Certification> certifications;
 ....
}

So as you can see I only want ALL operations to be cascaded. After selecting all CVs in the table, I pass the collection to the upper layers which serializes the entity into JSON. The user will modify the entity values and children values as he sees fit.

When the entity comes back it will be deserialized from JSON and merged back into the session:

// cv is the deserialization of [{"id":1,"creationDate":1368230400000,"creationUser" ... along with all child entities ( educations, experiences and certifications )
em.merge(cv); 

Now, normally JPA(Hibernate) will only persist what the user has changed, so we're fine on this point . But what happens if the initially serialized CV does not contain any child properties? What if it is just the result of this JPA query:

em.em.createNamedQuery("Select cv from CV"); // no eager loading no nothing. 

In this case all children of the CV ( experiences, educations, certifications) will not be populated. When doing the merge() operations, all the child relations will be deleted.

So long story short, can I cascade in some cases (when I have the entire graph of modified objects) and NOT cascade in others ( when I just have a plain list of freshly deserialized CVs with no children) ?

解决方案

There is no way that I know of to do that. If you merge an Object that should have children into the storage but the Object does not have the children, then they will be deleted.

You could write a specific UPDATE Statement to match that case. Depending on how many values you have you can also read the object from the DB and call all non-children setter.

这篇关于JPA以编程方式定义级联选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆