0

I wish to implement an online library in hibernate. Here is my sql sschema:

sql schema

Between Book and Programming book I am thinking of using hibernate inheritance of type table per subclass joined. I designed the sql schema this way because I don't see the point of having the same columns duplicate in 10 tables. Maybe I need to add one more common column, I don't want to do it in 10 tables. But this means that whenever a Book is needed somewhere, a huge join would be made between all the subtables ( I have only 2, but I could have 50 book types!!! ). The same stuff applies if for example I would have had an online shop: I would have had Product, Tv, Laptop, Phone, etc...).

What design can help me avoid this behaviour?

My thoughts are:

  • I could duplicate all the columns in all the subtables, and thus, don't use inheritance at all, treat them like different entities. The only thing here is that I will have to tie every specific book table of other tables ( authors, etc )
  • I could avoid using inheritance, keep the current sql schema but use unidirectional association ( a ProgrammingBook has a Book, etc )

What option do you guys recommend?

Kind regards,

adragomir
  • 457
  • 4
  • 16
  • 33
  • "The only thing here is that I will have to tie every specific book table of other tables ( authors, etc )" Can you detail please ? :) – davidxxx Nov 16 '16 at 12:48
  • As you can see right now, a ProgrammingBook relation to authors is through Book. If I would duplicate the columns in each table, then ProgrammingBook should be tied to authors, etc, then PsychologyBook need to have relations with authors, etc, and the same for each book – adragomir Nov 16 '16 at 13:11
  • It was what I though but I would be sure. Thank you – davidxxx Nov 16 '16 at 13:35

1 Answers1

0

From experience, when using inheritance with Hibernate, you should prefer the Table per Hierarchy point of view, with a discriminator.

Here's a thread that could help you pick an answer : How can you represent inheritance in a database?

Community
  • 1
  • 1
DamCx
  • 1,047
  • 1
  • 11
  • 25
  • I agree with you, from performance point of view, but what would mean to put all products in the same table and de-normalizing the sql schema. – adragomir Nov 16 '16 at 13:12
  • It wouldn't be a very expensive operation, as you will be able to quickly get your data – DamCx Nov 16 '16 at 15:41