Hello, I'm hoping that someone can give me an example of using Hibernate's PersistentEnum within a persistent class.
I have the following class which implements the PersistentEnum interface:
Code:
package eg;
import net.sf.hibernate.PersistentEnum;
public class MembershipLevel implements PersistentEnum {
private final int level;
private MembershipLevel(int level) {
this.level = level;
}
public static final MembershipLevel MEMBER = new MembershipLevel(0);
public static final MembershipLevel EDITOR = new MembershipLevel(1);
public static final MembershipLevel ADMINISTRATOR = new MembershipLevel(2);
public int toInt() {
return level;
}
public static MembershipLevel fromInt(int level) {
switch (level) {
case 0 :
return MEMBER;
case 1 :
return EDITOR;
case 2 :
return ADMINISTRATOR;
default :
throw new RuntimeException("Unknown membership level.");
}
}
}
which is basically identical to the PersistentEnum example given in the Hibernate documentation.
Having created this class however, I'm unclear how I can make use of it within a persistant class. I'm making use of Xdoclet as a way of generating mapping documents, and had hoped to be able to use it like this:
Code:
package eg;
/**
* @hibernate.class
* table="COMMUNITY_MEMBERSHIP"
*/
public class CommunityMembership {
private String id = null;
private MembershipLevel membershipLevel = null;
/**
* @hibernate.property
* column="MEMBERSHIP_LEVEL"
*/
public MembershipLevel getMembershipLevel() {
return membershipLevel;
}
/**
* Sets the membership level of a user with a community.
* @param membershipLevel
*/
public void setMembershipLevel(MembershipLevel membershipLevel) {
this.membershipLevel = membershipLevel;
}
/**
* @hibernate.id
* column="ID"
* unsaved-value="any"
* generator-class="uuid.string"
*/
public String getId() {
return id;
}
/**
* @param string - the id of this object.
*/
public void setId(String string) {
id = string;
}
}
This then gives me a generated mapping document which looks like this:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="eg.CommunityMembership"
table="COMMUNITY_MEMBERSHIP"
dynamic-update="false"
dynamic-insert="false">
<id
name="id"
column="ID"
type="java.lang.String"
unsaved-value="any">
<generator class="uuid.string"></generator>
</id>
<property
name="membershipLevel"
type="eg.MembershipLevel"
update="true"
insert="true"
column="MEMBERSHIP_LEVEL" />
</class>
</hibernate-mapping>
but when I try and use this for mapping document to export a schema to the database it doesn't work, and gives me a NoClassDefFoundError for the PersistentEnum class.
I'm using Maven's hibernate:schema-export plugin which complicates matters because I'm not sure if it's a problem with the plugin, or whether I'm just not setting up the mapping documents correctly.
An example of correct usage would be greatly appreciated, so I can verify whether or not the problem is with my flaky code, or with Maven.
thanks
sam