Hibernate Enum mapping using annotaions

后端 未结 2 2032
长情又很酷
长情又很酷 2021-01-18 16:20

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

相关标签:
2条回答
  • 2021-01-18 16:41

    If your Database values are "new", "mailed", "in" and "out" then your Enum need exactly the same names. - I believe that the problem is, that your Enums are in capital letters but your data base values not.

    0 讨论(0)
  • 2021-01-18 16:44

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

    UPDATE

    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()));
       }
    

    }

    0 讨论(0)
提交回复
热议问题