简体   繁体   English

数据库中的Hibernate枚举关系映射

[英]Hibernate Enum relation mapping in database

Currently I have Enums let's say 目前我有枚举

enum Category {
  A, B
}

and

enum Type {
  TYPE1(Category.A), TYPE2(Category.B), TYPE3(Category.A)

  private Category cat;
  private Type(Category cat) {
    this.cat = cat;
  }

}

I am using EnumUserType for these enums in hibernate. 我在休眠中将EnumUserType用于这些枚举。 I would like to change it so that the mapping of Type to category would be defined in database rather than explicitly in the code, what would be the easiest way to do that? 我想更改它,以便将类型到类别的映射在数据库中定义而不是在代码中显式定义,最简单的方法是什么?

I would add column to table defining TYPE in database with foreign key referencing CATEGORY, what I would like to know is the hibernate mapping. 我会在表中添加列,并在数据库中使用引用CATEGORY的外键来定义TYPE,我想知道的是休眠映射。

(I am aware that this may seem like a stupid question) (我知道这似乎是一个愚蠢的问题)

You would need to create an Entity for at least Type (so no enum Type anymore). 您将需要至少为Type创建一个Entity(因此不再需要枚举Type)。 Then store the enum Category as a field in Type or also create an Entity for this. 然后将枚举类别存储为“类型”中的字段,或为此创建一个实体。

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

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