2
枚举值不会持久化为EnumType.STRING但作为子实体EnumType.ORDINAL价值坚持我有了Employee实体名称,ENUM字段和地址(子)实体的一对多关联。当Employee实体被保存时,父实体上的枚举字段被持久化为String(如预期的那样),但子实体中的枚举字段被持久化为序号值,尽管我在父类中使用了@Enumerated EnumType.STRING以及子实体。枚举值不为EnumType.STRING但ORDINALvalue儿童实体
我的实体
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(即序号值)
那么如何枚举值作为字符串存储子表上即employee_address