Darren Hartford
2004-08-12 11:29:13 UTC
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
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
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