简体   繁体   English

在Hibernate中标准化重复值-Java

[英]Normalize repeating values in Hibernate - Java

I have a String property of an entity, which is often repeated by other entities - which would (in traditional databases) be mapped to its own table. 我有一个实体的String属性,其他实体通常会重复使用该属性-在传统数据库中,该属性会映射到其自己的表中。

For example: I could having a clothing entity, with each item of clothing having its own object or row. 例如:我可以有一个服装实体,每件服装都有自己的对象或行。 Each item will have a brand, but this brand (String) could be repeated by many other clothing items. 每个项目都会有一个品牌,但是许多其他服装项目可能会重复使用此品牌(字符串)。 - it should essentially be a manyToOne mapping, though brand is not an entity on its own, its just a String. -本质上,它应该是manyToOne映射,尽管品牌本身不是一个实体,而只是一个String。

How would I do this in hibernate? 在休眠状态下我该怎么办? Or should I rather create an entity for each brand and use ManyToOne? 还是应该为每个品牌创建一个实体并使用ManyToOne?

Any help will be appreciated! 任何帮助将不胜感激!

I think that, just in terms of database normalization (most specifically 3NF ), if you are expecting a column to have repeated values, you should export those values to their own table and have a foreign key column. 我认为,就数据库规范化而言 (最具体地说是3NF ),如果您期望一列具有重复值,则应将这些值导出到它们自己的表中并具有一个外键列。 That way, if one of those values changed you could change them all at once. 这样,如果其中一个值发生更改,则可以立即更改所有这些值。

That would allow you to use ManyToOne in Hibernate as well. 这样一来,您还可以在Hibernate中使用ManyToOne。

However, if that isn't possible, I would recommend using an Enum . 但是,如果不可能,我建议使用Enum

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

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