Hibernate的枚举使用映射annotaions [英] Hibernate Enum mapping using annotaions

查看:126
本文介绍了Hibernate的枚举使用映射annotaions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的数据库,我现在连接到使用Hibernate。我不能在此刻改变它的数据,并拥有一切从一列工作分开。

I have an existing database that I am now connecting to using hibernate. I cannot change the data in it at the moment and have everything working apart from a single column.

我有了值的状态栏:


  • 新的

  • 邮寄


  • 退出

和列如下被映射

@Column(name = "STATUS", nullable = false, length = 50)
@Enumerated(EnumType.STRING)
private TeamMemberStatus status;

我真的想(用于应用程序的原因)有此列映射为一个Java枚举(TeamMemberStatus),但是由于这样的事实,新是Java中的关键字我不能有作为一个枚举成员。

I would REALLY like (for application reasons) to have this column mapped as a Java Enum (TeamMemberStatus), but due to the fact that 'new' is a keyword in Java I cannot have that as an enum member.

如果我有枚举contstants因为里面EnumType它做了Enum.valueOf()。

If I have the enum contstants NEW, MAILED, IN and OUT hibernate fails as inside EnumType it does a Enum.valueOf().

有没有办法对我来说,无需编写复杂的UserType这映射到我的枚举?

Is there any way for me to map this to my Enum without having to write a complex UserType?

- 添加的内容。

我的枚举是这样的:

public enum TeamMemberStatus {
    NEW, MAILED, IN, OUT 
}

是有效的Java枚举,但是不匹配的数据库的情况下。如果我改变以匹配像数据库:

is a valid Java enum, but not matching the case of the database. If I change it to match the database like:

public enum TeamMemberStatus {
    new, mailed, in, out
}

这不会编译为'新'是一个Java保留字。

It won't compile as 'new' is a Java reserved word.

推荐答案

如果您可以在数据库中使用SQL语句UPPER 后,它将不使用任何工作的UserType

If you can use a SQL UPPER statement at database, It will work without using any UserType

更新

好了,这不可能是最好的解决办法,但它解决了你想要的东西。

Well, It can not be The nicest solution but it solves what you want

@Entity
public class WrapperEntity {

   private TeamMemberStatus memberStatus;

   @Transient
   private TeamMemberStatus getMemberStatus() {
       return this.memberStatus;
   }

   public void setMemberStatus(TeamMemberStatus memberStatus) {
       this.memberStatus = memberStatus;
   }

   @Column(name="STATUS", nullable=false, length=50)
   public String getMemberStatusAsString() {
       return memberStatus.name().toLowerCase();
   }

   public void setMemberStatusAsString(String memberStatus) {
       this.setsetMemberStatus(TeamMemberStatus.valueOf(memberStatus.toUpperCase()));
   }

}

这篇关于Hibernate的枚举使用映射annotaions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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