Discussion:
AW: [Middlegen-user] mapping DB types to Java types
Darren Hartford
2004-08-12 11:29:13 UTC
Permalink
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-----
Sent: Wednesday, August 11, 2004 9:25 PM
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
Jonathan Scott
2004-08-13 00:30:02 UTC
Permalink
Darren,

Thanks for the information. I have tried playing around with it a little. I just tried to confirm my overriding of the getPreferredType() was working, and that was just not going at all.

I noticed the code below as well. However, my problem comes about from the fact that Types does not contain an IP Address type. So, how I would be able to accomplish the below, in the specific case of PG's "inet" type, and map it to a class of my own, is not exactly apparent to me.

Perhaps I need to hard code for each field, in a script, to modify my properties file. Would be preferred to just be able to override getPreferredType(), instead of doing something funky like sed'ing the properties file. I'd prefer to just be able to play around with my sql files, run ant, and see the classes all built properly and my tests running all green.

Jonathan Scott


On Thu, 12 Aug 2004 09:28:23 -0400
Post by Darren Hartford
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-----
Sent: Wednesday, August 11, 2004 9:25 PM
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
_______________________________________________
middlegen-user mailing list
https://lists.sourceforge.net/lists/listinfo/middlegen-user
--
Jonathan Scott, Programmer, Vanten K.K.
***@vanten.com Tel: 03-5919-0266
http://www.vanten.com Fax: 03-5919-0267
Darren Hartford
2004-08-13 10:38:03 UTC
Permalink
Hi Jonathan,
If I remember correctly, the 'Types' specified in Sql2Java are integer types. I would recommend doing some debugging to see what Pgsql lists it as in Middlegen. This may be a case that it will not have a specific type for Inet and it will only identify as an Object. You may have to change the preferred Type.Object to map to a String or whatever you may want to use those IP addresses as in Hibernate. This doesn't help if you have true Objects in your database, but at least it's a work-around to keep you moving forward!

my two cents,
-D


-----Original Message-----
From: Jonathan Scott [mailto:***@vanten.com]
Sent: Thu 8/12/2004 10:29 PM
To: middlegen-***@lists.sourceforge.net
Cc:
Subject: Re: AW: [Middlegen-user] mapping DB types to Java types



Darren,

Thanks for the information. I have tried playing around with it a little. I just tried to confirm my overriding of the getPreferredType() was working, and that was just not going at all.

I noticed the code below as well. However, my problem comes about from the fact that Types does not contain an IP Address type. So, how I would be able to accomplish the below, in the specific case of PG's "inet" type, and map it to a class of my own, is not exactly apparent to me.

Perhaps I need to hard code for each field, in a script, to modify my properties file. Would be preferred to just be able to override getPreferredType(), instead of doing something funky like sed'ing the properties file. I'd prefer to just be able to play around with my sql files, run ant, and see the classes all built properly and my tests running all green.

Jonathan Scott

Loading...