-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Add a Record in Parent Table and Update a Record in Child
PostPosted: Thu Jun 25, 2009 11:55 am 
Newbie

Joined: Thu Nov 16, 2006 3:17 am
Posts: 2
Hi I am writing a program to where there is a Parent Child Relation between two Tables.
Table A is Parent and Table B is child.
Table B(Child) always will be updated, even if there is ADD / UPDATE On table A.
I am persisting the Two Entities Master (Table A) and Child (Table B).
The Object (POJO) of A contains a List of objects of Table B. Cause it is One to Many Mapping.
Annotations used:
@OneToMany(mappedBy = "A_PrimaryKey", cascade = CascadeType.MERGE)

the persist gives me error of DEtach Entity....
Can any one give any more ideas onthis.

SQL Logs show: Insert on A and Update on B (Child)


Top
 Profile  
 
 Post subject: Re: Add a Record in Parent Table and Update a Record in Child
PostPosted: Thu Jun 25, 2009 7:14 pm 
Newbie

Joined: Thu Jun 25, 2009 3:45 am
Posts: 4
I supposed the following

Code:
@Entity
private class Parent {
   
    @Id
    @GeneratedValue(GenerationType.AUTO)
    @Column(name="PARENT_ID")
    private Integer id;
   
    // Notice mappedBy attribute's value refers Parent property OR field in Child class
    @OneToMany(
       mappedBy="parent",
       cascade={
           CascadeType.PERSIST,
           CascadeType.MERGE
       })
    @JoinColumn(name="PARENT_ID", nullable=false)
    private List<Child> childList = new ArrayList<Child>();

    public void addChild(Child child) {
        childList.add(child);

        // It is important because mappedBy="parent"
        child.setParent(this);
    }

}

@Entity
public class Child {

    @Id
    @GeneratedValue(GenerationType.AUTO)
    @Column(name="CHILD_ID")
    private Integer id;   

    @ManyToOne
    @JoinColumn(name="PARENT_ID", nullable=false)
    private Parent parent;

}


So you should use the following

Persist

Code:
Parent parent = new Parent();

parent.addChild(new Child());
parent.addChild(new Child());

session.save(parent); // Both parent and childs will be saved


Update

Code:
Parent parent = new Parent();

Child updatableChild; // Notice child id should be set up

parent.addChild(updatableChild);

session.save(parent); // Parent will be saved and Child will be updatable


I hope it helps you
Regards,
Arthur Ronald F D Garcia (Java programmer)
Natal/RN (Brazil)


Top
 Profile  
 
 Post subject: Re: Add a Record in Parent Table and Update a Record in Child
PostPosted: Wed Jul 01, 2009 5:04 am 
Newbie

Joined: Thu Nov 16, 2006 3:17 am
Posts: 2
Hi alpha88 /Arthur
Thanks for the Reply. Will try to follow that logic.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.