-->
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.  [ 11 posts ] 
Author Message
 Post subject: Problem with javaTypeMapper
PostPosted: Wed Mar 24, 2004 6:22 am 
Newbie

Joined: Wed Mar 24, 2004 6:18 am
Posts: 15
Hi!

I'm posting a message that I sent to the middlegen-user mailing list. I was suggested to send it to this forum, so here it is:

I'm having problems using the Hibernate plugin
(http://www.hibernate.org/98.html).

I want middlegen to generate only non-primitive types for the hbm.xml files. As
far as I know, this can be made through the javaTypeMapper attribute of the
<hibernate> tag inside build.xml.

I created a class similar to this one:

http://cvs.middlegen.sourceforge.net/vi ... in&rev=1.2

but returning always object types (just as this one does for primary keys, but
for all fields).

I've referenced my class this way in build.xml:

<hibernate
destination="${build.gen-src.dir}"
package="${name}.persistence"
genXDocletTags="true"
genIntergratedCompositeKeys="false"
javaTypeMapper="mypackage.MyTypeMapper"
/>

And I placed it inside a jar under /lib directory.

The problem is that middlegen seems to overlook it. No matter what I return in
getPreferredJavaType method, the hbm.xml files are generated always the same
way.

Middlegen finds the class, because if I remove it from the /lib directory, I get
a ClassNotFound exception when I run "ant hbm2java".

Any ideas of what am I doing wrong?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 24, 2004 10:56 am 
Regular
Regular

Joined: Tue Nov 11, 2003 7:35 pm
Posts: 63
!) Might I suggest you put System.out statements to make sure that the class is actually being called.

2) Did you change the class much from the version in cvs? If so could you please post your changes?

Assuming your class is being called and is very similar to the version in cvs, my guess is that you are getting an Object from this code:

result = Sql2Java.getPreferredJavaType(sqlType, size, decimalDigits);
if (result == null) {
result = "java.lang.Object";
}

For some reason result is returning null. Perhaps do System.out to see what values are being returned in Middlegen for sqlType, size and decimalDigits, and see if there are what you were expecting.

Daniel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 24, 2004 12:18 pm 
Newbie

Joined: Wed Mar 24, 2004 6:18 am
Posts: 15
Hi, drosenbaum, thanks for your answer. I've put several System.out.println statements but nothing is displayed. However, as I already put on my previous post, apparently the class is being called, because if I remove it, I get a ClassNotFound exception. It's weird.

"My" class is the same as the CVS one, but removing this if clause:

if (nullable || column.isPk())

to return always a non-primitive type.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 24, 2004 12:28 pm 
Newbie

Joined: Wed Mar 24, 2004 6:18 am
Posts: 15
More information: I had put the println statements inside the getPreferredJavaType method and nothing was being displayed, but if when I put a println inside the class constructor, it's getting printed. I'm totally puzzled with this.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 24, 2004 1:27 pm 
Regular
Regular

Joined: Tue Nov 11, 2003 7:35 pm
Posts: 63
Could you please post your class?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 24, 2004 1:47 pm 
Newbie

Joined: Wed Mar 24, 2004 6:18 am
Posts: 15
drosenbaum wrote:
Could you please post your class?


Here it is. As I said, it's the same as the one in the CVS. I've just deleted the last if clause and renamed the package and class name to make sure Middlegen wasn't invoking the original one. It also has two println statements. I put the class inside a jar named mypackage.jar, which I place under Middlegen lib dir.

Code:
package mypackage;

import java.sql.Types;

import middlegen.javax.JavaColumn;
import middlegen.javax.Sql2Java;
import middlegen.plugins.hibernate.interfaces.JavaTypeMapper;

/**
* This class provides (one possible) implementation of a Java Type mapper for
* the Middlegen plugin. This class is configured by the ant script by providing
* the HibernateJavaTypeMapper as a preference to using Middlegen's exclusively.
* Once a mapping has been determined or updated by the user through the GUI
* then this mapper will not override the result.
*
* @author Daniel Rosenbaum and David Channon
* @created 24 December 2003
* @version 2.1
*/
public class MyHibernateJavaTypeMapper implements JavaTypeMapper {

   /**
   * Describe what the HibernateJavaTypeMapper constructor does
   *
   * @todo-javadoc Write javadocs for constructor
   */
   public MyHibernateJavaTypeMapper() {
      System.out.println("This will be printed");
   }


   /**
   * Gets the PreferredJavaType attribute of the HibernateJavaTypeMapper object
   *
   * @param column Describe what the parameter does
   * @return The PreferredJavaType value
   * @todo-javadoc Write javadocs for method parameter
   */
   public String getPreferredJavaType(JavaColumn column) {
      
        System.out.println("This won't be printed");
      
     int sqlType = column.getSqlType();
     int size = column.getSize();
     int decimalDigits = column.getDecimalDigits();
     boolean nullable = column.isNullable();

     String result = null;
     if ((sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) && decimalDigits == 0) {
       if (size < 10) {
         result = "int";
       }
       else if (size < 19) {
         result = "long";
       }
       else {
         result = "java.math.BigDecimal";
       }
     }
     else {
       // if not any of above get the default
       result = Sql2Java.getPreferredJavaType(sqlType, size, decimalDigits);
       if (result == null) {
         result = "java.lang.Object";
       }
     }

     // if the column is nullable make sure it is not a primitive.
     // Its best practice not to use primitives in primary keys.
       if ("int".equals(result) ||
            "short".equals(result) ||
            "long".equals(result) ||
            "byte".equals(result) ||
            "float".equals(result) ||
            "boolean".equals(result) ||
            "double".equals(result)) {
         result = Sql2Java.getClassForPrimitive(result);
       }
     return result;
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 24, 2004 1:50 pm 
Regular
Regular

Joined: Tue Nov 11, 2003 7:35 pm
Posts: 63
Hmm it is strange. Which version of Middlegen and plugin are you using?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 24, 2004 1:57 pm 
Regular
Regular

Joined: Tue Nov 11, 2003 7:35 pm
Posts: 63
Actually, I think I got it. The javaTypeMapper is only called the first time you run middlegen. Subsequent times, since the columns are already in the prefs file, middlegen uses the values in the prefs file and does not determine new types.

To solve this, delete your prefs file and run middlegen again.

Daniel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 25, 2004 3:33 am 
Newbie

Joined: Wed Mar 24, 2004 6:18 am
Posts: 15
Thanks drosenbaum! I've just done that and now it's working properly, all my fields have non primitive types.

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 9:58 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Sorry I have been off line (and still am) because my ADSL modem blew up. Now arguing about warantry...

Anyway, to confirm what Daniel said. The preferences file stores the current state so the mapper is used only the first time. Thus what ever you selected (through the GUI or was present previously) is maintained.

You can delete the complete preferences file or delete the entries that represent the type mapping(s).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 10:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
BTW: The comment for the JavaTypeMapper does (sort of) detail this behaviour.


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