javadatabasehibernate

Hibernate reverse engineering INTEGER with different sizes


I am working with an existing MySQL database where all INT column types were all set as INT(11).

Using Hibernate, the corresponding generated classes global values are being set as Integer, which will become a problem when a row value has more than 9 digits.

So, I created a reverse engineering file like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.org/dtd/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>
    <type-mapping>
        <sql-type jdbc-type="BIGINT" hibernate-type="Long"></sql-type>
        <sql-type jdbc-type="INTEGER" hibernate-type="Long"
            length="12"></sql-type>
        <sql-type jdbc-type="INTEGER" hibernate-type="Long"
            length="11"></sql-type>
        <sql-type jdbc-type="INTEGER" hibernate-type="Integer"
            length="8"></sql-type>
        <sql-type jdbc-type="INTEGER" hibernate-type="Integer"
            length="9"></sql-type>
        <sql-type jdbc-type="INTEGER" hibernate-type="Long"
            length="10"></sql-type>


        <sql-type jdbc-type="SMALLINT" hibernate-type="Integer"></sql-type>
    </type-mapping>


    <table-filter match-name="DATABASECHANGELOG"
        exclude="true">
    </table-filter>
    <table-filter match-name="DATABASECHANGELOGLOCK"
        exclude="true">
    </table-filter>

</hibernate-reverse-engineering>

The reason I did this, is because that database also has MEDIUMINT and SMALLINT columns and I would like to keep those as Integer since Long is not needed in those cases.

The problem is that the resulting classes are like this:

INT - Integer (I was expecting Long)
MEDIUMINT - Integer (I know that this Java SQL type doesn't exist that is why I adopted this INTEGER length strategy)
SMALLINT - Integer

What it seems is that "length" property is being discarded.

Any ideas?


Solution

  • INT(11) is still int reference to this answer, (number) is for padding,

    from the hibernate doc, it says length for VARCHAR, precision for NUMERIC.

    Reference of java.sql.Types as oracle document.

    The <type-mapping> section specifies how the JDBC types found in the database should be mapped to Hibernate types. e.g. java.sql.Types.VARCHAR with a length of 1 should be mapped to the Hibernate type yes_no, or java.sql.Types.NUMERIC should generally just be converted to the Hibernate type long.

    <type-mapping>
     <sql-type
      jdbc-type="integer value or name from java.sql.Types"
      length="a numeric value"
      precision="a numeric value"
      scale="a numeric value"
      not-null="true|false"  
      hibernate-type="hibernate type name"  
     />
    </type-mapping>
    

    in Oracle database NUMBER[(precision [, scale])] is range effect.