• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

one to many unidirectional and bidirectional

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Macro , I understood when to use Unidirectional and bidirectional with your help.


 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please give a code example of this scenario? Will it work if there is a foreign key relationship?

Thanks,
Steve
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic