-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate Tools: How to activate User-Defined type?
PostPosted: Tue Sep 08, 2009 2:03 pm 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
I'm trying to use a user-defined type in Hibernate Tools reverse engineering.
Where do I specify the class path to make the tools recognize a user-defined type?
There seems to be something for Ant tasks, but I couldn't find a documentation for the equivalent in Hibernate Tools. (I'm not even sure whether this goes into hibernate.reveng.xml or in hibernate.cfg.xml.)

Any help appreciated!
Jo

For reference, some details about what I'm doing (heavily stripped&anonymized to keep the size down and protect the guilty):

hibernate.reveng.xml has this in a <table> section:
Code:
  <column name="SOME_COLUMN" type="user_defined_type"></column>

Here's the user-defined class in the project:
Code:
package my.project.lib.hibernate;
import ...
import org.hibernate.type.BooleanType;
public class UserDefinedType extends BooleanType {
  ...
  @Override
  public String getName () {
    return "user_defined_type";
  }
}

I expected the generated Pojo to have
Code:
  private Boolean someColumn;

((EDIT: plus a @Type annotation on the field to invoke the mapping, and maybe @TypeDef(s) somewhere else,)) but it had
Code:
  private user_defined_type someColumn;


(Yes I need a user-defined Boolean hibernate type, the database can contain ' ' or 'N' for Boolean.FALSE. Besides, I'll want to do enums next.)


Top
 Profile  
 
 Post subject: Re: Hibernate Tools: How to activate User-Defined type?
PostPosted: Wed Sep 16, 2009 5:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
The UserType class needs to be available to hibernate tools - i.e. on the classpath when executing.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Re: Hibernate Tools: How to activate User-Defined type?
PostPosted: Thu Sep 17, 2009 5:38 am 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
Agh... it wasn't a classpath issue.
Usertype class names need to be qualified with the package name. I.e. YesNoType is not enough, it must be something like package.name.YesNoType.
I was assuming the opposite because the predefined types do not need the package name.

Proposals:
1. Expand the examples in the docs for @TypeDef to cover user-defined types.
2. Make Hibernate Reverse Engineering Editor validate that any Hibernate Type given in a Column Detail is available.
Should I add Jira issues?

[UPDATE]

I see the generated classes are declared with the user-defined type.
I.e. I'm getting private YesNoType aovakz.

Types inferred from the database are handled differently. E.g. for a VARCHAR field, I'm getting private String aoartk; if it were handled as in the case of user-defined type, I'd see private StringType aoartk.

Is this intentional?
If yes: is there a mechanism to specify a transformation from database field type to Java member type?
I need something that can be specified on a field-by-field basis. VARCHAR(1) fields can have wildly varying semantics: most are essentially booleans, some encode what should be an Enum on the Java side, and a few are just strings (stuff like ABC status for article information).
So I can't use <type-mapping>, which looks just at the database type, not at the table and field name.


Top
 Profile  
 
 Post subject: Re: Hibernate Tools: How to activate User-Defined type?
PostPosted: Sun Sep 20, 2009 5:08 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Joachim Durchholz wrote:
Agh... it wasn't a classpath issue.
Usertype class names need to be qualified with the package name. I.e. YesNoType is not enough, it must be something like package.name.YesNoType.
I was assuming the opposite because the predefined types do not need the package name.


ok - sorry for the confusion.

Quote:
Proposals:
1. Expand the examples in the docs for @TypeDef to cover user-defined types.
2. Make Hibernate Reverse Engineering Editor validate that any Hibernate Type given in a Column Detail is available.
Should I add Jira issues?


Yup - patches welcome too ;)

Quote:
I see the generated classes are declared with the user-defined type.
I.e. I'm getting private YesNoType aovakz.

Types inferred from the database are handled differently. E.g. for a VARCHAR field, I'm getting private String aoartk; if it were handled as in the case of user-defined type, I'd see private StringType aoartk.


They are handled the exact same way. If the class exists .getReturnedClass() is called on it otherwise we return the specified typename.

So if you are still seeing this then the class is not on the classpath yet. Did you compile the class ?

Quote:
If yes: is there a mechanism to specify a transformation from database field type to Java member type?
I need something that can be specified on a field-by-field basis. VARCHAR(1) fields can have wildly varying semantics: most are essentially booleans, some encode what should be an Enum on the Java side, and a few are just strings (stuff like ABC status for article information).
So I can't use <type-mapping>, which looks just at the database type, not at the table and field name.


Yes, you can specify type in the table/column elements too of reveng.xml

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Re: Hibernate Tools: How to activate User-Defined type?
PostPosted: Wed Oct 07, 2009 10:13 am 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
max wrote:
Quote:
I see the generated classes are declared with the user-defined type.
I.e. I'm getting private YesNoType aovakz.

Types inferred from the database are handled differently. E.g. for a VARCHAR field, I'm getting private String aoartk; if it were handled as in the case of user-defined type, I'd see private StringType aoartk.


They are handled the exact same way. If the class exists .getReturnedClass() is called on it otherwise we return the specified typename.


YesNoType is a translator type, i.e. a descendant of org.hibernate.type.AbstractType.

Maybe I should specify <hibernate-reverse-engineering><table ...><column ... type="java.lang.Boolean">, but where do I tell Hibernate Tools that it should use my YesNoType instead of its standard org.hibernate.type.BooleanType?

max wrote:
So if you are still seeing this then the class is not on the classpath yet. Did you compile the class ?


The translator class is available as a .class file in the Eclipse project's bin directory (i.e. the output directory of the Java compiler).
The domain class is just java.lang.Boolean.

This gives me class instantiation errors:
Code:
!ENTRY org.hibernate.eclipse.console 4 666 2009-10-07 15:59:28.625
!MESSAGE org.hibernate.MappingException: Could not instantiate Type: my.domain.my.project.YesNoType
!STACK 0
org.hibernate.MappingException: Could not instantiate Type: my.domain.my.project.YesNoType
   at org.hibernate.type.TypeFactory.heuristicType(TypeFactory.java:234)
   at org.hibernate.type.TypeFactory.heuristicType(TypeFactory.java:210)
   at org.hibernate.cfg.JDBCBinder.guessAndAlignType(JDBCBinder.java:827)
   at org.hibernate.cfg.JDBCBinder.bindColumnToSimpleValue(JDBCBinder.java:781)
   at org.hibernate.cfg.JDBCBinder.bindBasicProperty(JDBCBinder.java:773)
   at org.hibernate.cfg.JDBCBinder.bindColumnsToProperties(JDBCBinder.java:719)
   at org.hibernate.cfg.JDBCBinder.createPersistentClasses(JDBCBinder.java:182)
   at org.hibernate.cfg.JDBCBinder.readFromDatabase(JDBCBinder.java:95)
   at org.hibernate.cfg.JDBCMetaDataConfiguration.readFromJDBC(JDBCMetaDataConfiguration.java:42)
   at org.hibernate.eclipse.launch.CodeGenerationLaunchDelegate$2.execute(CodeGenerationLaunchDelegate.java:313)
   at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:64)
   at org.hibernate.console.ConsoleConfiguration.execute(ConsoleConfiguration.java:94)
   at org.hibernate.eclipse.launch.CodeGenerationLaunchDelegate.buildConfiguration(CodeGenerationLaunchDelegate.java:281)
   at org.hibernate.eclipse.launch.CodeGenerationLaunchDelegate.runExporters(CodeGenerationLaunchDelegate.java:214)
   at org.hibernate.eclipse.launch.CodeGenerationLaunchDelegate.launch(CodeGenerationLaunchDelegate.java:136)
   at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:853)
   at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:703)
   at org.eclipse.debug.internal.ui.DebugUIPlugin.buildAndLaunch(DebugUIPlugin.java:866)
   at org.eclipse.debug.internal.ui.DebugUIPlugin$8.run(DebugUIPlugin.java:1069)
   at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
Caused by: java.lang.InstantiationException
   at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source)
   at java.lang.reflect.Constructor.newInstance(Unknown Source)
   at java.lang.Class.newInstance0(Unknown Source)
   at java.lang.Class.newInstance(Unknown Source)
   at org.hibernate.type.TypeFactory.heuristicType(TypeFactory.java:231)
   ... 19 more
!SUBENTRY 1 org.hibernate.eclipse.console 4 666 2009-10-07 15:59:28.625
!MESSAGE org.hibernate.MappingException: Could not instantiate Type: my.domain.my.project.YesNoType
!STACK 0
org.hibernate.MappingException: Could not instantiate Type: my.domain.my.project.YesNoType
   at org.hibernate.type.TypeFactory.heuristicType(TypeFactory.java:234)
   at org.hibernate.type.TypeFactory.heuristicType(TypeFactory.java:210)
   at org.hibernate.cfg.JDBCBinder.guessAndAlignType(JDBCBinder.java:827)
   at org.hibernate.cfg.JDBCBinder.bindColumnToSimpleValue(JDBCBinder.java:781)
   at org.hibernate.cfg.JDBCBinder.bindBasicProperty(JDBCBinder.java:773)
   at org.hibernate.cfg.JDBCBinder.bindColumnsToProperties(JDBCBinder.java:719)
   at org.hibernate.cfg.JDBCBinder.createPersistentClasses(JDBCBinder.java:182)
   at org.hibernate.cfg.JDBCBinder.readFromDatabase(JDBCBinder.java:95)
   at org.hibernate.cfg.JDBCMetaDataConfiguration.readFromJDBC(JDBCMetaDataConfiguration.java:42)
   at org.hibernate.eclipse.launch.CodeGenerationLaunchDelegate$2.execute(CodeGenerationLaunchDelegate.java:313)
   at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:64)
   at org.hibernate.console.ConsoleConfiguration.execute(ConsoleConfiguration.java:94)
   at org.hibernate.eclipse.launch.CodeGenerationLaunchDelegate.buildConfiguration(CodeGenerationLaunchDelegate.java:281)
   at org.hibernate.eclipse.launch.CodeGenerationLaunchDelegate.runExporters(CodeGenerationLaunchDelegate.java:214)
   at org.hibernate.eclipse.launch.CodeGenerationLaunchDelegate.launch(CodeGenerationLaunchDelegate.java:136)
   at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:853)
   at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:703)
   at org.eclipse.debug.internal.ui.DebugUIPlugin.buildAndLaunch(DebugUIPlugin.java:866)
   at org.eclipse.debug.internal.ui.DebugUIPlugin$8.run(DebugUIPlugin.java:1069)
   at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
Caused by: java.lang.InstantiationException
   at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source)
   at java.lang.reflect.Constructor.newInstance(Unknown Source)
   at java.lang.Class.newInstance0(Unknown Source)
   at java.lang.Class.newInstance(Unknown Source)
   at org.hibernate.type.TypeFactory.heuristicType(TypeFactory.java:231)
   ... 19 more
!SUBENTRY 1 org.hibernate.eclipse.console 4 666 2009-10-07 15:59:28.625
!MESSAGE java.lang.InstantiationException: <no message>
!STACK 0
java.lang.InstantiationException
   at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source)
   at java.lang.reflect.Constructor.newInstance(Unknown Source)
   at java.lang.Class.newInstance0(Unknown Source)
   at java.lang.Class.newInstance(Unknown Source)
   at org.hibernate.type.TypeFactory.heuristicType(TypeFactory.java:231)
   at org.hibernate.type.TypeFactory.heuristicType(TypeFactory.java:210)
   at org.hibernate.cfg.JDBCBinder.guessAndAlignType(JDBCBinder.java:827)
   at org.hibernate.cfg.JDBCBinder.bindColumnToSimpleValue(JDBCBinder.java:781)
   at org.hibernate.cfg.JDBCBinder.bindBasicProperty(JDBCBinder.java:773)
   at org.hibernate.cfg.JDBCBinder.bindColumnsToProperties(JDBCBinder.java:719)
   at org.hibernate.cfg.JDBCBinder.createPersistentClasses(JDBCBinder.java:182)
   at org.hibernate.cfg.JDBCBinder.readFromDatabase(JDBCBinder.java:95)
   at org.hibernate.cfg.JDBCMetaDataConfiguration.readFromJDBC(JDBCMetaDataConfiguration.java:42)
   at org.hibernate.eclipse.launch.CodeGenerationLaunchDelegate$2.execute(CodeGenerationLaunchDelegate.java:313)
   at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:64)
   at org.hibernate.console.ConsoleConfiguration.execute(ConsoleConfiguration.java:94)
   at org.hibernate.eclipse.launch.CodeGenerationLaunchDelegate.buildConfiguration(CodeGenerationLaunchDelegate.java:281)
   at org.hibernate.eclipse.launch.CodeGenerationLaunchDelegate.runExporters(CodeGenerationLaunchDelegate.java:214)
   at org.hibernate.eclipse.launch.CodeGenerationLaunchDelegate.launch(CodeGenerationLaunchDelegate.java:136)
   at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:853)
   at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:703)
   at org.eclipse.debug.internal.ui.DebugUIPlugin.buildAndLaunch(DebugUIPlugin.java:866)
   at org.eclipse.debug.internal.ui.DebugUIPlugin$8.run(DebugUIPlugin.java:1069)
   at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.