File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes one to many unidirectional and bidirectional Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "one to many unidirectional and bidirectional " Watch "one to many unidirectional and bidirectional " New topic
Author

one to many unidirectional and bidirectional

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234



Sorry for the unsophisticated and vague question .

Please tell me when using one to many mapping when should we go for unidirectional and bidirectional relationship .

Any help will be greatly appreciated .

Thanks in advance.


Save India From Corruption - Anna Hazare.
Marco Masi
Greenhorn

Joined: Apr 18, 2009
Posts: 7
When using unidirectional or bidirectional relationship? (I hope it was the right question... )

I think it's all about your needings.

Personally i try to avoid bidirectional relationship if I don't need them so I can simplify mapping and entities.

You could have a simple Case - Pencil 1:n relationship where you need pencils in a particular case but you don't need where is a particular pencil.
But you could have a Customer - Bill 1:n relationship where you need all bills for a customer AND the customer of a particular bill.

Ok I know they were silly examples
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

You could have a simple Case - Pencil 1:n relationship where you need pencils in a particular case but you don't need where is a particular pencil.


You have explained in a very nice manner , somehow i am feeling confusion in understanding .

"where you need pencils in a particular case but you don't need where is a particular pencil."

Thanks in advance .
Marco Masi
Greenhorn

Joined: Apr 18, 2009
Posts: 7
It was a very silly example i admit

Il'll take an example from the project I'm working on:

I have one entity rapresenting user data in the system (call that User) and another entity rapresentig some options setted from the user (call them UserOption).

User and UserOption are related in a 1:n relationship. When i access a User need to read UserOptions data so i mapped a OneToMany relationship User -> UserOption. It never happens in my logic to access some UserOption and look for owning user than i didn't mapped inverse ManyToOne relationship UserOption -> User.

If you don't need inverse relationship you could not map that (so you don't need to work on configuring inverse side... I make always mistake when I write them )

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thanks Macro , I understood when to use Unidirectional and bidirectional with your help.


Steve Daskam
Greenhorn

Joined: Mar 04, 2008
Posts: 14
Can you please give a code example of this scenario? Will it work if there is a foreign key relationship?

Thanks,
Steve
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
It will only work with a foreign key of some sort, there must be a way of tracking the relationship in the db. The simplest way is to have the foreign key in the "many" side of the relationship, otherwise a join table is needed. here is an example



You will need to fill in all the other stuff needed in a good bean, but the above should give you an idea of how the relationship looks. You will also have to decide who will manage the relationship and may have to add a "mappedBy=XXX" to the non-managing side.

Btw, in the above example the UserOption table will end up with a user_id column which will be the foreign key pointing to the User and that's how the relationship will be tracked.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: one to many unidirectional and bidirectional
 
Similar Threads
EJB3 Entity Relationship
OneToMany Unidirectional relationship without Join table (JSR 220)
The "optional" element on @OneToMany and @ManyToMany
Hibernate one-to-many association
1 to many case : howto ?