org.hibernate.type.CalendarType 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.util.Calendar;
import java.util.Comparator;
import java.util.GregorianCalendar;
import javax.persistence.TemporalType;
import org.hibernate.QueryException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.model.domain.AllowableTemporalParameterType;
import org.hibernate.type.descriptor.java.CalendarTypeDescriptor;
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 Calendar}
*
* @author Gavin King
* @author Steve Ebersole
*/
public class CalendarType
extends AbstractSingleColumnStandardBasicType
implements VersionType, AllowableTemporalParameterType {
public static final CalendarType INSTANCE = new CalendarType();
public CalendarType() {
super( TimestampTypeDescriptor.INSTANCE, CalendarTypeDescriptor.INSTANCE );
}
@Override
public String getName() {
return "calendar";
}
@Override
public String[] getRegistrationKeys() {
return new String[] { getName(), Calendar.class.getName(), GregorianCalendar.class.getName() };
}
@Override
public Calendar next(Calendar current, SharedSessionContractImplementor session) {
return seed( session );
}
@Override
public Calendar seed(SharedSessionContractImplementor session) {
return Calendar.getInstance();
}
@Override
public Comparator getComparator() {
return getJavaTypeDescriptor().getComparator();
}
@Override
public AllowableTemporalParameterType resolveTemporalPrecision(
TemporalType temporalPrecision,
TypeConfiguration typeConfiguration) {
switch ( temporalPrecision ) {
case TIMESTAMP: {
return this;
}
case DATE: {
return CalendarDateType.INSTANCE;
}
case TIME: {
return CalendarTimeType.INSTANCE;
}
}
throw new QueryException( "Calendar type cannot be treated using `" + temporalPrecision.name() + "` precision" );
}
}