-->
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: Middlegen problem
PostPosted: Wed Nov 19, 2003 10:05 pm 
Regular
Regular

Joined: Tue Nov 11, 2003 7:35 pm
Posts: 63
I have two questions. I think they may be bugs as well:

1) In the Middlegen GUI, I select a Key Generator, say "sequence" and enter the sequence name in the box beside the dropdown. I then go on to some other table and click on it. If I would go back to the original box it goes back to saying "assigned", not "sequence" as I just changed it to. I tried changing it a few times with the same result. What do I need to do to make my selection "stick"?

The funny thing is if I would click "Generate" the generated xml file DOES have a sequence, not "assigned". The resulting properties file also says sequence. But the GUI does not reflect this. Also, if I restart my GUI and select "generate", the code has "assigned" in it, not seqence.


2) I have seen middlegen guess at java mapping types that just seem to make no sense. For example, I have a NUMBER(10) that was mapped to a java "short" primitive. A number with potentially 10 digits cannot obviously fit in a short. An integer would be more appropriate. In additon, that column was nullible. So when I tried to load an object that had a null for that column, Hibernate threw an exception that it trying to assign NULL to a primitive object. An Integer data type is the appropriate choice.

I also had another column that was a NUMBER but it was mapped to a java boolean type. This is obvioulsly incorrect.

This happened to several of the mapping objects I tried to generate. How smart is middlegen in trying to use the correct datatype? Is there a way to configure it to be smarted, or use a default type?


For both these questions, I am using Windows XP, java 1.4 and an Oracle 8 database.

Thank you,
Daniel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2003 10:38 pm 
Regular
Regular

Joined: Tue Nov 11, 2003 7:35 pm
Posts: 63
oh yeah, I am also using the R3 plugin.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 10:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Quote:
1) In the Middlegen GUI, I select a Key Generator, say "sequence" and enter the sequence name in the box beside the dropdown. I then go on to some other table and click on it. If I would go back to the original box it goes back to saying "assigned", not "sequence" as I just changed it to. I tried changing it a few times with the same result. What do I need to do to make my selection "stick"?


Sorry - I cannot get this to happen for me. It works fine.

Quote:
2) I have seen middlegen guess at java mapping types that just seem to make no sense. For example, I have a NUMBER(10) that was mapped to a java "short" primitive. A number with potentially 10 digits cannot obviously fit in a short. An integer would be more appropriate.


You have a valid point here, middlegen core provides the options. The GUI does allow you to set it appropriately. I will give it some though and see if I can add some smarts to the plugin with a set of reasonable defaults.

Quote:
In additon, that column was nullible. So when I tried to load an object that had a null for that column, Hibernate threw an exception that it trying to assign NULL to a primitive object. An Integer data type is the appropriate choice.


Yes this is one example where the plugin can help to provide a more reasonable default choice.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 2:24 pm 
Regular
Regular

Joined: Tue Nov 11, 2003 7:35 pm
Posts: 63
After a bit of debugging I found the problem for #1 (where the original box went back to saying "assigned" even though I pick "sequence" or whatever.

In the JHibernateTableSettingsPanel.setKeyGenerator(), the code is like this:

Code:
_beanKeyGeneratorCombo.setModel(cModel);
_beanKeyGeneratorField.setEditable(!_currentTable.isCompositeKey());
_beanKeyGeneratorField.setText(_currentTable.getKeyGeneratorArg());
_beanKeyGeneratorCombo.setSelectedItem(_currentTable.getKeyGenerator());


What was happening is that when the _beanKeyGeneratorCombo.setModel(cModel) call was performed, the keyGeneratorComboAction.actionPerformed() method is triggered, which calls the HibernateTable.setKeyGenerator() method with the value in _beanKeyGeneratorCombo.getSelectedItem(). At this point though the _beanKeyGeneratorCombo.getSelectedItem() only returns "assigned" since it was not initialized with the correct value. This in turn set the HibernateTable with "assigned" and the original value was lost.

I hope that was not too confusing. Anyhow, to fix this problem I changed the code to this:

Code:
String keyGeneratorValue = _currentTable.getKeyGenerator();
_beanKeyGeneratorCombo.setModel(cModel);
_beanKeyGeneratorField.setEditable(!_currentTable.isCompositeKey());
_beanKeyGeneratorField.setText(_currentTable.getKeyGeneratorArg());
_beanKeyGeneratorCombo.setSelectedItem(keyGeneratorValue);


Basically, I saved the original value in a variable before setmodel was called, and reassigned that same value back afterwards.

Do you see any problem with my code?

I am not sure how to submit this change. Could you please do it for me or provide instructions how to do so?

Thank you,
Daniel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 11:10 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
I see no problem with the change. I have taken note of it and will make the to the code ASAP. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 10:47 pm 
Regular
Regular

Joined: Tue Nov 11, 2003 7:35 pm
Posts: 63
Thanks.

BTW, where is the CVS located for the hibernate plugin code? I found the code in the middlegen cvs tree, but no file in the hibernate plugin has been updated on that tree for more than 4 weeks. Is there some other CVS tree where the plugin development takes place?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 10:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The development has been occuring in a Branch but we are moving it back to HEAD. Its not stable as yet but we are preparing for a middlegen2.0 final. I have not updated the CVS as yet with the small changes that I have prepared (as yet). Soon.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2003 5:27 pm 
Regular
Regular

Joined: Tue Nov 11, 2003 7:35 pm
Posts: 63
David,

I created some functionality into the Hibernate plugin to solve the problem #2 specified above. Basically, a user can specify and optional class to customize how middlegen dermines the java type from the sql type. (This replaces the Sql2Java.getPreferredJavaType() call of Middlegen).

The optional class must implement the following interface:

Code:
package middlegen.plugins.hibernate.interfaces;

import middlegen.javax.JavaColumn;

public interface JavaTypeMapper {
  public String getPreferredJavaType(JavaColumn column);
}


This class would be specified as one of the attributes in the hibernate plugin ant task definition. For example:

Code:
<hibernate
  .....
  javaTypeMapper="my.package.MyJavaTypeMapper"
/>


One example class I found works well with Hibernate follows. Basically, this solves a major problem I have been having that if a column is nullible, a primitive is not a good mapping, but the wrapper class should be the one used. Also, I prefer to always use Integers and not Shorts or Bytes for small numbers. Also, I do not want NUMBER(1) to map to boolean, as is the default in Middlegen. Because I have my own class to determine the type I can specify all this in a custom way.

Code:
package my.package.MyJavaTypeMapper;

import java.sql.Types;

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

public class MyJavaTypeMapper implements JavaTypeMapper {

    public MyJavaTypeMapper() {
    }

    public String getPreferredJavaType(JavaColumn column) {
      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
      if (nullable) {
         if (result.equals("int") ||
               result.equals("short") ||
               result.equals("long") ||
               result.equals("byte") ||
               result.equals("float") ||
               result.equals("boolean") ||
               result.equals("double")) {
            result = Sql2Java.getClassForPrimitive(result);
         }
      }
      return result;
   }
}


I am not sure how to submit my code changes. Basically some lines are added to the HibernatePlugin, HibernateColumnSettingsPanel and HibernateColumn classes. Also one interface JavaTypeMapper is added. In all it is not many lines of code changed. Could you please give me instructions where to post and/or send my code to? I am sure others would find this useful as well.

Thank you,
Daniel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2003 6:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Great - I have updated CVS the other day. How about sending me the patches against the latest version and I will check it out. I like the idea.
email david@hibernate.org


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 29, 2003 1:49 pm 
Regular
Regular

Joined: Tue Nov 11, 2003 7:35 pm
Posts: 63
One small addition to the sample mapper class. Please change the following code:

Code:
      // if the column is nullable make sure it is not a primitive
      if (nullable) {


to:

Code:
      // if the column is nullable or part of a pk make sure it is not a primitive
      if (nullable || column.isPk()) {


It is not the best situation if a pk is a primitive type, as there are no Session.load() methods for primitives. It would also be nec. to specify an "unsaved value" for a primitive. To get around all this it is better to use wrapper classes instread of primitives for pk columns.

Daniel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 12:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
In CVS.


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.