开发者

How to do a cascade save in hibernate

开发者 https://www.devze.com 2023-03-21 22:29 出处:网络
I have objects A and B. 开发者_如何学运维Object A is like class A{ Set<B> } Now when I save A I want that all objects in Set<B> of A should be automatically saved in DB. How can I do

I have objects A and B.

开发者_如何学运维

Object A is like

class A{
 Set<B>
}

Now when I save A I want that all objects in Set<B> of A should be automatically saved in 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. But if you are using Hibernate directly, then you need to use its own annotation for cascading.

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

...

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

精彩评论

暂无评论...
验证码 换一张
取 消