seesee
seesee

Reputation: 1155

Hibernate: Updating OneToMany relationship

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.

I decided to delete B and add D

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

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.

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.

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

Upvotes: 2

Views: 17487

Answers (2)

manurajhada
manurajhada

Reputation: 5390

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.

@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

@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.

Upvotes: 2

Pramod Kumar
Pramod Kumar

Reputation: 8014

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;

    }

Upvotes: 3

Related Questions