View Javadoc

    1   /*
    2    * joey-gen and its relative products are published under the terms
    3    * of the Apache Software License.
    4    *
    5    * Created on 2004/11/24 17:58:41
    6    */
    7   package org.asyrinx.joey.gen.hibernate;
    8
    9   import java.sql.Types;
    10
    11  import org.apache.commons.lang.StringUtils;
    12  import org.asyrinx.joey.gen.jdbc.JdbcUtils;
    13
    14  import net.sf.hibernate.Hibernate;
    15  import net.sf.hibernate.type.NullableType;
    16
    17  /***
    18   * @author takeshi
    19   */
    20  public class HibernateUtils {
    21
    22      /***
    23       *
    24       */
    25      public HibernateUtils() {
    26          super();
    27      }
    28
    29      public static NullableType getHibernateType(String sqlTypeName, int columnSize, int decimalDigits) {
    30          final int sqlType = JdbcUtils.toJdbcType(sqlTypeName);
    31          return (sqlType == JdbcUtils.UNKNOWN_TYPE) ? null : getHibernateType(sqlType, columnSize, decimalDigits);
    32      }
    33
    34      public static NullableType getHibernateType(int sqlType, int columnSize, int decimalDigits) {
    35          //logger.debug("sqlType=" + sqlType);
    36          //logger.debug("columnSize=" + columnSize);
    37          //logger.debug("decimalDigits=" + decimalDigits);
    38          NullableType rv = Hibernate.SERIALIZABLE;
    39          if (sqlType == Types.CHAR || sqlType == Types.VARCHAR) {
    40              rv = Hibernate.STRING;
    41          } else if (sqlType == Types.LONGVARCHAR) {
    42              rv = Hibernate.TEXT;
    43          } else if (sqlType == Types.BLOB) {
    44              rv = Hibernate.BLOB;
    45          } else if (sqlType == Types.CLOB) {
    46              rv = Hibernate.CLOB;
    47          } else if (sqlType == Types.FLOAT || sqlType == Types.REAL) {
    48              rv = Hibernate.FLOAT;
    49          } else if (sqlType == Types.INTEGER) {
    50              rv = Hibernate.INTEGER;
    51          } else if (sqlType == Types.DOUBLE) {
    52              rv = Hibernate.DOUBLE;
    53          } else if (sqlType == Types.DATE) {
    54              rv = Hibernate.DATE;
    55          } else if (sqlType == Types.TIMESTAMP) {
    56              rv = Hibernate.TIMESTAMP;
    57          } else if (sqlType == Types.TIME) {
    58              rv = Hibernate.TIME;
    59          } else if (sqlType == Types.BOOLEAN) {
    60              rv = Hibernate.BOOLEAN;
    61          } else if (sqlType == Types.SMALLINT) {
    62              rv = Hibernate.SHORT;
    63          } else if (sqlType == Types.BIT) {
    64              rv = Hibernate.BOOLEAN;
    65          } else if (sqlType == Types.BIGINT) {
    66              rv = Hibernate.LONG;
    67          } else if (sqlType == Types.NUMERIC || sqlType == Types.DECIMAL) {
    68              if (decimalDigits == 0) {
    69                  if (columnSize == 1) {
    70                      rv = Hibernate.BYTE;
    71                  } else if (columnSize < 5) {
    72                      rv = Hibernate.SHORT;
    73                  } else if (columnSize < 10) {
    74                      rv = Hibernate.INTEGER;
    75                  } else {
    76                      rv = Hibernate.LONG;
    77                  }
    78              } else {
    79                  if (columnSize < 9) {
    80                      rv = Hibernate.FLOAT;
    81                  } else {
    82                      rv = Hibernate.DOUBLE;
    83                  }
    84              }
    85          }
    86          return rv;
    87      }
    88
    89      public static String toHibernateCascade(String cascade) {
    90          if (StringUtils.isEmpty(cascade))
    91              return "none";
    92          if ("none".equals(cascade))
    93              return "none";
    94          if ("all".equals(cascade))
    95              return "all";
    96          if ("update".equals(cascade))
    97              return "save-update";
    98          if ("delete".equals(cascade))
    99              return "delete";
    100         return "none";
    101     }
    102
    103 }