Example usage for org.hibernate.type TimestampType TimestampType

List of usage examples for org.hibernate.type TimestampType TimestampType

Introduction

In this page you can find the example usage for org.hibernate.type TimestampType TimestampType.

Prototype

public TimestampType() 

Source Link

Usage

From source file:com.opengamma.util.db.hibernate.types.PersistentInstant.java

License:Open Source License

@SuppressWarnings("deprecation")
public Object nullSafeGet(ResultSet resultSet, String name) throws SQLException {
    java.sql.Timestamp value = (java.sql.Timestamp) (new TimestampType()).nullSafeGet(resultSet, name);
    if (value == null) {
        return null;
    }//  w w  w  . ja v  a 2 s.  c o  m
    return DbDateUtils.fromSqlTimestamp(value);
}

From source file:com.opengamma.util.db.hibernate.types.PersistentInstant.java

License:Open Source License

@SuppressWarnings("deprecation")
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index)
        throws HibernateException, SQLException {
    if (value == null) {
        s_logger.debug("INSTANT -> TIMESTAMP : NULL -> NULL");
        (new TimestampType()).nullSafeSet(preparedStatement, null, index);
    } else {//w w w  .j  a  v  a2 s .c  o  m
        s_logger.debug("INSTANT -> TIMESTAMP : {}   ->  {}", value,
                DbDateUtils.toSqlTimestamp((Instant) value));
        (new TimestampType()).nullSafeSet(preparedStatement, DbDateUtils.toSqlTimestamp((Instant) value),
                index);
    }
}

From source file:org.unitime.timetable.backup.SessionRestore.java

License:Open Source License

public void create(TableData.Table table)
        throws InstantiationException, IllegalAccessException, DocumentException {
    ClassMetadata metadata = iHibSessionFactory.getClassMetadata(table.getName());
    if (metadata == null)
        return;//from  ww w .  j  av  a 2  s.co  m
    PersistentClass mapping = _RootDAO.getConfiguration().getClassMapping(table.getName());
    Map<String, Integer> lengths = new HashMap<String, Integer>();
    for (String property : metadata.getPropertyNames()) {
        Type type = metadata.getPropertyType(property);
        if (type instanceof StringType)
            for (Iterator<?> i = mapping.getProperty(property).getColumnIterator(); i.hasNext();) {
                Object o = i.next();
                if (o instanceof Column) {
                    Column column = (Column) o;
                    lengths.put(property, column.getLength());
                }
                break;
            }
    }
    iProgress.setPhase(metadata.getEntityName().substring(metadata.getEntityName().lastIndexOf('.') + 1) + " ["
            + table.getRecordCount() + "]", table.getRecordCount());
    for (TableData.Record record : table.getRecordList()) {
        iProgress.incProgress();
        Object object = metadata.getMappedClass().newInstance();
        for (String property : metadata.getPropertyNames()) {
            TableData.Element element = null;
            for (TableData.Element e : record.getElementList())
                if (e.getName().equals(property)) {
                    element = e;
                    break;
                }
            if (element == null)
                continue;
            Object value = null;
            Type type = metadata.getPropertyType(property);
            if (type instanceof PrimitiveType) {
                if (type instanceof BooleanType) {
                    value = new Boolean("true".equals(element.getValue(0)));
                } else if (type instanceof ByteType) {
                    value = Byte.valueOf(element.getValue(0));
                } else if (type instanceof CharacterType) {
                    value = Character.valueOf(element.getValue(0).charAt(0));
                } else if (type instanceof DoubleType) {
                    value = Double.valueOf(element.getValue(0));
                } else if (type instanceof FloatType) {
                    value = Float.valueOf(element.getValue(0));
                } else if (type instanceof IntegerType) {
                    value = Integer.valueOf(element.getValue(0));
                } else if (type instanceof LongType) {
                    value = Long.valueOf(element.getValue(0));
                } else if (type instanceof ShortType) {
                    value = Short.valueOf(element.getValue(0));
                }
            } else if (type instanceof DateType) {
                try {
                    value = new SimpleDateFormat("dd MMMM yyyy", Localization.getJavaLocale())
                            .parse(element.getValue(0));
                } catch (ParseException e) {
                    value = new DateType().fromStringValue(element.getValue(0));
                }
            } else if (type instanceof TimestampType) {
                value = new TimestampType().fromStringValue(element.getValue(0));
            } else if (type instanceof StringType) {
                value = element.getValue(0);
                Integer len = lengths.get(property);
                if (len != null && value.toString().length() > len) {
                    message("Value is  too long, truncated (property " + metadata.getEntityName() + "."
                            + property + ", length " + len + ")", record.getId());
                    value = value.toString().substring(0, len);
                }
            } else if (type instanceof BinaryType) {
                value = ByteString.copyFromUtf8(element.getValue(0)).toByteArray();
            } else if (type instanceof CustomType && type.getReturnedClass().equals(Document.class)) {
                value = new SAXReader().read(new StringReader(element.getValue(0)));
            } else if (type instanceof EntityType) {
            } else if (type instanceof CollectionType) {
            } else {
                message("Unknown type " + type.getClass().getName() + " (property " + metadata.getEntityName()
                        + "." + property + ", class " + type.getReturnedClass() + ")", record.getId());
            }
            if (value != null)
                metadata.setPropertyValue(object, property, value);
        }
        add(new Entity(metadata, record, object, record.getId()));
    }
}