Convert Enum to smallint using hibernate

Source: Internet
Author: User
For database reasons, smallint must be used to store enumeration types.

Hibernate 3.0 or above also supports Enum type conversion. Here we take smallint as an example (of course, we can also convert it to another type, such as varchar ).

 

First, the following is an enumeration type

  1. Public Enum consumertype
  2. {
  3. Admin, vistor, VIP;
  4. }

Then, write another template to implement the usertype interface. In this way, you can convert all the enumeration types written later to smallint and save them to the database.

 

  1. Import java. Io. serializable;
  2. Import java. SQL. preparedstatement;
  3. Import java. SQL. resultset;
  4. Import java. SQL. sqlexception;
  5. Import java. SQL. types;
  6. Import org. hibernate. hibernateexception;
  7. Import org. hibernate. usertype. usertype;
  8. Public class intenumusertype <E extends Enum <E> implements usertype
  9. {
  10. Private class <E> clazz = NULL;
  11. Private e [] theenumvalues;
  12. Protected intenumusertype (class <E> C, E [] E)
  13. {
  14. This. clazz = C;
  15. This. theenumvalues = E;
  16. }
  17. Private Static final int [] SQL _types = {types. smallint };
  18. Public int [] sqltypes ()
  19. {
  20. Return SQL _types;
  21. }
  22. Public class <E> returnedclass ()
  23. {
  24. Return clazz;
  25. }
  26. Public object nullsafeget (resultset, string [] names, object owner)
  27. Throws hibernateexception, sqlexception
  28. {
  29. Final int val = resultset. getshort (Names [0]);
  30. E result = NULL;
  31. If (! Resultset. wasnull ())
  32. {
  33. Try
  34. {
  35. For (INT I = 0; I <theenumvalues. Length & Result = NULL; I ++)
  36. {
  37. If (theenumvalues [I]. ordinal () = Val)
  38. {
  39. Result = theenumvalues [I];
  40. }
  41. }
  42. }
  43. Catch (securityexception E)
  44. {
  45. Result = NULL;
  46. }
  47. Catch (illegalargumentexception E)
  48. {
  49. Result = NULL;
  50. }
  51. }
  52. Return result;
  53. }
  54. Public void nullsafeset (preparedstatement,
  55. Object value, int index) throws hibernateexception, sqlexception
  56. {
  57. If (null = value)
  58. {
  59. Preparedstatement. setnull (index, types. smallint );
  60. }
  61. Else
  62. {
  63. Preparedstatement. setint (index, (Enum) value). ordinal ());
  64. }
  65. }
  66. Public object deepcopy (object Value) throws hibernateexception
  67. {
  68. Return value;
  69. }
  70. Public Boolean ismutable ()
  71. {
  72. Return false;
  73. }
  74. Public object assemble (serializable cached, object owner) throws hibernateexception
  75. {
  76. Return cached;
  77. }
  78. Public serializable disassemble (object Value) throws hibernateexception
  79. {
  80. Return (serializable) value;
  81. }
  82. Public object Replace (Object original, object target, object owner) throws hibernateexception
  83. {
  84. Return Original;
  85. }
  86. Public int hashcode (Object X) throws hibernateexception
  87. {
  88. Return X. hashcode ();
  89. }
  90. Public Boolean equals (Object X, object y) throws hibernateexception
  91. {
  92. If (x = y)
  93. Return true;
  94. If (null = x | null = y)
  95. Return false;
  96. Return X. Equals (y );
  97. }
  98. }

Next, create a class to implement the template.

 

  1. /** This class is used only in the hibernate xml configuration */
  2. Public class consumertypeenum extends intenumusertype <consumertype>
  3. {
  4. Public consumertypeenum ()
  5. {
  6. // We must give the values of the enum to the parent.
  7. Super (consumertype. Class, consumertype. Values ());
  8. }
  9. }

Finally, use the following code in the mapping XML file:

 

  1. <Property column = "usertpye"
  2. Not-null = "true"
  3. Name = "consumertype"
  4. Type = "consumertypeenum"/>

Note that consumertypeenum is used instead of consumertype.

 

At this point, the conversion is complete ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.