How to map a set of enum type in Hibernate?

后端 未结 3 824
梦毁少年i
梦毁少年i 2020-12-09 22:00

In hibernate, is it possible to define a mapping for a class to a set of enums?

I\'ve been able to find examples of how to define mappings of Sets and I\'ve been a

3条回答
  •  没有蜡笔的小新
    2020-12-09 22:40

    Does this not do what you need?

    To elaborate on the flippant initial response, the reference provides a means to use the ordinal of the enum to map enumerations.

    In this case it's actually simpler than it looks, because you are hosting the enums in a set, you need to provide an accessor for the WicketType to the sub-type of IntEnumUserType, the super-type will take care of mapping the ordinal to the instance.

    package test;
    
    public class WicketTypeState extends IntEnumUserType {
        private WicketType wicketType;
    
    public WicketTypeState() {
        // we must give the values of the enum to the parent.
        super(WicketType.class, WicketType.values());
    }
    
        public WicketType getWicketType() {
            return wicketType;
        }
    
        public void setWicketType(final WicketType wicketType) {
            this.wicketType = wicketType;
        }
    }
    

    Define the mappings for the enum table:

      
      
        
        
          
          
        
      
    
    

    Then for the type with the set of enums, define a set mapping for that property:

    
      
        
      
    
    

    This worked on my box(tm), let me know if you need any more info.

提交回复
热议问题