将枚举映射到hibernate中的字符串 [英] Mapping enum to string in hibernate

查看:112
本文介绍了将枚举映射到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中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆