I am new to Hibernate and ORM in general.
I’ve got a Product entity which holds properties such as description, price, barcode and other product generics.
Now I want to manage other product specific data, e.g. I can imagine a Sunglasses entity with information about lenses, size …. and a Watch entity with other details related to shape, battery duration and so on.
Which is the better strategy? In a “non-ORM” world I would have had:
-
two tables: watches and sunglasses, with duplicated fields
-
one table: “products” with ALL properties from watches and glasses and a field “kindOfProduct”
How can I model that on Hibernate/JPA?
You mainly have three inheritance strategies to choose from. The two you mentioned in the question, plus a mixed one, which stores the common attributes into a base table, and the specific properties into specialized tables.
But with only this information, it’s a bit hard to give an advice, as there are rules and constraints on what you can accomplish with each of the strategies. I’d suggest to start by reading the Hibernate documentation on this topic and experimenting by yourself:
http://docs.jboss.org/hibernate/stable/orm/manual/en-US/html_single/#d0e7071