将枚举映射到hibernate中的字符串

编程入门 行业动态 更新时间:2024-10-28 14:29:55
本文介绍了将枚举映射到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中的字符串

本文发布于:2023-10-23 03:30:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519622.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   hibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!