Enumerations in Hibernate

走远了吗. 提交于 2019-11-27 10:33:01

using hibernate or JPA annotations:

class User {
   @Enumerated(EnumType.STRING)
   UserType type
}

UserType is just a standard java 5 enum.

I can't imagine this is just limited to just annotations but I don't actually know how to do this with hbm files. It may be very version dependant, I'm guessing but I'm pretty sure that hibernate 3.2+ is required.

edit: it is possible in a hbm, but is a little messy, have a look at this forum thread

Craig

From the Hibernate documentation: http://www.hibernate.org/272.html

You can create a new typedef for each of your enums and reference the typedefs in the property tag.

Example Mapping - inline <type> tag

  <property name='suit'>
    <type name="EnumUserType">
      <param name="enumClassName">com.company.project.Suit</param>
    </type>
  </property>

Example Mapping - using <typedef>

  <typedef name="suit" class='EnumUserType'>
      <param name="enumClassName">com.company.project.Suit</param>
  </typedef>

  <class ...>
    <property name='suit' type='suit'/>
  </class>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!