views:

36

answers:

2

I am planing to create an application relying on DB using Hibernate. I will have some similar classes like teacher and student and so on. In DB they will have some fields with similar names. So I wonder If I can create a class Human with annotations for standard fields like Name, SName and so on so to just extend that class in teacher student and so on. Will it work? Does any one use it in such way?

+1  A: 

Hibernate has extensive support for different scenarios involving inheritance and polymorphism. See the documentation

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

The short answer is, yes you can do exactly what you want -- create a base class with fields common to subclasses. How hard/involved it is depends on how you structure the tables.

hvgotcodes
+2  A: 

I would suggest that teacher and student are not subclasses of human, but rather are roles that a human can play. If you make them subclasses then you are effectively saying that teacher can never be a student and vice versa. Also, if one goes from being a student to teacher (or vice versa) then you lose any associations and history for that object. Consider roles instead. Consider composition and delegation instead of inheritance in this example. Take a look at Peter Coad's book: Java Design for more on this.

Also, you do want to think about the table implementation if you do decide to use inheritance: single table (with null cols for the subtype attribs) separate table or single table for super class and separate tables for subclasses.

Joe
Joining tables would be a real problem for me - I want to use Google app engine...
Kabumbus