Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

discriminator in hibernate

 
Vijay K Vivek
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning Hibernate and am a newbie to the same. I do not understand the purpose served by the discriminator attribute of the class mapping. I tried reading a couple of articles on the same but still I do not understand where it will be required to use the same. Please throw some light on the same.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's difficult to understand? The name itself implies that it is used to discriminate between Types and SuperTypes in an inheritance heirarchy.
 
Vijay K Vivek
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
Why do we need to discriminate between the types and super types in a object relational mapping?

Also the hibernate documentation mentions that the valid values are 'null' and 'not null'. What will I do with these valid values? How can they discriminate?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use a discriminator to flag to Hibernate which type of object a particular row in your table represents. It is there to support one table being used to represent an entire class hierachy (the table per class hierachy pattern).

You don't need to use it. My opinion is it is a bit of an anti-pattern, because it weakens your data model for the sake of your object model. But all object relational mapping is a bit of a trade off so others may disagree.


Also the hibernate documentation mentions that the valid values are 'null' and 'not null'. What will I do with these valid values?

These are not valid attributes for discriminator. Where did you come accross these?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a quick example of a discriminator

You have a table called phone_numbers, but home, office, and cell phone numbers all could go in that one table

So phone_numbers table is

id
phone_number
phone_type

some values



In Java you have a class heirachy of

You have a Phone abstract class with two attributes id and phoneNumber

You have three subclasses
Home, Cell, and Office that subclass Phone

Now how does Hibernate know which type of subclass to create, well it uses the discriminator column "phone_type"

Hope that helps

Mark
 
Vijay K Vivek
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark

Thanks a lot. The explanation was excellent and I understood the purpose with this. I guess I was missing on the part "how did Hibernate know" before. Now it is clear.
 
Shahnawaz Siddiqui
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Lot ,for explanation of discriminator purpose,Its really helpful
 
Anandha Loganathan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. Nice explanation from Mark Spritzler for discriminator.
 
Jason Pyeron
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Also the hibernate documentation mentions that the valid values are 'null' and 'not null'. What will I do with these valid values?

These are not valid attributes for discriminator. Where did you come accross these?



Section 5.1.3:

http://docs.jboss.org/hibernate/stable/core/reference/en/html/mapping.html

discriminator-value (optional - defaults to the class name): a value that distinguishes individual subclasses that is used for polymorphic behavior. Acceptable values include null and not null.
 
Ajit Sahoo
Greenhorn
Posts: 6
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic