These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Using a PersistentEnum
PostPosted: Thu Sep 04, 2003 11:21 am 
Newbie

Joined: Thu Sep 04, 2003 10:05 am
Posts: 3
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

_________________
waving not drowning


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2003 11:02 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
The mapping looks ok. What happens if you run the mapping through the hibernate schema generator tool?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2003 1:11 pm 
Newbie

Joined: Thu Sep 04, 2003 10:05 am
Posts: 3
That's useful to know.

When I run it through the schema generator tool using Maven, I get the following exception:

Code:
BUILD FAILED
java.lang.NoClassDefFoundError: net/sf/hibernate/PersistentEnum
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:250)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
        at net.sf.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:2
60)
        at net.sf.hibernate.type.TypeFactory.hueristicType(TypeFactory.java:131)

        at net.sf.hibernate.cfg.Binder.getTypeFromXML(Binder.java:768)
        at net.sf.hibernate.cfg.Binder.bindValue(Binder.java:348)
        at net.sf.hibernate.cfg.Binder.bindComponent(Binder.java:696)
        at net.sf.hibernate.cfg.Binder.bindCollectionSecondPass(Binder.java:1036
)
        at net.sf.hibernate.cfg.Binder.bindSetSecondPass(Binder.java:904)
        at net.sf.hibernate.cfg.Binder$SetSecondPass.secondPass(Binder.java:1168
)
        at net.sf.hibernate.cfg.Binder$SecondPass.doSecondPass(Binder.java:1116)

        at net.sf.hibernate.cfg.Configuration.secondPassCompile(Configuration.ja
va:495)
        at net.sf.hibernate.cfg.Configuration.generateDropSchemaScript(Configura
tion.java:352)
        at net.sf.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:5
1)
        at org.apache.maven.hibernate.beans.SchemaExportBean.getSchemaExport(Sch
emaExportBean.java:358)
        at org.apache.maven.hibernate.beans.SchemaExportBean.execute(SchemaExpor
tBean.java:268)
        at org.apache.maven.hibernate.jelly.SchemaExportTag.execute(SchemaExport
Tag.java:83)
        at org.apache.maven.hibernate.jelly.SchemaExportTag.doTag(SchemaExportTa
g.java:98)
        at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:279)
        at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:135)
        at org.apache.commons.jelly.TagSupport.invokeBody(TagSupport.java:233)
        at com.werken.werkz.jelly.GoalTag$1.performAction(GoalTag.java:128)
        at com.werken.werkz.Goal.fire(Goal.java:639)
        at com.werken.werkz.Goal.attain(Goal.java:575)
        at com.werken.werkz.WerkzProject.attainGoal(WerkzProject.java:193)
        at com.werken.werkz.jelly.AttainGoalTag.doTag(AttainGoalTag.java:134)
        at org.apache.maven.jelly.tags.werkz.LazyAttainGoalTag.doTag(LazyAttainG
oalTag.java:107)
        at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:279)
        at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:135)
        at org.apache.commons.jelly.TagSupport.invokeBody(TagSupport.java:233)
        at com.werken.werkz.jelly.GoalTag$1.performAction(GoalTag.java:128)
        at com.werken.werkz.Goal.fire(Goal.java:639)
        at com.werken.werkz.Goal.attain(Goal.java:575)
        at org.apache.maven.plugin.PluginManager.attainGoals(PluginManager.java:
434)
        at org.apache.maven.MavenSession.attainGoals(MavenSession.java:348)
        at org.apache.maven.cli.App.doMain(App.java:525)
        at org.apache.maven.cli.App.main(App.java:1088)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:324)
        at com.werken.forehead.Forehead.run(Forehead.java:543)
        at com.werken.forehead.Forehead.main(Forehead.java:573)
File...... file:/C:/maven-1.0-beta-10/plugins/maven-hibernate-plugin-1.0-SNAPHOT
/
Element... h:schema-export
Line...... 27
Column.... 53
net/sf/hibernate/PersistentEnum
Total time:  14 seconds


which looks (as I suspected) like there is a problem with the Maven Hibernate plugin, in particular how it is setting up its classpath.

I'll try running the schema export tool manually and see if I can get exports working properly that way.

Thanks for the quick response - I'll post any outcomes here.

sam

_________________
waving not drowning


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 06, 2003 4:44 am 
Newbie

Joined: Thu Sep 04, 2003 10:05 am
Posts: 3
Well that was the problem all right - when I used Ant rather than Maven to automate the SchemaExport process, everything worked just fine.

_________________
waving not drowning


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.