The Problem
Suppose we have class User which is the super class for all kind of Users, and we have these subclasses that inherit from User class:
ü EmployeeUser
ü StudentUser
ü TeacherUser
ü MerchantUser
ü VoucherUser
The User class is NOT abstract class, and many methods are implemented in it… keep in mind J
How to present this in Hibernate?
All of them will be presented in User.hbm.xml like this:
<class name="...User">
<id name="id"/>
<property ...
... All the properties in User class
<joined-subclass name="EmployeeUser">
<key column="ID" />
<property ...
... All the properties in EmployeeUser class
joined-subclass>
...All the other subclasses
Simple!!
After running the application, Hibernate will create the database schema... Nothing new Sami!!?? I know J
Let’s say now you want to implement the Login action for example, userName and password is in User class
What you will do is something like this:
//Code to check username and password that user entered in the login screen
Session session =
getSession();
Integer count = (Integer) session.createQuery("select count(*) from User where username = :NAME and password = :PASSWORD").setString("NAME", userName).setString("PASSWORD", password).uniqueResult();
Don’t tell me Sami that the login action is just 6 lines, impossible L
After doing this you will go to your team leader or Manager and tell him that you have done the project and you are ready to be team leader… Just Joking J
But unfortunately, the code you have written is a disaster… Yes, and you are fired out your company L
Look at the Hibernate generated query, you will see a very big query that you haven’t ever seen in your life, and it will be like this:
select count (*) from USER
inner join EMPLOYEE ON USER.ID= EMPLOYEE.ID-----------------1
inner join STUDENT ON USER.ID= STUDENT.ID-------------------2
inner join TEACHER ON USER.ID= TEACHER.ID-------------------3
inner join MERCHANT ON USER.ID= MERCHANT.ID-----------------4
inner join VOUCHER ON USER.ID= VOUCHER.ID-------------------5
Five inner joins for checking username and password that stored in the USER table... What you were thinking about when you wrote this?
This is a big problem, because if three users for example login at the same time, a very huge database load will fly!!
Solution
Using <discriminator> Hibernate tag
You can tell Hibernate that when you want a data for any subclass stored in the superclass, don’t “YA” Hibernate make any extra join that I don’t need, in other words: get the subclass from the superclass.
You will not change anything in your java code; all the changes will be in User.hbm.xml
Change hibernate configuration file to something like this after we take the User.hbm.xml for our example:
<class name="...User">
<id name="id"/>
<property ...
... All the properties in User class
<discriminator column="USER_TYPE" type="string" length="15" not-null="false" force="true"/>
<subclass name="EmployeeUser" discriminator-value="EMPLOYEE">
<join table="EMPLOYEE_USER" fetch="select">
<key column="ID" />
<property ...
... All the properties in EmployeeUser class
join>
subclass>
<subclass name="StudentUser" discriminator-value="STUDENT">
<join table="STUDENT_USER" fetch="select">
<key column="ID" />
<property ...
... All the properties in StudentUser class
join>
subclass>
...All the other subclasses and for each one of them don’t forget the discriminator-value
After doing this you have to restart the project and let Hibernate to alter the database to reflect the changes you made, and after that you and after running your big project (Login screen project) look at the hibernate sql generated query, it will be like this:
select count (*) from USER
where USER.USER_TYPE in (‘STUDENT’, ‘EMPLOYEE’, ‘TEACHER’, ‘MERCHANT’, ‘VOUCHER’) These are the discriminator-value for each subclass
No inner join at all J
Conclusion
One of the best practices in Hibernate says that:
When you have to implement inheritance tree in Hibernate, what is the kind of the superclass? What is the behavior of this superclass and its subclasses?
If the superclass is not abstract class, and many methods for subclasses are implemented in the superclass, and the superclass tasks are more than it subclasses tasks, then you have to use <discriminator> to get the subclasses from the superclass and decrease the database load.