当我尝试将自定义Expenditure对象映射到MySQL中的关系模型时,出现错误:
Initial SessionFactory creation failed.org.hibernate.MappingException: Could not determine type for: javax.money.MonetaryAmount, at table: Expenditure, for columns: [org.hibernate.mapping.Column(monetaryAmount)]
我的支出类别:
@Entity
public class Expenditure implements Comparable<Expenditure> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String description;
private MonetaryAmount monetaryAmount;
private LocalDate date;
private ExpenditureType type;
@OneToOne
private User client;
...
}
在这种情况下如何执行映射?
最佳答案
您可以使用jpa'2 @Convert
批注:
@Convert(converter = MonetaryAmountConverter.class)
private MonetaryAmount monetaryAmount;
然后像这样实现它:
@Converter
public class MonetaryAmountConverter implements AttributeConverter<MonetaryAmount, BigDecimal> {
@Override
public BigDecimal convertToDatabaseColumn(MonetaryAmount attribute) {...}
@Override
public MonetaryAmount convertToEntityAttribute(BigDecimal dbData) {...}
}