Hibernate Entity dependant on column value
Is it possible with Hibernate to have a single table in the database, which stores multiple types of entities, differentiated开发者_如何学JAVA by the value of some column?
I've got an application that uses lists of data, the structure of all the data is the same - id, description, sequence. Currently I have one table per list, which means many tables that are all structurally the same.
What I want to do is to add one more piece of data to the list data - entity type, and then stuff it all in one table. The problem I've encountered so far is that now I can not use constructs like find("from " + EntityName.class)
or loadAll(EntityName.class)
- because that would load the lot.
Is there some way of configuring the entity to automatically add a where clause to ensure only the right type of data is returned per entity? (Ie, I don't want to have to add the where clause to each but of code that loads each entity)
Yes. What you're describing is the "table per class hierarchy" inheritance mapping strategy.
精彩评论