Java SQL Type getPreferredHibernateType(int sqlType, int size, int precision, int scale, boolean nullable, boolean generatedIdentifier)

Here you can find the source of getPreferredHibernateType(int sqlType, int size, int precision, int scale, boolean nullable, boolean generatedIdentifier)

Description

get Preferred Hibernate Type

License

Open Source License

Declaration

public static String getPreferredHibernateType(int sqlType, int size, int precision, int scale,
            boolean nullable, boolean generatedIdentifier) 

Method Source Code

//package com.java2s;

import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class Main {
    /** The Map containing the preferred conversion type values. */
    private static final Map<Integer, String[]> PREFERRED_HIBERNATETYPE_FOR_SQLTYPE = new HashMap<Integer, String[]>();

    public static String getPreferredHibernateType(int sqlType, int size, int precision, int scale,
            boolean nullable, boolean generatedIdentifier) {
        boolean returnNullable = nullable || generatedIdentifier;
        if ((sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) && scale <= 0) { // <= 
            if (precision == 1) {
                // NUMERIC(1) is a often used idiom for storing boolean thus providing it out of the box.
                return returnNullable ? Boolean.class.getName() : "boolean"; //$NON-NLS-1$
            } else if (precision < 3) {
                return returnNullable ? Byte.class.getName() : "byte"; //$NON-NLS-1$
            } else if (precision < 5) {
                return returnNullable ? Short.class.getName() : "short"; //$NON-NLS-1$
            } else if (precision < 10) {
                return returnNullable ? Integer.class.getName() : "int"; //$NON-NLS-1$
            } else if (precision < 19) {
                return returnNullable ? Long.class.getName() : "long"; //$NON-NLS-1$
            } else {
                return "big_decimal"; //$NON-NLS-1$
            }//from www .  j  a v  a2s.c o  m
        }

        if (sqlType == Types.CHAR && size > 1) {
            return "string"; //$NON-NLS-1$
        }

        String[] result = (String[]) PREFERRED_HIBERNATETYPE_FOR_SQLTYPE.get(new Integer(sqlType));

        if (result == null) {
            return null;
        } else if (returnNullable) {
            return result[1];
        } else {
            return result[0];
        }
    }
}

Related

  1. getObjectType(Object param)
  2. getObjectType(Object param)
  3. getOdaTypeName(int odaTypeCode)
  4. getParamArray(String signature, int numParam, boolean returnAllParams)
  5. getPartitionSizeValidationError(int colType, String column, String partitionSize)
  6. getPreparedStatementSetMethodMap()
  7. getPrimitiveInstance(Type type, String valueString)
  8. getReturnClass(String typeName)
  9. getSemestre(Integer actionformation_id, Connection connection, String actionformation_libellecourt)