Ankush Kapoor
Ankush Kapoor

Reputation: 543

Tinyint returning as BIT in DatabaseMetaData JAVA

MySql tinyint is returning as BIT in java and column size is returning as null. For other data types, it is working fine. Any Solution for this?

Class clsObj = Class.forName(className);
AbstractEntityPersister classMetadata = (AbstractEntityPersister) getSessionFactory().getClassMetadata(clsObj);
String[] properties = classMetadata.getPropertyNames();
String tableName = classMetadata.getTableName();

Map<String, String> dbFieldToPojoFieldMap = new HashMap<String, String>();
for (String prop : properties) {
    String[] names = classMetadata.getPropertyColumnNames(prop);
    dbFieldToPojoFieldMap.put(names[0], prop);
}

DatabaseMetaData meta = con.getMetaData();
ResultSet rsColumns = meta.getColumns(null, null, tableName, null);

while (rsColumns.next()) {
    Map<String, String> columnMetaData = new HashMap<String, String>();
    String columnName = rsColumns.getString(Constants.COLUMN_NAME);
    String columnType = rsColumns.getString(Constants.TYPE_NAME);
    String columnSize = rsColumns.getString(Constants.COLUMN_SIZE);
    String decimalDigits = rsColumns.getString(Constants.DECIMAL_DIGITS);
}

Upvotes: 0

Views: 521

Answers (2)

mcanvar
mcanvar

Reputation: 475

Thanks to the answer of @NatFar & @blainwag, I've solved my situation. I also wanted to add an answer for those trying to replicate tables via ReplicaDB. By default, it converts TINYINT to BIN by using UNHEX() function. To prevent this you can add the JDBC option at the end of your connection string like this:

--source-connect "jdbc:mysql://localhost:3306/db?tinyInt1isBit=false"

Upvotes: 0

blairwag
blairwag

Reputation: 31

We had a similar problem. Looks like newer versions of MySQL (namely post 5.0.6, like 8) have defined a BIT column type that is an alias for TINYINT(1). Additionally, BOOL and BOOLEAN have been introduced. To resolve this issue for us, we decided to use the java connection string option "tinyInt1isBit=false" to get our correctly defined TINYINT columns reported correctly. We don't use BIT. Hope this helps.

Upvotes: 3

Related Questions