問題
Java BO類Gender是枚舉類型
[java] view plaincopyprint?
public enum Gender{
UNKNOWN(
MALE(
FEMALE(
private String key;
private Gender(final String key) {
this
}
public getGender(String key) {
for (Gender gender : Gender
if (key
return gender;
}
throw new NoSuchElementException(key);
}
}
public enum Gender{
UNKNOWN(
MALE(
FEMALE(
private String key;
private Gender(final String key) {
this
}
public getGender(String key) {
for (Gender gender : Gender
if (key
return gender;
}
throw new NoSuchElementException(key);
}
}
使用UserType
[java] view plaincopyprint?
public class GenderUserType implements UserType {
private static int[] typeList = { Types
/*
* Return the SQL type codes for the columns mapped by this type
* The codes are defined on <tt>java
/**設置和Gender類的sex屬性對應的字段的SQL類型 */
public int[] sqlTypes() {
return typeList;
}
/*The class returned by <tt>nullSafeGet()</tt>
/** 設置GenderUserType所映射的Java類
public Class returnedClass() {
return Gender
}
/** 指明Gender類是不可變類 */
public boolean isMutable() {
return false;
}
/*
* Return a deep copy of the persistent state
* collections
* values
*/
/** 返回Gender對象的快照
public Object deepCopy(Object value) {
return (Gender)value;
}
/** 比較一個Gender對象是否和它的快照相同 */
public boolean equals(Object x
//由於內存中只可能有兩個靜態常量Gender實例
//因此可以直接按內存地址比較
return (x == y);
}
public int hashCode(Object x){
return x
}
/*
* Retrieve an instance of the mapped class from a JDBC resultset
* should handle possibility of null values
*/
/** 從JDBC ResultSet中讀取key
public Object nullSafeGet(ResultSet rs
throws HibernateException
//從ResultSet中讀取key
String sex = (String) Hibernate
if (sex == null) { return null; }
//按照性別查找匹配的Gender實例
try {
return Gender
}catch (java
throw new HibernateException(
}
}
/*
* Write an instance of the mapped class to a prepared statement
* should handle possibility of null values
* A multi
*/
/** 把Gender對象的key屬性添加到JDBC PreparedStatement中 */
public void nullSafeSet(PreparedStatement st
throws HibernateException
String sex = null;
if (value != null)
sex = ((Gender)value)
Hibernate
}
/*
* Reconstruct an object from the cacheable representation
* method should perform a deep copy if the type is mutable
*/
public Object assemble(Serializable cached
return cached;
}
/*
* Transform the object into its cacheable representation
* method should perform a deep copy if the type is mutable
* for some implementations
* identifier values
*/
public Serializable disassemble(Object value) {
return (Serializable)value;
}
/*
* During merge
* with a new (original) value from the detached entity we are merging
* objects
* mutable objects
* with component values
*/
public Object replace(Object original
return original;
}
}
public class GenderUserType implements UserType {
private static int[] typeList = { Types
/*
* Return the SQL type codes for the columns mapped by this type
* The codes are defined on <tt>java
/**設置和Gender類的sex屬性對應的字段的SQL類型 */
public int[] sqlTypes() {
return typeList;
}
/*The class returned by <tt>nullSafeGet()</tt>
/** 設置GenderUserType所映射的Java類
public Class returnedClass() {
return Gender
}
/** 指明Gender類是不可變類 */
public boolean isMutable() {
return false;
}
/*
* Return a deep copy of the persistent state
* collections
* values
*/
/** 返回Gender對象的快照
public Object deepCopy(Object value) {
return (Gender)value;
}
/** 比較一個Gender對象是否和它的快照相同 */
public boolean equals(Object x
//由於內存中只可能有兩個靜態常量Gender實例
//因此可以直接按內存地址比較
return (x == y);
}
public int hashCode(Object x){
return x
}
/*
* Retrieve an instance of the mapped class from a JDBC resultset
* should handle possibility of null values
*/
/** 從JDBC ResultSet中讀取key
public Object nullSafeGet(ResultSet rs
throws HibernateException
//從ResultSet中讀取key
String sex = (String) Hibernate
if (sex == null) { return null; }
//按照性別查找匹配的Gender實例
try {
return Gender
}catch (java
throw new HibernateException(
}
}
/*
* Write an instance of the mapped class to a prepared statement
* should handle possibility of null values
* A multi
*/
/** 把Gender對象的key屬性添加到JDBC PreparedStatement中 */
public void nullSafeSet(PreparedStatement st
throws HibernateException
String sex = null;
if (value != null)
sex = ((Gender)value)
Hibernate
}
/*
* Reconstruct an object from the cacheable representation
* method should perform a deep copy if the type is mutable
*/
public Object assemble(Serializable cached
return cached;
}
/*
* Transform the object into its cacheable representation
* method should perform a deep copy if the type is mutable
* for some implementations
* identifier values
*/
public Serializable disassemble(Object value) {
return (Serializable)value;
}
/*
* During merge
* with a new (original) value from the detached entity we are merging
* objects
* mutable objects
* with component values
*/
public Object replace(Object original
return original;
}
}
然後再hbm
[html] view plaincopyprint?
<hibernate
<typedef name=
<property name=
<column name=
</column>
</property>
<hibernate
<typedef name=
<property name=
<column name=
</column>
</property>
延伸
為每個枚舉類型定義一個UserType是比較麻煩的
例如擴展下例即可適用於所有保存為index的枚舉類型
[java] view plaincopyprint?
public abstract class OrdinalEnumUserType<E extends Enum<E>> implements UserType {
protected Class<E> clazz;
protected OrdinalBasedEnumUserType(Class<E> clazz) {
this
}
private static final int[] SQL_TYPES = {Types
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class<?> returnedClass() {
return clazz;
}
public E nullSafeGet(ResultSet resultSet
throws HibernateException
//Hibernate
int index = resultSet
E result = null;
if (!resultSet
result = clazz
}
return result;
}
public void nullSafeSet(PreparedStatement preparedStatement
Object value
if (null == value) {
preparedStatement
} else {
//Hibernate
preparedStatement
}
}
public Object deepCopy(Object value) throws HibernateException{
return value;
}
public boolean isMutable() {
return false;
}
public Object assemble(Serializable cached
throws HibernateException {
return cached;
}
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable)value;
}
public Object replace(Object original
throws HibernateException {
return original;
}
public int hashCode(Object x) throws HibernateException {
return x
}
public boolean equals(Object x
if (x == y)
return true;
if (null == x || null == y)
return false;
return x
}
}
public abstract class OrdinalEnumUserType<E extends Enum<E>> implements UserType {
protected Class<E> clazz;
protected OrdinalBasedEnumUserType(Class<E> clazz) {
this
}
private static final int[] SQL_TYPES = {Types
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class<?> returnedClass() {
return clazz;
}
public E nullSafeGet(ResultSet resultSet
throws HibernateException
//Hibernate
int index = resultSet
E result = null;
if (!resultSet
result = clazz
}
return result;
}
public void nullSafeSet(PreparedStatement preparedStatement
Object value
if (null == value) {
preparedStatement
} else {
//Hibernate
preparedStatement
}
}
public Object deepCopy(Object value) throws HibernateException{
return value;
}
public boolean isMutable() {
return false;
}
public Object assemble(Serializable cached
throws HibernateException {
return cached;
}
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable)value;
}
public Object replace(Object original
throws HibernateException {
return original;
}
public int hashCode(Object x) throws HibernateException {
return x
}
public boolean equals(Object x
if (x == y)
return true;
if (null == x || null == y)
return false;
return x
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28186.html