• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

0 x N in hibernate (null values in a FK field)

 
Rodrigo Bossini
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Let's consider the following example:

One Client may have from 1 to N Cars.

One car may belong to 0 or 1 Client.

It's then a 1 x N relationship, and the Car table will have the ClientID field as an FK.

Since it is an optional relationship, it's totally acceptable that the FK in the Car table will accept null values, which will indicate that the car does not belong to any Client yet.

Here's my question:

How does Hibernate implement this in practice?

Does it in fact create the car table with a FK field that accepts null values?

Or does it create another table just to store the relationships?

Thank you.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Normally you'd create the tables, unless you're having Hibernate generate the schema.)

If a person can have multiple cars I don't see how a single column could represent it.

Wouldn't it be pretty straight-forward to try it?
 
Christian Dillinger
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Java Persistence with Hibernate" from Bauer/King suggest to build a many-to-many with a unique "one"-side and an optinal "many"-side.

In your example at the user's side:


The car's side:


 
Rodrigo Bossini
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:(Normally you'd create the tables, unless you're having Hibernate generate the schema.)

If a person can have multiple cars I don't see how a single column could represent it.



A person can have multiple cars and one car belong to 0 or 1 person. One single column (personID as FK in car table) is capable of representing such a relationship.

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had assumed you'd want a car table representing the same model of car rather than duplicating a bunch of information.

If that's the case, why are you asking the question? Are you asking how Hibernate would create the DDL?
 
Rodrigo Bossini
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:I had assumed you'd want a car table representing the same model of car rather than duplicating a bunch of information.

If that's the case, why are you asking the question? Are you asking how Hibernate would create the DDL?


I don't understand why a bunch of information would be duplicated in this case.

I'm asking the question because I wanted to know if having null values in a FK field is considered "bad practice". So I asked it involving hibernate because I was assuming that, given hibernate's importance these days, it would do this as best as possible. I haven't yet tried hibernate to see how it works. Yes, please, if possible and easy for you, please send me the DDL generated by hibernate in this case.

Thank you.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can have a car in the car table that doesn't have an associated owner, then it's fine (probably mandatory) to have a null FK. A null indicates there's no relationship available/defined.

As far as the duplicated information, your problem statement was vague, so I made an assumption, which you're now saying was incorrect. I assumed "car make, model" etc., whereas you meant "specific car".

If you're asking questions about how Hibernate does something, I recommend you get Hibernate and actually use it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic