This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

one to many unidirectional and bidirectional

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Marco Masi
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Macro , I understood when to use Unidirectional and bidirectional with your help.


 
Steve Daskam
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • 0
  • Mark post as helpful
  • send pies
  • 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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic