AW: mapping DB types to Java types

"Darren Hartford" <[email protected]>
Newsgroups gmane.comp.java.ejb.middlegen.user
Message-ID <[email protected]>
I, as well as probably a lot of other people, ended up hard-coding  the Sql2Java source code in Middlegen for what you need and recompile. As an example, this is what I have at the bottom of the Sql2Java.java file:

   static {
      _preferredJavaTypeForSqlType.put(Types.TINYINT, "java.lang.Integer");
      _preferredJavaTypeForSqlType.put(Types.SMALLINT, "java.lang.Integer");
      _preferredJavaTypeForSqlType.put(Types.INTEGER, "java.lang.Integer");
      _preferredJavaTypeForSqlType.put(Types.BIGINT, "java.lang.Integer");
      _preferredJavaTypeForSqlType.put(Types.REAL, "java.lang.Float");
      _preferredJavaTypeForSqlType.put(Types.FLOAT, "java.lang.Float");
      _preferredJavaTypeForSqlType.put(Types.DOUBLE, "java.lang.Float");
      _preferredJavaTypeForSqlType.put(Types.DECIMAL, "java.math.BigDecimal");
      _preferredJavaTypeForSqlType.put(Types.NUMERIC, "java.math.BigDecimal");
      _preferredJavaTypeForSqlType.put(Types.BIT, "java.lang.Boolean");
      _preferredJavaTypeForSqlType.put(Types.CHAR, "java.lang.String");
      _preferredJavaTypeForSqlType.put(Types.VARCHAR, "java.lang.String");
      // according to resultset.gif, we should use java.io.Reader, but String is more convenient for EJB
      _preferredJavaTypeForSqlType.put(Types.LONGVARCHAR, "java.lang.String");
      _preferredJavaTypeForSqlType.put(Types.BINARY, "byte[]");
      _preferredJavaTypeForSqlType.put(Types.VARBINARY, "byte[]");
      _preferredJavaTypeForSqlType.put(Types.LONGVARBINARY, "byte[]");
      _preferredJavaTypeForSqlType.put(Types.DATE, "java.util.Date");
      _preferredJavaTypeForSqlType.put(Types.TIME, "java.util.Date");
      _preferredJavaTypeForSqlType.put(Types.TIMESTAMP, "java.util.Date");
      _preferredJavaTypeForSqlType.put(Types.CLOB, "byte[]");
      _preferredJavaTypeForSqlType.put(Types.BLOB, "byte[]");
      _preferredJavaTypeForSqlType.put(Types.ARRAY, "java.sql.Array");
      _preferredJavaTypeForSqlType.put(Types.REF, "java.sql.Ref");
      _preferredJavaTypeForSqlType.put(Types.STRUCT, "java.lang.Object");
      _preferredJavaTypeForSqlType.put(Types.JAVA_OBJECT, "java.lang.Object");
   }

Opposed to the above solution, if you are using *only* Hibernate, I would recommend getting the most recent (R5 I think) of the Hibernate-Middlegen plugin from Hibernate's site and write a class to implement JavaTypeMapper and override the "public String getPreferredJavaType(JavaColumn column)" method to have it return what you want it to. Then, if using Ant, add a property to the Middlegen/Hibernate subtask for 'javaTypeMapper=' and point it to this class (making sure the class is in the classpath).

Hope this helps you get started, in between things at the momement so this is a quick response.

-D


> 
> -----Original Message-----
> From: Jonathan Scott [mailto:[email protected]]
> Sent: Wednesday, August 11, 2004 9:25 PM
> To: [email protected]
> Subject: [Middlegen-user] mapping DB types to Java types
> 
> 
> Hi All,
> 
> Looking through the MiddleGen site, I am worried it is not at 
> all possible
> to force certain DB types to specific Java classes. 
> 
> PostgreSQL has a type "inet" which is an IP address. 
> MiddleGen does not
> understand this type and thus marks it as "Object" in our 
> hbm.xml files. We
> would like to map it to another class.
> 
> Is there any configuration file/option etc. that I could use 
> to achieve
> this? Any pointers would be more than welcome.
> 
> TIA,
> Jonathan Scott

> 


-------------------------------------------------------
SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.