我在两个不同的表中有两个不同的枚举(它们具有相同的结构,但含义不同)。
这是我从FIRST_SCHEMA开始的第一个表,叫做TYPE
id team desc days
1 "AB" "Do Thins" 2
2 "CD" "Other things" 3
这是SECOND_SCHEMA中的第二个类型,称为TYPE。
id team desc days
1 "AB" "Other other thins" 1
2 "CD" "More and more things" 1
我尝试使用休眠注释在Java中映射此枚举,它们就像这样:
FirstEnum.java
@Entity
@Table(name = "TYPE", schema="FIRST_SCHEMA")
public enum FirstEnum implements SuperEnum<FirstEnum> {
DO_THINGS(1, "AB", "Do things", 2),
OTHER_THINGS(2, "CD", "Other things", 3);
@Id
@NotNull
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private final int value;
@Column(name = "team")
private final String team;
@Column(name = "desc")
private final String dec;
@Column(name = "days")
private final int days;
//getters and setters
}
SecondEnum.java
@Entity
@Table(name = "TYPE", schema="SECOND_SCHEMA")
public enum SecondEnum implements SuperEnum<SecondEnum> {
DO_THINGS(1, "AB", "Other other thins", 2),
OTHER_THINGS(2, "CD", "More and more thingsS", 3);
@Id
@NotNull
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private final int value;
@Column(name = "team")
private final String team;
@Column(name = "desc")
private final String dec;
@Column(name = "days")
private final int days;
//getters and setters
}
使用这些类型之一的类的映射如下:
Foo.java
@Table(name = "TABLE", schema="FIRST_SCHEMA")
@Entity
public class Foo extends Bar implements Serializable {
private static final long serialVersionUID = 1L;
@Enumerated(EnumType.ORDINAL)
@Column(name = "id_type")
private FirstEnum type;
//getters and setters
}
如果我在persistence.xml
中设置以下行:
<property name="hibernate.hbm2ddl.auto" value="update" />
到
<property name="hibernate.hbm2ddl.auto" value="none" />
它可以按预期工作,不会创建新的“类型”表。但是,如果没有,即使您已经拥有类型表,它也会创建一个空表。
我映射错了什么?
答案 0 :(得分:0)
首先,枚举用于常量值.. 如果这样放在表里,还不如直接创建一个类..
@Entity
@Data
@Accessors(chain = true)
public class FirstEnum {
@Id
@NotNull
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int value;
@Column(name = "team")
private String team;
@Column(name = "desc")
private String dec;
@Column(name = "days")
private int days;
}
枚举功能用于在您的类中具有恒定的业务逻辑。 所以也许你可以在没有数据库的情况下定义枚举..
public enum SecondEnum {
DO_THINGS(1, "AB", "Other other thins", 2),
OTHER_THINGS(2, "CD", "More and more thingsS", 3);
}