Thursday, October 29, 2009

EJB entity relations onetomany

Using @OneToMany and @ManyToOne entity relationship might sound little difficult and confusing in the beginning but actually it is very easy. I recently used it in one of my projects and it worked great.

Here is how the code in your entities should look like...

Assuming table 1 and table 2 have one to many relationship...then...

in the table 1 entity...you have...

...
    @OneToMany(mappedBy="table2mapping")
    private Collection table2list;
   ..associated getter and setter...


in the table2 entity..you have...
@JoinColumn(name = "table2ID", referencedColumnName = "table1ID", insertable=false, updatable=false)
    @ManyToOne
    private table1 table2mapping;

@JoinColumn should be changed depending upon your requirements...
Let me know if you have any questions...

0 comments:

Post a Comment

About This Blog

In this blog you will find simple but great ideas on various topics ranging from food recipes to investment strategies. I hope the ideas presented in this blog will help you all in some way or other. Also included are the recent and hot news around the world...

Go ahead and grasp my ideas...

Disclaimer and Terms of Use

All the ideas and opinions provided herein are intended to share and inform simple and useful information. They come with no warranty of any kind. If you should choose to interpret graspmyideas.blogspot.com information as perfect working ideas, or advice or if you want to completely rely on these ideas, you do so at your own risk.

This site is offered as is and without warranties of any kind. We are not responsible for timeliness, accuracy, unavailability or interruptions in availability, viruses or other defects in the site or its contents. In no event shall graspmyideas.blogspot.com site or its authors are responsible for any damages to users or their computer systems.


Your Ad Here

  © Blogger template AutumnFall by Ourblogtemplates.com 2008

Back to TOP