Here is a real example taken from code that is working:

I have a User class that has a one to many association with a
Profile(address+email).  In the User class I have the following code:

        @OneToMany (mappedBy="user")
        public Set<ProfileBean> getProfiles(){
                return profiles;
        }
        
        @Transient
        public void addProfile(ProfileBean vp){
                vp.setUser(this);
                this.getProfiles().add(vp);
        }
        @Transient
        public void removeProfile(ProfileBean vp){
                this.getProfiles().remove(vp);
        }

        public void setProfiles(Set<ProfileBean> profiles) {
                this.profiles = profiles;
        }

In the Profile class I have the following code:

        @ManyToOne
        @JoinColumn (name="user_id")
        public UserBean getUser(){
                return this.user;
        }

        public void setUser(UserBean user){
                this.user = user;
        }

To manage the relationship: (for Generic Dao code refer sources for
appfuse-data-common-[version].jar...)

add Profile:
        ...
        UserBean vu = userDao.findById(uid);
        ProfileBean vp = new ProfileBean();
                ///vp.setXxxx(value); etc etc
        vu.addProfile(vp);
        vp = profileDao.saveProfile(vp);
        ...

to delete a Profile:
        ...
        ProfileBean vp = profileDao.findById(pid); 
        UserBean vu = vp.getUser();
        vu.removeProfile(vp); //orphans profile
        profileDao.deleteProfile(vp); //deletes the orphan
        ...

Hope that is enough to get you started on the right path. Another reference
you might find useful is

http://www.hibernate.org/hib_docs/v3/reference/en/html/example-parentchild.html

Although examples use xml mapping rather than annotations but logic still
follows.

Luke.


Luke McLean wrote:
> 
> .... I will find some more detailed code and post a complete tomorrow.
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Basic-One-To-Many-tf3764612s2369.html#a10775281
Sent from the AppFuse - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to