Reputation: 1406
I am trying to predict the sql
type from java
type for any store. I know that spring
+ hibernate
is internally doing it using dialect
to do operations like auto-ddl
and to create scripts.
How can I do this in java ?
When I say java type, I meant the data type of attribute of my jpa
entity.
I don't want to do the manual mapping between the types and looking it up instead I want the stores corresponding dialect
to do it for me at runtime.Is there any way to do this ?
Is there any way to do this?
Since hibernate is doing it with the help of the dialect, there should be something I can use.
Upvotes: 2
Views: 1564
Reputation: 1406
I investigated how Hibernate
was doing it using dialect
and came up with the code.
First I mapped the java type
to sql type integer value
MAPPING = new HashMap<String, Integer>();
MAPPING.put("short", Types.SMALLINT);
MAPPING.put("java.lang.Short", Types.SMALLINT);
MAPPING.put("int", Types.INTEGER);
MAPPING.put("java.lang.Integer", Types.INTEGER);
MAPPING.put("float", Types.FLOAT);
MAPPING.put("java.lang.Float", Types.FLOAT);
MAPPING.put("long", Types.BIGINT);
MAPPING.put("java.lang.Long", Types.BIGINT);
MAPPING.put("BigInteger", Types.BIGINT);
MAPPING.put("double", Types.DECIMAL);
MAPPING.put("java.lang.Double", Types.DECIMAL);
MAPPING.put("java.math.BigDecimal", Types.DECIMAL);
MAPPING.put("BigDecimal", Types.DECIMAL);
MAPPING.put("Integer", Types.DECIMAL);
MAPPING.put("Decimal", Types.DECIMAL);
MAPPING.put("boolean", Types.SMALLINT);
MAPPING.put("java.lang.Boolean", Types.SMALLINT);
MAPPING.put("Date", Types.TIMESTAMP);
MAPPING.put("char", Types.VARCHAR);
MAPPING.put("java.lang.Character", Types.VARCHAR);
MAPPING.put("byte", Types.CHAR);
MAPPING.put("java.lang.Byte", Types.CHAR);
MAPPING.put("java.lang.String", Types.VARCHAR);
MAPPING.put("String", Types.VARCHAR);
MAPPING.put("java.lang.Object", Types.JAVA_OBJECT);
Then I used the mapping
//the type value varies with the dialect of the store
registerColumnType(-7, "bit");
registerColumnType(16, "boolean");
registerColumnType(-6, "tinyint");
registerColumnType(5, "smallint");
registerColumnType(4, "integer");
registerColumnType(-5, "bigint");
registerColumnType(6, "float($p)");
registerColumnType(8, "double precision");
registerColumnType(2, "numeric($p,$s)");
registerColumnType(7, "real");
registerColumnType(91, "date");
registerColumnType(92, "time");
registerColumnType(93, "timestamp");
registerColumnType(-3, "bit varying($l)");
registerColumnType(-4, "bit varying($l)");
registerColumnType(2004, "blob");
registerColumnType(1, "char($l)");
registerColumnType(12, "varchar($l)");
registerColumnType(-1, "varchar($l)");
registerColumnType(2005, "clob");
registerColumnType(-15, "nchar($l)");
registerColumnType(-9, "nvarchar($l)");
registerColumnType(-16, "nvarchar($l)");
registerColumnType(2011, "nclob");
registerColumnType(-7, "SMALLINT");
registerColumnType(-6, "SMALLINT");
registerColumnType(-5, "NUMERIC(21,0)");
registerColumnType(5, "SMALLINT");
registerColumnType(1, "CHARACTER(1)");
registerColumnType(8, "DOUBLE PRECISION");
registerColumnType(6, "FLOAT");
registerColumnType(7, "REAL");
registerColumnType(4, "INTEGER");
registerColumnType(2, "NUMERIC(21,$l)");
registerColumnType(3, "NUMERIC(21,$l)");
registerColumnType(91, "DATE");
registerColumnType(92, "TIME");
registerColumnType(93, "TIMESTAMP");
registerColumnType(12, "CHARACTER($l)");
registerColumnType(2004, "BLOB($l)");
And the look-up code went like this
public String toSQLType(String java_type){
String type=columnTypes.get(JAVA_DATABASE_MAPPING.get(java_type));
return type;
}
Upvotes: 1