Normalize repeating values in Hibernate - Java

ぃ、小莉子 提交于 2020-01-05 10:32:12

问题


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.

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.

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

Any help will be appreciated!


回答1:


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.

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

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



来源:https://stackoverflow.com/questions/10145069/normalize-repeating-values-in-hibernate-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!