java Hibernate:更新一对多关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10896883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:01:02  来源:igfitidea点击:

Hibernate: Updating OneToMany relationship

javahibernate

提问by seesee

I have a parent with a list of child mapped.

我有一个父级,其中包含映射的子级列表。

May I know how do I update the list of child when I update the parent

我可以知道如何在更新父级时更新子级列表吗

Example:

例子:

A parent P1 have child A B C.

父母 P1 有孩子 AB C。

I decided to delete B and add D

我决定删B加D

so parent P1 have child A C D in the database.

所以父 P1 在数据库中有子 ACD。

currently I merely delete all child that belongs to parent P1 and repopulate chid(A C D) again... I believe there is a more simple way of doing so.

目前我只是删除属于父 P1 的所有子项并再次重新填充 chid(ACD) ......我相信有一种更简单的方法。

I know this task is quite sensitive, please be kind enough to let me know what I need to set to ensure the child is inserted and deleted properly.

我知道这个任务非常敏感,请让我知道我需要设置什么以确保正确插入和删除子项。

New Question:

新问题:

1) Is there a way to lazy update/delete a relationship? I have a list that is FetchType.Lazy, when I am updating the data, I might not wish to load the relationship or update it.

1)有没有办法延迟更新/删除关系?我有一个 FetchType.Lazy 列表,当我更新数据时,我可能不希望加载关系或更新它。

2) If a list have item A B C, I removed item C and do a dto.save(). Will item C be deleted?

2) 如果列表有项目 ABC,我删除项目 C 并执行 dto.save()。C项会被删除吗?

回答by Pramod Kumar

Here is the mapping to update child using parent -

这是使用父级更新子级的映射 -

@Entity
@Table(name = "COMPANY")
@SequenceGenerator(name="CompanySeq", sequenceName="COMPANYseq", allocationSize=1)
public class Company implements Serializable {

    private static final long serialVersionUID = 1L;

    /*
     * Customer Company Details 
     */

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CompanySeq")
    @Column(name = "COMPANY_ID")
    private Integer id;

    @Column(name="COMPANY_NAME")
    private String name;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "COMPANY_ID",nullable=false)
    @OrderBy(clause = "PRODUCT_NAME" )
    @ForeignKey(name = "fk_company_product")     
    private List<Product> products = new ArrayList<Product>();    

 }



 @Entity
@Table(name = "PRODUCT")
@SequenceGenerator(name="CompanyProductSeq", sequenceName="COMPANY_PRODUCT",  allocationSize=1)
public class Product implements Serializable{

    /**
     * SerialVersion ID
     */
    private static final long serialVersionUID = 4073418246832063389L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CompanyProductSeq")
    @Column(name = "PRODUCT_ID")
    private Integer id;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "COMPANY_ID", updatable = false, insertable = false, nullable=false)  
    private Company companyId;

        @Column(name = "PRODUCT_NAME")
    private String name;

    }

回答by manurajhada

1+ for Promod as he provided very good code to understand it. The functionality you want is all about Hibernate Cascade. By the use of cascade you just need to update parent bean object and the list of its child objects and Cascade will do the rest.

Promod 1+,因为他提供了很好的代码来理解它。您想要的功能都是关于Hibernate Cascade. 通过使用级联,您只需要更新父 bean 对象及其子对象列表,级联将完成剩下的工作。

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "COMPANY_ID",nullable=false)
@OrderBy(clause = "PRODUCT_NAME" )
@ForeignKey(name = "fk_company_product")     
private List<Product> products = new ArrayList<Product>();    

In upper example given by Pramod, Company is keeping all data about Products as it's children. Here

在 Pramod 给出的上例中,公司将有关产品的所有数据作为其子项保留。这里

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)

cascadeType.All will perform all parental operation on child too i.e. Insertion/Updation on Company will perform the same on Product, If you want to update Products of a Company, You just need to fetch the loaded Company bean with products (FetchType.EAGER), modify or add new products in the bean and saveUpdate the Company. It will save/update Company and its Products too.

cascadeType.All 也会对孩子执行所有父操作,即公司上的插入/更新将对产品执行相同的操作,如果您想更新公司的产品,您只需要使用产品(FetchType.EAGER)获取加载的公司 bean 、修改或在bean 中添加新产品并保存更新公司。它也会保存/更新公司及其产品。