本文介绍了将枚举映射到hibernate中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类别Hibernate模型:
I've got a Category Hibernate model:
@Entity @Table(name = "category") public class Category { @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name = "id") private long id; @Column(name = "type") private String type;其中有一个字符串类型字段。另外我有一个代表一个类别的Java枚举:
which have a type string field. Also I've got a Java enum which represent a type of a category:
public enum CategoryType { INCOME, OUTCOME; }我想使用而不是字符串类型。 SQL在varchar参数中接受两个不同的值: CategoryIncome 或 CategoryOutcome 。我想让类别模型类接受一个枚举变量 - 并且在hibernate要求的时候将其映射到字符串。
which I would like to use instead of the string type. The SQL accepts two distinct values in the varchar parameter: either CategoryIncome or CategoryOutcome. I would like the Category model class to accept an enum variable - and map it somehow to the string whenever hibernate asks for it.
可以吗?
推荐答案是的,是可以的。它应该是:
Yes, is possible. It should be:
@Enumerated(EnumType.STRING) @Column(name = "category_type") private CategoryType categoryType;更多推荐
将枚举映射到hibernate中的字符串
发布评论