0

I thought it was a good idea to use single table inheritance for my use case - the ability to store different content types in the one table. In my case storing different article types (link, text, image).

  1. text (the base class)
    • articleId
    • text
  2. link (extends of text)
    • super(articleId, text)
    • link
  3. image (extends of text)
    • super(articleId, text)
    • image
    • imageDescription

I'm in the process of making a feed of different articles (image, text, and link), and came across the problem that I cannot select * from text and have the super classes included? Is there a way around this? Have I chosen the wrong database schema setup?

It'd be ideal if I could select all from text and have the link & image objects included.

Another problem I'm facing is whether I should use the one controller in spring for my text, link, and image classes? At the moment I've got 3 different controllers, and 3 different services for each of the extended entities?!

James111
  • 15,378
  • 15
  • 78
  • 121

1 Answers1

0

Is text both a member and also an entity itself? I wouldn't recommend that. But I try to focus your controllers on the request and response interactions. You don't necessarily need a controller per an entity. I did this and I found it made my app revolve around the entities rather than the processes the user was trying to do. Definitely a Dao and Service per an entity, but a controller is about the user interacting with the app in my opinion.

I'm not sure about your question with querying the base table in hibernate though. I know you can do something like this in JPA with treat() JPA Criteria Query over an entity hierarchy using single table inheritance. But I wouldn't do that. Rather just have this functionality in the Text Service layer, which would have access to all the Daos needed. Ofcourse if you had hundreds of classes extending Text I would look for a solution of the type you are seeking, but seeing as you only have 3 I think that solution would be fine, you'll only have 3 daos there.

Doing this would also mean you won't need to worry about moving away from the single table policy.

So in the service layer it would just be:

List<String> texts = textDao.getAllText();
texts.addAll(linkDao.getAllText());
texts.addAll(imgDao.getAllText());
return texts;

Probably the easiest.

Community
  • 1
  • 1
Derrops
  • 7,651
  • 5
  • 30
  • 60