org.hibernate.type.TimestampType Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-core Show documentation
Show all versions of hibernate-core Show documentation
Hibernate's core ORM functionality
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.type;
import java.sql.Timestamp;
import java.util.Comparator;
import java.util.Date;
import javax.persistence.TemporalType;
import org.hibernate.HibernateException;
import org.hibernate.QueryException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.model.domain.AllowableTemporalParameterType;
import org.hibernate.type.descriptor.java.JdbcTimestampTypeDescriptor;
import org.hibernate.type.descriptor.sql.TimestampTypeDescriptor;
import org.hibernate.type.spi.TypeConfiguration;
/**
* A type that maps between {@link java.sql.Types#TIMESTAMP TIMESTAMP} and {@link java.sql.Timestamp}
*
* @author Gavin King
* @author Steve Ebersole
*/
public class TimestampType
extends AbstractSingleColumnStandardBasicType
implements VersionType, LiteralType, AllowableTemporalParameterType {
public static final TimestampType INSTANCE = new TimestampType();
public TimestampType() {
super( TimestampTypeDescriptor.INSTANCE, JdbcTimestampTypeDescriptor.INSTANCE );
}
@Override
public String getName() {
return "timestamp";
}
@Override
public String[] getRegistrationKeys() {
return new String[] { getName(), Timestamp.class.getName(), java.util.Date.class.getName() };
}
@Override
public Date next(Date current, SharedSessionContractImplementor session) {
return seed( session );
}
@Override
public Date seed(SharedSessionContractImplementor session) {
return new Timestamp( System.currentTimeMillis() );
}
@Override
public Comparator getComparator() {
return getJavaTypeDescriptor().getComparator();
}
@Override
public String objectToSQLString(Date value, Dialect dialect) throws Exception {
final Timestamp ts = Timestamp.class.isInstance( value )
? ( Timestamp ) value
: new Timestamp( value.getTime() );
// TODO : use JDBC date literal escape syntax? -> {d 'date-string'} in yyyy-mm-dd hh:mm:ss[.f...] format
return StringType.INSTANCE.objectToSQLString( ts.toString(), dialect );
}
@Override
public Date fromStringValue(String xml) throws HibernateException {
return fromString( xml );
}
@Override
public AllowableTemporalParameterType resolveTemporalPrecision(
TemporalType temporalPrecision,
TypeConfiguration typeConfiguration) {
switch ( temporalPrecision ) {
case TIMESTAMP: {
return this;
}
case DATE: {
return DateType.INSTANCE;
}
case TIME: {
return TimeType.INSTANCE;
}
}
throw new QueryException( "Timestamp type cannot be treated using `" + temporalPrecision.name() + "` precision" );
}
}