-->
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: a bug? precision not taken in hbm2hbm in ant env.
PostPosted: Mon Nov 20, 2006 5:17 pm 
Regular
Regular

Joined: Wed Mar 15, 2006 1:48 pm
Posts: 91
For a simple test case, I found precision and scale aren't taken by Hibernate Tool 3.2 beta 8 with MySQL 5. Here is my test case (pay attention to "test" field),

CREATE TABLE `test` (
`id` int(5) NOT NULL,
`test` smallint(2) default NULL,
`comment` char(1) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

<hibernate-reverse-engineering>
<schema-selection match-table="test"/>
<type-mapping>
<!-- jdbc-type is name fom java.sql.Types -->
<sql-type jdbc-type="CHAR" length="1" hibernate-type="yes_no"/>
<sql-type jdbc-type="SMALLINT" hibernate-type="java.lang.Integer"/>
</type-mapping>
<!--sql-type jdbc-type="SMALLINT" precision="2" scale="0" hibernate-type="java.lang.Integer"/-->
<table name="test">
<primary-key>
<generator class="native"/>
</primary-key>
</table>
</hibernate-reverse-engineering>

Form log file I noticed this difference,

1. Without precision and attribute specified

[hibernatetool] (cfg.JDBCBinder 803 ) Building property id
[hibernatetool] (cfg.JDBCBinder 542 ) Scanning org.hibernate.cfg.reveng.TableIdentifier(test) for <version>/<timestamp> columns.
[hibernatetool] (cfg.JDBCBinder 552 ) No columns reported while scanning for <version>/<timestamp> columns in org.hibernate.cfg.reveng.TableIdentifier(test)
[hibernatetool] (reveng.OverrideRepository 312 ) columnToHibernateTypeName, <type-mapping> found: java.lang.Integer for Table: test column: test
[hibernatetool] (cfg.JDBCBinder 635 ) Sql type mismatch for Table: test column: test between DB and wanted hibernate type. Sql type set to 5(SMALLINT) instead of 4(INTEGER)
[hibernatetool] (cfg.JDBCBinder 803 ) Building property test
[hibernatetool] (reveng.OverrideRepository 312 ) columnToHibernateTypeName, <type-mapping> found: yes_no for Table: test column: comment


2. With precision and attribute specified
[hibernatetool] (cfg.JDBCBinder 803 ) Building property id
[hibernatetool] (cfg.JDBCBinder 542 ) Scanning org.hibernate.cfg.reveng.TableIdentifier(test) for <version>/<timestamp> columns.
[hibernatetool] (cfg.JDBCBinder 552 ) No columns reported while scanning for <version>/<timestamp> columns in org.hibernate.cfg.reveng.TableIdentifier(test)
[hibernatetool] (cfg.JDBCBinder 803 ) Building property test
[hibernatetool] (reveng.OverrideRepository 312 ) columnToHibernateTypeName, <type-mapping> found: yes_no for Table: test column: comment

Is it a bug?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 20, 2006 8:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
hmmm...could you put this in jira and i'll look into it.

either we have a bug or precision/scaling is reported wrong by mysql.

at least i need to improve the logging so you actually can see why its finding/rejecting the reveng.xml setup.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 20, 2006 11:34 pm 
Regular
Regular

Joined: Wed Mar 15, 2006 1:48 pm
Posts: 91
Done at http://opensource.atlassian.com/project ... se/HBX-828


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 11:56 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Looks to be a bug in the OverrideRepository.getPreferredHibernateType method and the TypeMappingKey inner class. The TypeMappingKey has no references to precision and scale, only length, so precision and scale will never be analyzed for matches in the reveng file.

As a workaround for now, implement the columnToHibernateTypeName method in a custom Strategy class thusly:

Code:
public class Strategy extends DelegatingReverseEngineeringStrategy {


   @Override
   public String columnToHibernateTypeName(TableIdentifier table,
         String columnName, int sqlType, int length, int precision,
         int scale, boolean nullable, boolean generatedIdentifier) {
      // Generate column mappings as Integer if the column type is SMALLINT and the precision is 2
      if ((sqlType == java.sql.Types.SMALLINT ) && (precision == 2)) {
         return "java.lang.Integer";
      } else {
         return super.columnToHibernateTypeName(table, columnName, sqlType,
               length, precision, scale, nullable, generatedIdentifier);
      }

   }

}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 1:05 pm 
Regular
Regular

Joined: Wed Mar 15, 2006 1:48 pm
Posts: 91
I added one line output code and it shows me precision is 19 instead of 2 for test column even though I defined precision 2 for it in db. Any idea or any workround?

@Override
public String columnToHibernateTypeName(TableIdentifier table, String columnName, int sqlType, int length, int precision, int scale, boolean nullable, boolean generatedIdentifier) {
System.out.println("columnName/type/precision ------- "+columnName+"/"+sqlType+"/"+precision);
if ((sqlType == java.sql.Types.SMALLINT) && (precision == 2)) {
System.out.println("Got it!");
return "java.lang.Integer";
} else {
return super.columnToHibernateTypeName(table, columnName, sqlType, length, precision, scale, nullable, generatedIdentifier);
}

Console log,

[hibernatetool] columnName/type/precision ------- id/4/19
[hibernatetool] (cfg.JDBCBinder 803 ) Building property id
[hibernatetool] (cfg.JDBCBinder 542 ) Scanning org.hibernate.cfg.reveng.TableIdentifier(test) for <version>/<timestamp> columns.
[hibernatetool] (cfg.JDBCBinder 552 ) No columns reported while scanning for <version>/<timestamp> columns in org.hibernate.cfg.reveng.TableIdentifier(test)
[hibernatetool] columnName/type/precision ------- test/5/19
[hibernatetool] (cfg.JDBCBinder 803 ) Building property test
[hibernatetool] columnName/type/precision ------- comment/1/19


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 1:09 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Now that is funky! I'll take a gander at it and see what I can come up with. Lemme run a reveng on my data (400+ tables) and see what the reported precisions are, that'll give me an idea as to whether it is the tool code or the jdbc driver.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 1:20 pm 
Regular
Regular

Joined: Wed Mar 15, 2006 1:48 pm
Posts: 91
I don't think it is a driver issue. I used the same driver(3.1.12) with beta 3 or 4(I can't remember exact tool version) and it worked fine. I have tried both latest 5.0.3 driver and 3.1.12 driver, and I got the same precision=19.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 1:37 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Aha. The JDBCToHibernateTypeHelper does not list Types.SMALLINT as having scale or precision (only DECIMAL, NUMERIC, REAL, FLOAT, and DOUBLE have these attributes at the moment)! This may or may not be a bug, but I will JIRA it with a patch and hopefully it will get included in the next iteration. in the meantime, I would use some sort of workaround, even if you have to modify the columnToHibernateType to specify the return type based on the table and/or column name. If you want all SMALLINTs to be java.lang.Integers then just remove the precision conditional.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 1:48 pm 
Regular
Regular

Joined: Wed Mar 15, 2006 1:48 pm
Posts: 91
Confusion with customized strategy is,

1. why do I have length 19 for all columns?
2. how can comment column be converted properly from char(1) to yes_no type even it shows length 19 here? where did this conversion occur?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 2:01 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
1) 19 is the default precision for column types with no precision
2) the reported precision for the comment column is 19, the length will be 1


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 2:18 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Patch added to JIRA HBX-828


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.