锁定老帖子 主题:在hibernate中如何实现枚举?
该帖已经被评为精华帖
|
|
---|---|
作者 | 正文 |
发表时间:2004-10-12
一个类的属性是下:
paymentCycle = [月|季|半年|年] 如何在hibernate中实现? 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2004-10-12
userType
|
|
返回顶楼 | |
发表时间:2004-10-12
我想,你提到的这种需求并不是枚举。真正的枚举需要如下定义了:
public class MyDateType { public static final int 月 = 0; public static final int 季 = 1; public static final int 半年 = 2; public static final int 年 = 3; } 你只不过想限定String的取值而已,那么我想这并不是Hibernate的事情,而是你的应用程序的责任。 |
|
返回顶楼 | |
发表时间:2004-10-12
如果是所谓类型安全的枚举或者java1.5的枚举,目前的方法是使用userType
|
|
返回顶楼 | |
发表时间:2004-10-12
本来我一直以为如此,现在听robbin说了后,反而不敢确定了。
package org.erproad.employeesys.vo; import org.apache.commons.lang.builder.HashCodeBuilder; import net.sf.hibernate.PersistentEnum; /** * @author weihello * *工资类型 */ public class SalaryType implements PersistentEnum{ private int code; public static SalaryType MONTHLY_PAY = new SalaryType(1);; public static SalaryType PIECE_RATE = new SalaryType(2);; public static SalaryType HOURLY_WAGES = new SalaryType(3);; public static SalaryType PIECE_HOURLY = new SalaryType(4);; /** * @param i */ public SalaryType(int i); { code = i; } /* (non-Javadoc); * @see net.sf.hibernate.PersistentEnum#toInt(); */ public int toInt(); { return code; } public static SalaryType fromInt(int code); { switch (code); { case 1 : return MONTHLY_PAY; case 2: return PIECE_RATE; case 3: return HOURLY_WAGES; case 4: return PIECE_HOURLY; default : throw new RuntimeException("Unknow code");; //$NON-NLS-1$ } } public String toString(); { switch (code); { case 1 : return "计月工资"; case 2 : return "计件工资"; case 3 : return "计时工资"; case 4 : return "计件计时工资"; default : throw new RuntimeException("Unknow code");; //$NON-NLS-1$ } } public boolean equals(Object obj); { if (obj instanceof SalaryType); return ((SalaryType); obj);.code == code; return super.equals(obj);; } } |
|
返回顶楼 | |
发表时间:2004-10-12
我没有做过枚举类型,我只是看了一下ersistentEnumType的源代码,里面对于枚举类型的操作就是往相应的ResultSet位置上放int值,我并没有发现有类似这样的操作
setString(index, object.toString()) 按照我对楼主的理解,他希望该枚举属性持久化到数据库里面应该是“月”“年”这些东西,而不是1,2,3这些东西。 你的SalaryType代码完全正解,但是仍然不是楼主希望的东西,我估计往数据库持久也是得插入int值,我会待会试试看。 |
|
返回顶楼 | |
发表时间:2004-10-12
确实是存int的。不过存int和字符串应该是差不多的。
其他的,这个枚举类型和原子类型一般处理即可。 |
|
返回顶楼 | |
发表时间:2004-10-12
试过了,确实如此。这也算一个最佳实践了吧。我也多学了点东西。
但是不太明白为什么PersistentEnum被deprecated,并且这个接口的语意也不完整,缺了一个 public static fromInt(int code); 方法。不过Javadoc却给了出来。并且还说明要被移到2.2中(Hibernate3)。没有看过3的源码,不清楚有什么改进没有。 |
|
返回顶楼 | |
发表时间:2004-10-12
也许PersistentEnum被deprecated可能是要适应java1.5语法级的enum?
|
|
返回顶楼 | |
发表时间:2004-10-12
找到了hibernate 3.0不过还不是1.5语法的枚举
Documentation > Community Area > UserType for persisting Typesafe Enumerations with a single class UserType for persisting Typesafe Enumerations with a single class The replacement solution for the now deprecated PersistentEnum given in UserType for persisting a Typesafe Enumeration with a VARCHAR column required that every enum implementation had a companion UserType implementation. Properties of the enum type had to be typed with the UserType implementation. This was bad news for users of the hbm2java POJO generator since setters/getters of such properties required the UserType implementation. Single class solution The solution presented here only requires one class per enum -- the enum itself. In adittion, the persisted representation of the enum can be either a char, an integer, a string, or the enum string name itself (more types can easily be added). There is one difference between real typesafe enums and the persistent type safe enums presented here -- they all require a no-arg constructor implementation (preferrably no-op). This is so that Hibernate will be able to create an instance while restoring objects from the database (the enum will still be resolved to an already created static enum instance in the nullSafeGet method). This is analogous to the requirement for Serialzable classes to have a public/protected no-arg constructor. How to use it? To create an enum persisted as char, simply provide an enum implementation that extend PersistentCharacterEnum. package com.foo; public final class Gender extends PersistentCharacterEnum { public static final Gender MALE = new Gender("male", 'M');; public static final Gender FEMALE = new Gender("female", 'F');; public static final Gender UNDETERMINED = new Gender("undetermined", 'U');; public Gender(); {} private Gender(String name, char persistentValue); { super(name, persistentValue);; } } Now this enumeration can be used on properties in your mapping file as: <property name="gender" type="com.foo.Gender"> Base class implementation All that is required to use this enum scheme is to import the following four classes into your project. public abstract class PersistentCharacterEnum extends PersistentEnum public abstract class PersistentIntegerEnum extends PersistentEnum public abstract class PersistentStringEnum extends PersistentEnum abstract class PersistentEnum implements Comparable, Serializable, UserType[/list] |
|
返回顶楼 | |