2016-12-13 3 views
2

列挙値がEnumType.STRINGとしてではなく、子エンティティのEnumType.ORDINAL値として永続化されていない子エンティティのためのORDINALvalueとして永続化されていない私が持っているEmployeeエンティティを持っています名前、ENUMフィールド、および1対多のアドレス(子)エンティティの関連付けが含まれます。従業員エンティティが保存されるとき、親エンティティの列挙型フィールドはString(期待どおり)として永続化されますが、親エンティティと子エンティティの@Enumerated EnumType.STRINGを使用していますが、子エンティティの列挙フィールドは序数値として保持されます。私はDBを確認すると、期待どおり列挙値が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); 

、1行従業員テーブルに作成され、行の適切な数は、従業員テーブルで

employee_addressテーブルに作成され、STATUS_TEXTフィールドでありますACTIVEですが、 employee_addressテーブルでは、address_type_nameは0と3です(ORDINAL値)

ENUM値を子テーブル、つまりemployee_addressにStringとして格納する方法

答えて

0

一つの解決策は、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; 
} 
関連する問題