简体   繁体   English

如何在 hibernate 中进行级联保存

[英]How to do a cascade save in hibernate

I have objects A and B.我有对象 A 和 B。

Object A is like Object A 就像

class A{
 Set<B>
}

Now when I save AI want that all objects in Set<B> of A should be automatically saved in DB.现在,当我保存 AI 时,希望 A 的Set<B>中的所有对象都应自动保存在 DB 中。 How can I do it?我该怎么做?

// Use the cascade option in your annotation
@OneToMany(cascade = {CascadeType.ALL}, orphanRemoval = true)
public List<FieldEntity> getB() {
    return fields;
}

The question from Ransom will work if you are working through EntityManager and using pure JPA annotations.如果您通过 EntityManager 工作并使用纯 JPA 注释,来自 Ransom 的问题将起作用。 But if you are using Hibernate directly, then you need to use its own annotation for cascading.但是如果你是直接使用Hibernate,那么你需要使用自己的注解进行级联。

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

...

@OneToMany
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
public List<FieldEntity> getB() {
    return fields;
}

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

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