作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 Spring JPA 时,枚举值不会作为 EnumType.STRING 保留,而是作为子实体的 EnumType.ORDINAL 值保留
我的 Employee 实体具有名称、ENUM 字段和地址(子)实体的一对多关联。保存 Employee 实体时,父实体上的枚举字段保留为字符串(如预期),但子实体中的枚举字段保留为序数值,尽管我在父实体和子实体中使用了 @Enumerated EnumType.STRING .
我的实体是
Employee.java
@Entity
@Table(name = "employee", schema = "schema_emp")
public class Employee {
public enum Status {
PROJECT,
BENCH;
}
private String _name;
private Status _status;
private Map<EmployeeAddress.Type, EmployeeAddress> _addresses;
protected Employee () {
}
@Column(name = "employee_name", nullable = false)
public String getName() {
return this._name;
}
public void setName(String name) {
this._name = name;
}
@Column(name = "status_text", nullable = false)
@Enumerated(EnumType.STRING)
public Status getStatus() {
return this._status;
}
protected void setStatus(Status status) {
this._status= status;
}
@ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "address_type_name")
@OneToMany(fetch = FetchType.EAGER, mappedBy = "employee", cascade = CascadeType.ALL, orphanRemoval = true)
public Map<EmployeeAddress.Type, EmployeeAddress> getAddresses() {
return _addresses;
}
public void setAddresses(Map<EmployeeAddress.Type, EmployeeAddress> addresses) {
_addresses = addresses;
}
}
EmployeeAddress.java
@Entity
@Table(name = "employee_address", schema = "schema_emp")
public class EmployeeAddress {
public enum Type {
PRIMARY,
BILLING,
MAILING,
SHIPPING;
}
private Employee _employee;
private Type _type;
private String _line1;
protected EmployeeAddress() {
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "employee_id", nullable = false)
public Employee getEmployee() {
return _employee;
}
public void setEmployee(Employee employee) {
_employee = employee ;
}
@Column(name = "address_type_name", nullable = false)
@Enumerated(EnumType.STRING)
@NotNull
public Type getType() {
return _type;
}
public void setType(Type type) {
_type = type;
}
@Column(name = "address_line_1")
public String getLine1() {
return _line1;
}
public void setLine1(String line1) {
_line1 = line1;
}
}
My service method
Employee entity = populateEntity(resource);
new Employee .Builder<>().buildAddresses(resource.getAddresses(), entity);
return _repository.saveAndFlush(entity);
当我检查数据库时,在 Employee 表中按预期创建了一行,并在 employee_address 表中创建了适当数量的行
在员工表中,status_text 字段是 ACTIVE 但在 employee_address 表中,address_type_name 为 0 和 3(即 ORDINAL 值)
那么如何将 ENUM 值存储为子表上的字符串,即 employee_address
最佳答案
一种解决方案是定义一个 Converter 类并自动将其应用于子实体中的类型字段
转换器代码:
@Converter(autoApply = true)
public class AddressTypeConverter implements AttributeConverter<Type, String> {
@Override
public String convertToDatabaseColumn(Type type) {
return type.name();
}
@Override
public Type convertToEntityAttribute(String typeStr) {
return Type.valueOf(typeStr);
}
}
及其在 EmployeeAddress 类中的用法:
@Column(name = "address_type_name", nullable = false)
@Convert(converter = AddressTypeConverter.class)
@NotNull
public Type getType() {
return _type;
}
关于spring - 枚举值不保留为 EnumType.STRING,而是保留为子实体的 ORDINALvalue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41128622/
我是一名优秀的程序员,十分优秀!