David,
I created some functionality into the Hibernate plugin to solve the problem #2 specified above. Basically, a user can specify and optional class to customize how middlegen dermines the java type from the sql type. (This replaces the Sql2Java.getPreferredJavaType() call of Middlegen).
The optional class must implement the following interface:
Code:
package middlegen.plugins.hibernate.interfaces;
import middlegen.javax.JavaColumn;
public interface JavaTypeMapper {
public String getPreferredJavaType(JavaColumn column);
}
This class would be specified as one of the attributes in the hibernate plugin ant task definition. For example:
Code:
<hibernate
.....
javaTypeMapper="my.package.MyJavaTypeMapper"
/>
One example class I found works well with Hibernate follows. Basically, this solves a major problem I have been having that if a column is nullible, a primitive is not a good mapping, but the wrapper class should be the one used. Also, I prefer to always use Integers and not Shorts or Bytes for small numbers. Also, I do not want NUMBER(1) to map to boolean, as is the default in Middlegen. Because I have my own class to determine the type I can specify all this in a custom way.
Code:
package my.package.MyJavaTypeMapper;
import java.sql.Types;
import middlegen.javax.JavaColumn;
import middlegen.javax.Sql2Java;
import middlegen.plugins.hibernate.interfaces.JavaTypeMapper;
public class MyJavaTypeMapper implements JavaTypeMapper {
public MyJavaTypeMapper() {
}
public String getPreferredJavaType(JavaColumn column) {
int sqlType = column.getSqlType();
int size = column.getSize();
int decimalDigits = column.getDecimalDigits();
boolean nullable = column.isNullable();
String result = null;
if ((sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) && decimalDigits == 0) {
if (size < 10) {
result = "int";
}
else if (size < 19) {
result = "long";
}
else {
result = "java.math.BigDecimal";
}
}
else {
// if not any of above get the default
result = Sql2Java.getPreferredJavaType(sqlType, size, decimalDigits);
if (result == null) {
result = "java.lang.Object";
}
}
// if the column is nullable make sure it is not a primitive
if (nullable) {
if (result.equals("int") ||
result.equals("short") ||
result.equals("long") ||
result.equals("byte") ||
result.equals("float") ||
result.equals("boolean") ||
result.equals("double")) {
result = Sql2Java.getClassForPrimitive(result);
}
}
return result;
}
}
I am not sure how to submit my code changes. Basically some lines are added to the HibernatePlugin, HibernateColumnSettingsPanel and HibernateColumn classes. Also one interface JavaTypeMapper is added. In all it is not many lines of code changed. Could you please give me instructions where to post and/or send my code to? I am sure others would find this useful as well.
Thank you,
Daniel