简体   繁体   English

休眠实体取决于列值

[英]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 by the value of some column? Hibernate是否有可能在数据库中拥有一个表,该表存储多种类型的实体,并根据某些列的值进行区分?

I've got an application that uses lists of data, the structure of all the data is the same - id, description, sequence. 我有一个使用数据列表的应用程序,所有数据的结构相同-id,描述,序列。 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. 到目前为止,我遇到的问题是,现在我不能使用诸如find("from " + EntityName.class)loadAll(EntityName.class) -因为那样会加载很多东西。

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? 是否有某种方法可以配置实体以自动添加where子句以确保每个实体仅返回正确类型的数据? (Ie, I don't want to have to add the where clause to each but of code that loads each entity) (即,我不需要在每个子句中添加where子句,但可以加载每个实体的代码)

Yes. 是。 What you're describing is the "table per class hierarchy" inheritance mapping strategy. 您要描述的是“每个类表的表”继承映射策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM