0

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);
}
2
  • 1
    I am unable to reproduce your issue with mysql-connector-java-5.1.46. For me, .getColumns returns TINYINT, 3, and 0 for TYPE_NAME, COLUMN_SIZE, and DECIMAL_DIGITS respectively. Can you provide a minimal reproducible example along with specific version information (driver version, server version, etc.)? Commented Jul 10, 2018 at 13:02
  • What's the MySQL version? What's JDBC driver are you using? Commented Jul 14, 2018 at 12:15

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.