-->
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: problem with EnhancedUserType as inner class
PostPosted: Sat Nov 08, 2008 4:08 pm 
Newbie

Joined: Wed Oct 31, 2007 9:16 pm
Posts: 9
I am trying to map a Java 5 enum to an int column in the database.
I'm using

Code:
  <property name="kind" type="com.mycompany.FooBar.KindUserType">
      <meta attribute="property-type">com.mycompany.FooBar.Kind</meta>
  </property>

Code:
public class FooBar {
  ...
  public static enum Kind { ... }
  public static class KindUserType implements EnhancedUserType { ... }
}

and I get this error:

Code:
org.hibernate.MappingException: Could not determine type for: com.mycompany.FooBar.KindUserType, for columns: [org.hibernate.mapping.Column(kind)]
        at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
        at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
        at org.hibernate.mapping.Property.isValid(Property.java:185)
        at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:440)
        at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
        at org.hibernate.cfg.Configuration.validate(Configuration.java:1102)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
...

However, if I move KindUserType so that it is a stand alone class (not
an inner class) it works fine. It works fine with the enum as an
inner class, so it doesn't seem like it could be a classpath problem.

So is this just a hibernate bug or is there something I am missing?
hibernate-3.2.6.ga, jboss-4.2.3.GA, jdk1.5.0_15_64bit


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 09, 2008 7:48 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Shouldn't you refer to the inner class using $ not .

com.mycompany.FooBar$KindUserType


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 1:48 pm 
Newbie

Joined: Wed Oct 31, 2007 9:16 pm
Posts: 9
I just tried it and "$" actually works - which is strange, and I still
think it is a bug. Java uses "$" for the class file on the filesystem,
but uses "." everywhere else. For example you use "." in import
statements when importing inner classes. And in my example I
use "." for the property-type class, which is also an inner class.
So to get it to work I need to use:

Code:
<property name="kind" type="com.mycompany.FooBar$KindUserType">
    <meta attribute="property-type">com.mycompany.FooBar.Kind</meta>
</property>

both these classes are inner classes, but you have to use "$" in
one place and "." in the other. That just seems wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 3:21 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I agree it seems a bit awkward but it does makes sense. The type name has to be instantiated by hibernate using ClassLoader.loadClass(name)/Class.forName(name). For these methods the "binary name" is required i.e. with the $. My understanding of the meta tags is they are used by tools to generate code - not instantiated directly. In this case you probably want the canonical name (with the dot) otherwise you'll end with an import statement containing a $.


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.