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
- Public Enum consumertype
- {
- Admin, vistor, VIP;
- }
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.
- Import java. Io. serializable;
- Import java. SQL. preparedstatement;
- Import java. SQL. resultset;
- Import java. SQL. sqlexception;
- Import java. SQL. types;
- Import org. hibernate. hibernateexception;
- Import org. hibernate. usertype. usertype;
- Public class intenumusertype <E extends Enum <E> implements usertype
- {
- Private class <E> clazz = NULL;
- Private e [] theenumvalues;
- Protected intenumusertype (class <E> C, E [] E)
- {
- This. clazz = C;
- This. theenumvalues = E;
- }
- Private Static final int [] SQL _types = {types. smallint };
- Public int [] sqltypes ()
- {
- Return SQL _types;
- }
- Public class <E> returnedclass ()
- {
- Return clazz;
- }
- Public object nullsafeget (resultset, string [] names, object owner)
- Throws hibernateexception, sqlexception
- {
- Final int val = resultset. getshort (Names [0]);
- E result = NULL;
- If (! Resultset. wasnull ())
- {
- Try
- {
- For (INT I = 0; I <theenumvalues. Length & Result = NULL; I ++)
- {
- If (theenumvalues [I]. ordinal () = Val)
- {
- Result = theenumvalues [I];
- }
- }
- }
- Catch (securityexception E)
- {
- Result = NULL;
- }
- Catch (illegalargumentexception E)
- {
- Result = NULL;
- }
- }
- Return result;
- }
- Public void nullsafeset (preparedstatement,
- Object value, int index) throws hibernateexception, sqlexception
- {
- If (null = value)
- {
- Preparedstatement. setnull (index, types. smallint );
- }
- Else
- {
- Preparedstatement. setint (index, (Enum) value). ordinal ());
- }
- }
- Public object deepcopy (object Value) throws hibernateexception
- {
- Return value;
- }
- Public Boolean ismutable ()
- {
- Return false;
- }
- Public object assemble (serializable cached, object owner) throws hibernateexception
- {
- Return cached;
- }
- Public serializable disassemble (object Value) throws hibernateexception
- {
- Return (serializable) value;
- }
- Public object Replace (Object original, object target, object owner) throws hibernateexception
- {
- Return Original;
- }
- Public int hashcode (Object X) throws hibernateexception
- {
- Return X. hashcode ();
- }
- Public Boolean equals (Object X, object y) throws hibernateexception
- {
- If (x = y)
- Return true;
- If (null = x | null = y)
- Return false;
- Return X. Equals (y );
- }
- }
Next, create a class to implement the template.
- /** This class is used only in the hibernate xml configuration */
- Public class consumertypeenum extends intenumusertype <consumertype>
- {
- Public consumertypeenum ()
- {
- // We must give the values of the enum to the parent.
- Super (consumertype. Class, consumertype. Values ());
- }
- }
Finally, use the following code in the mapping XML file:
- <Property column = "usertpye"
- Not-null = "true"
- Name = "consumertype"
- Type = "consumertypeenum"/>
Note that consumertypeenum is used instead of consumertype.
At this point, the conversion is complete ~