在Hibernate中标准化重复值-Java [英] Normalize repeating values in Hibernate - Java

查看:101
本文介绍了在Hibernate中标准化重复值-Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体的String属性,其他实体通常会重复使用该属性-在传统数据库中,该属性会映射到其自己的表中.

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.

例如:我可以有一个服装实体,每件服装都有自己的对象或行.每个项目都会有一个品牌,但是许多其他服装项目可能会重复使用此品牌(字符串). -本质上,它应该是manyToOne映射,尽管品牌本身并不是一个实体,而只是一个字符串.

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?

How would I do this in hibernate? Or should I rather create an entity for each brand and use ManyToOne?

任何帮助将不胜感激!

推荐答案

我认为,就而言数据库规范化(尤其是 3NF ),如果您希望某列重复出现值,您应该将这些值导出到它们自己的表中并具有一个外键列.这样,如果其中一个值发生更改,您可以立即更改所有这些值.

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. That way, if one of those values changed you could change them all at once.

这将使您也可以在Hibernate中使用ManyToOne.

That would allow you to use ManyToOne in Hibernate as well.

但是,如果不可能,我建议使用

However, if that isn't possible, I would recommend using an Enum.

这篇关于在Hibernate中标准化重复值-Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆