I have been diving into hibernate lately, and am enjoying it, but am wondering if anyone has come across this particular use case. If you have lots of classes like Cat, Dog, Parakeet, etc. and you have them organized with abstract classes and interfaces, is it considered bad form to create an object such as
Then in your DogDAO you would build an AnimalEntity and save it. I think this could simplify the data layer, and add for more search fluidity.
This seems simpler to me, but I have not come across anyone recommending that particular design. Is it widely used?
Yes , have a seperate entity for each object you are saving. I would have PersonPOJO map to a PersonEntity, and save that enity in a personRepository which would have my hibernate or JPA functionality.
Also think of caching, if you are going to cashe in hibernate second level. It would be as an entity.
Hibernate JPA, and for that matter, JPA in general, work quite well with Java inheritance. You could have a base class for animals, and separate subclasses for cats, dogs, and so forth. In such a case, each subclass would have a corresponding database table.
Alternatively, you might have a common database table and create database Views and map the CAT view to the Cat entity type, the DOG view to the Dog entity type, and so forth.
There are all sorts of fun games you can play that way.
An IDE is no substitute for an Intelligent Developer.
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database