|
Hibernate example source code file (JdbcTimestampTypeDescriptor.java)
The Hibernate JdbcTimestampTypeDescriptor.java source code/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2010, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ package org.hibernate.type.descriptor.java; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import org.hibernate.HibernateException; import org.hibernate.type.descriptor.WrapperOptions; /** * TODO : javadoc * * @author Steve Ebersole */ public class JdbcTimestampTypeDescriptor extends AbstractTypeDescriptor<Date> { public static final JdbcTimestampTypeDescriptor INSTANCE = new JdbcTimestampTypeDescriptor(); public static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss"; public static class TimestampMutabilityPlan extends MutableMutabilityPlan<Date> { public static final TimestampMutabilityPlan INSTANCE = new TimestampMutabilityPlan(); public Date deepCopyNotNull(Date value) { if ( value instanceof Timestamp ) { Timestamp orig = (Timestamp) value; Timestamp ts = new Timestamp( orig.getTime() ); ts.setNanos( orig.getNanos() ); return ts; } else { Date orig = value; return new Date( orig.getTime() ); } } } public JdbcTimestampTypeDescriptor() { super( Date.class, TimestampMutabilityPlan.INSTANCE ); } public String toString(Date value) { return new SimpleDateFormat( TIMESTAMP_FORMAT ).format( value ); } public Date fromString(String string) { try { return new Timestamp( new SimpleDateFormat( TIMESTAMP_FORMAT ).parse( string ).getTime() ); } catch ( ParseException pe) { throw new HibernateException( "could not parse timestamp string" + string, pe ); } } @Override public boolean areEqual(Date one, Date another) { if ( one == another ) { return true; } if ( one == null || another == null) { return false; } long t1 = one.getTime(); long t2 = another.getTime(); boolean oneIsTimestamp = Timestamp.class.isInstance( one ); boolean anotherIsTimestamp = Timestamp.class.isInstance( another ); int n1 = oneIsTimestamp ? ( (Timestamp) one ).getNanos() : 0; int n2 = anotherIsTimestamp ? ( (Timestamp) another ).getNanos() : 0; if ( t1 != t2 ) { return false; } if ( oneIsTimestamp && anotherIsTimestamp ) { // both are Timestamps int nn1 = n1 % 1000000; int nn2 = n2 % 1000000; return nn1 == nn2; } else { // at least one is a plain old Date return true; } } @Override public int extractHashCode(Date value) { return Long.valueOf( value.getTime() / 1000 ).hashCode(); } @SuppressWarnings({ "unchecked" }) public <X> X unwrap(Date value, Class Other Hibernate examples (source code examples)Here is a short list of links related to this Hibernate JdbcTimestampTypeDescriptor.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.