Author: steve.ebersole(a)jboss.com
Date: 2010-09-30 11:18:21 -0400 (Thu, 30 Sep 2010)
New Revision: 20759
Added:
core/trunk/core/src/main/java/org/hibernate/type/descriptor/JdbcTypeNameMapper.java
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/BasicOperationsTest.java
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/Grade.java
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/SomeEntity.java
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/SomeOtherEntity.java
Modified:
core/trunk/core/src/main/java/org/hibernate/type/BasicTypeRegistry.java
core/trunk/core/src/main/java/org/hibernate/type/DateType.java
core/trunk/core/src/main/java/org/hibernate/type/TimeType.java
core/trunk/core/src/main/java/org/hibernate/type/descriptor/sql/BasicBinder.java
core/trunk/testsuite/src/test/resources/log4j.properties
Log:
HHH-5606 - Incorrect standard type regsitrations to date/time
Modified: core/trunk/core/src/main/java/org/hibernate/type/BasicTypeRegistry.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/type/BasicTypeRegistry.java 2010-09-30
13:15:55 UTC (rev 20758)
+++ core/trunk/core/src/main/java/org/hibernate/type/BasicTypeRegistry.java 2010-09-30
15:18:21 UTC (rev 20759)
@@ -144,7 +144,7 @@
log.debug( "Adding type registration {} -> {}", key, type );
final Type old = registry.put( key, type );
if ( old != null && old != type ) {
- log.debug( " Overrides previous {}", old );
+ log.info( "Type registration [{}] overrides previous : {}", key, old );
}
}
}
Modified: core/trunk/core/src/main/java/org/hibernate/type/DateType.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/type/DateType.java 2010-09-30 13:15:55 UTC
(rev 20758)
+++ core/trunk/core/src/main/java/org/hibernate/type/DateType.java 2010-09-30 15:18:21 UTC
(rev 20759)
@@ -49,10 +49,18 @@
}
@Override
- protected boolean registerUnderJavaType() {
- return true;
+ public String[] getRegistrationKeys() {
+ return new String[] {
+ getName(),
+ java.sql.Date.class.getName()
+ };
}
+// @Override
+// protected boolean registerUnderJavaType() {
+// return true;
+// }
+
public String objectToSQLString(Date value, Dialect dialect) throws Exception {
final java.sql.Date jdbcDate = java.sql.Date.class.isInstance( value )
? ( java.sql.Date ) value
Modified: core/trunk/core/src/main/java/org/hibernate/type/TimeType.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/type/TimeType.java 2010-09-30 13:15:55 UTC
(rev 20758)
+++ core/trunk/core/src/main/java/org/hibernate/type/TimeType.java 2010-09-30 15:18:21 UTC
(rev 20759)
@@ -50,10 +50,18 @@
}
@Override
- protected boolean registerUnderJavaType() {
- return true;
+ public String[] getRegistrationKeys() {
+ return new String[] {
+ getName(),
+ java.sql.Time.class.getName()
+ };
}
+ // @Override
+// protected boolean registerUnderJavaType() {
+// return true;
+// }
+
public String objectToSQLString(Date value, Dialect dialect) throws Exception {
Time jdbcTime = Time.class.isInstance( value )
? ( Time ) value
Added:
core/trunk/core/src/main/java/org/hibernate/type/descriptor/JdbcTypeNameMapper.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/type/descriptor/JdbcTypeNameMapper.java
(rev 0)
+++
core/trunk/core/src/main/java/org/hibernate/type/descriptor/JdbcTypeNameMapper.java 2010-09-30
15:18:21 UTC (rev 20759)
@@ -0,0 +1,81 @@
+/*
+ * 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;
+
+import java.lang.reflect.Field;
+import java.sql.Types;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.hibernate.HibernateException;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class JdbcTypeNameMapper {
+ private static final Logger log = LoggerFactory.getLogger( JdbcTypeNameMapper.class );
+ private static Map<Integer,String> JDBC_TYPE_MAP = buildJdbcTypeMap();
+
+ private static Map<Integer, String> buildJdbcTypeMap() {
+ HashMap<Integer, String> map = new HashMap<Integer, String>();
+ Field[] fields = Types.class.getFields();
+ if ( fields == null ) {
+ throw new HibernateException( "Unexpected problem extracting JDBC type mapping
codes from java.sql.Types" );
+ }
+ for ( Field field : fields ) {
+ try {
+ final int code = field.getInt( null );
+ String old = map.put(
+ Integer.valueOf( code ),
+ field.getName()
+ );
+ if ( old != null ) {
+ log.info( "java.sql.Types mapped the same code [" + code + "]
multiple times; was [" + old + "]; now [" + field.getName() + "]"
);
+ }
+ }
+ catch ( IllegalAccessException e ) {
+ throw new HibernateException( "Unable to access JDBC type mapping [" +
field.getName() + "]", e );
+ }
+ }
+ return Collections.unmodifiableMap( map );
+ }
+
+ public static String getTypeName(int code) {
+ return getTypeName( Integer.valueOf( code ) );
+ }
+
+ public static String getTypeName(Integer code) {
+ String name = JDBC_TYPE_MAP.get( code );
+ if ( name == null ) {
+ return "UNKNOWN(" + code + ")";
+ }
+ return name;
+ }
+}
Modified:
core/trunk/core/src/main/java/org/hibernate/type/descriptor/sql/BasicBinder.java
===================================================================
---
core/trunk/core/src/main/java/org/hibernate/type/descriptor/sql/BasicBinder.java 2010-09-30
13:15:55 UTC (rev 20758)
+++
core/trunk/core/src/main/java/org/hibernate/type/descriptor/sql/BasicBinder.java 2010-09-30
15:18:21 UTC (rev 20759)
@@ -29,6 +29,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.hibernate.type.descriptor.JdbcTypeNameMapper;
import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
@@ -41,6 +42,9 @@
public abstract class BasicBinder<J> implements ValueBinder<J> {
private static final Logger log = LoggerFactory.getLogger( BasicBinder.class );
+ private static final String BIND_MSG_TEMPLATE = "binding parameter [%d] as [%s] -
%s";
+ private static final String NULL_BIND_MSG_TEMPLATE = "binding parameter [%d] as
[%s] - <null>";
+
private final JavaTypeDescriptor<J> javaDescriptor;
private final SqlTypeDescriptor sqlDescriptor;
@@ -62,11 +66,26 @@
*/
public final void bind(PreparedStatement st, J value, int index, WrapperOptions options)
throws SQLException {
if ( value == null ) {
- log.trace( "binding [null] to parameter [{}]", index );
+ if ( log.isTraceEnabled() ) {
+ log.trace(
+ String.format(
+ NULL_BIND_MSG_TEMPLATE,
+ index,
+ JdbcTypeNameMapper.getTypeName( sqlDescriptor.getSqlType() )
+ )
+ );
+ }
st.setNull( index, sqlDescriptor.getSqlType() );
}
else {
- log.trace( "binding [{}] to parameter [{}]",
getJavaDescriptor().extractLoggableRepresentation( value ), index );
+ log.trace(
+ String.format(
+ BIND_MSG_TEMPLATE,
+ index,
+ JdbcTypeNameMapper.getTypeName( sqlDescriptor.getSqlType() ),
+ getJavaDescriptor().extractLoggableRepresentation( value )
+ )
+ );
doBind( st, value, index, options );
}
}
Added:
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/BasicOperationsTest.java
===================================================================
---
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/BasicOperationsTest.java
(rev 0)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/BasicOperationsTest.java 2010-09-30
15:18:21 UTC (rev 20759)
@@ -0,0 +1,108 @@
+/*
+ * 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.test.annotations.dataTypes;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Date;
+
+import org.hibernate.Session;
+import org.hibernate.jdbc.Work;
+import org.hibernate.test.annotations.TestCase;
+import org.hibernate.type.descriptor.JdbcTypeNameMapper;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class BasicOperationsTest extends TestCase {
+ @Override
+ protected Class<?>[] getAnnotatedClasses() {
+ return new Class[] { SomeEntity.class, SomeOtherEntity.class };
+ }
+
+ public void testCreateAndDelete() {
+ Date now = new Date();
+
+ Session s = openSession();
+ s.doWork(
+ new Work() {
+ public void execute(Connection connection) throws SQLException {
+ // id -> java.util.Date (DATE - becase of explicit TemporalType)
+ validateColumn( connection, "ID", java.sql.Types.DATE );
+
+ // timeData -> java.sql.Time (TIME)
+ validateColumn( connection, "TIMEDATA", java.sql.Types.TIME );
+
+ // tsData -> java.sql.Timestamp (TIMESTAMP)
+ validateColumn( connection, "TSDATA", java.sql.Types.TIMESTAMP );
+ }
+
+ private void validateColumn(Connection connection, String columnName, int
expectedJdbcTypeCode)
+ throws SQLException {
+ ResultSet columnInfo = connection.getMetaData().getColumns( null, null,
"SOMEENTITY", columnName );
+ assertTrue( columnInfo.next() );
+ int dataType = columnInfo.getInt( "DATA_TYPE" );
+ columnInfo.close();
+ assertEquals( columnName, JdbcTypeNameMapper.getTypeName(expectedJdbcTypeCode),
JdbcTypeNameMapper.getTypeName(dataType) );
+ }
+
+ }
+ );
+ s.beginTransaction();
+ SomeEntity someEntity = new SomeEntity( now );
+ SomeOtherEntity someOtherEntity = new SomeOtherEntity(1);
+ s.save( someEntity );
+ s.save( someOtherEntity );
+ s.getTransaction().commit();
+ s.close();
+
+ s = openSession();
+ s.beginTransaction();
+ s.delete( someEntity );
+ s.delete( someOtherEntity );
+ s.getTransaction().commit();
+ s.close();
+ }
+
+ private Byte[] generateByteArray() {
+ final byte[] bytes = "I'll be back".getBytes();
+ final Byte[] wrappedBytes = new Byte[ bytes.length ];
+ for ( int i = 0, max = bytes.length; i < max; i++ ) {
+ wrappedBytes[i] = Byte.valueOf( bytes[i] );
+ }
+ return wrappedBytes;
+ }
+
+ private Character[] generateCharacterArray() {
+ final char[] chars = "I'll be back".toCharArray();
+ final Character[] wrappedChars = new Character[ chars.length ];
+ for ( int i = 0, max = chars.length; i < max; i++ ) {
+ wrappedChars[i] = Character.valueOf( chars[i] );
+ }
+ return wrappedChars;
+ }
+}
Added:
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/Grade.java
===================================================================
---
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/Grade.java
(rev 0)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/Grade.java 2010-09-30
15:18:21 UTC (rev 20759)
@@ -0,0 +1,37 @@
+/*
+ * 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.test.annotations.dataTypes;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public enum Grade {
+ A,
+ B,
+ C,
+ D,
+ F
+}
Added:
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/SomeEntity.java
===================================================================
---
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/SomeEntity.java
(rev 0)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/SomeEntity.java 2010-09-30
15:18:21 UTC (rev 20759)
@@ -0,0 +1,99 @@
+/*
+ * 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.test.annotations.dataTypes;
+
+import java.util.Date;
+import javax.persistence.Access;
+import javax.persistence.AccessType;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Lob;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+@Entity
+(a)Access(AccessType.FIELD)
+public class SomeEntity {
+ @Id
+ @Temporal(TemporalType.DATE)
+ private java.util.Date id;
+ private java.sql.Time timeData;
+ private java.sql.Timestamp tsData;
+ @Lob
+ private Byte[] byteData;
+ private Character[] charData;
+
+ public SomeEntity() {
+ }
+
+ public SomeEntity(Date id) {
+ this.id = id;
+ }
+
+ public java.util.Date getId() {
+ return id;
+ }
+
+ public void setId(java.util.Date id) {
+ this.id = id;
+ }
+
+ public Character[] getCharData() {
+ return charData;
+ }
+
+ public void setCharData(Character[] charData) {
+ this.charData = charData;
+ }
+
+ public java.sql.Time getTimeData() {
+ return timeData;
+ }
+
+ public void setTimeData(java.sql.Time timeData) {
+ this.timeData = timeData;
+ }
+
+ public java.sql.Timestamp getTsData() {
+ return tsData;
+ }
+
+ public void setTsData(java.sql.Timestamp tsData) {
+ this.tsData = tsData;
+ }
+
+ public Byte[] getByteData() {
+ return byteData;
+ }
+
+ public void setByteData(Byte[] byteData) {
+ this.byteData = byteData;
+ }
+
+}
Added:
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/SomeOtherEntity.java
===================================================================
---
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/SomeOtherEntity.java
(rev 0)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/dataTypes/SomeOtherEntity.java 2010-09-30
15:18:21 UTC (rev 20759)
@@ -0,0 +1,150 @@
+/*
+ * 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.test.annotations.dataTypes;
+
+import javax.persistence.Access;
+import javax.persistence.AccessType;
+import javax.persistence.Basic;
+import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
+import javax.persistence.Id;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+@Entity
+(a)Access(AccessType.FIELD)
+public class SomeOtherEntity {
+ @Id
+ protected int id;
+ protected boolean booleanData;
+ protected byte byteData;
+ protected char characterData;
+ protected short shortData;
+ protected int integerData;
+ protected long longData;
+ protected double doubleData;
+ protected float floatData;
+ @Enumerated(EnumType.STRING)
+ protected Grade grade;
+
+
+ public SomeOtherEntity()
+ {
+ }
+
+ public SomeOtherEntity(int id)
+ {
+ this.id = id;
+ }
+
+ public SomeOtherEntity(
+ int id,
+ boolean booleanData,
+ byte byteData,
+ char characterData,
+ short shortData,
+ int integerData,
+ long longData,
+ double doubleData,
+ float floatData) {
+ this.id = id;
+ this.booleanData = booleanData;
+ this.byteData = byteData;
+ this.characterData = characterData;
+ this.shortData = shortData;
+ this.integerData = integerData;
+ this.longData = longData;
+ this.doubleData = doubleData;
+ this.floatData = floatData;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public Character getCharacterData() {
+ return characterData;
+ }
+
+ public void setCharacterData(Character characterData) {
+ this.characterData = characterData;
+ }
+
+ public Short getShortData() {
+ return shortData;
+ }
+
+ public void setShortData(Short shortData) {
+ this.shortData = shortData;
+ }
+
+ public Integer getIntegerData() {
+ return integerData;
+ }
+
+ public void setIntegerData(Integer integerData) {
+ this.integerData = integerData;
+ }
+
+ public Long getLongData() {
+ return longData;
+ }
+
+ public void setLongData(Long longData) {
+ this.longData = longData;
+ }
+
+ public Double getDoubleData() {
+ return doubleData;
+ }
+
+ public void setDoubleData(Double doubleData) {
+ this.doubleData = doubleData;
+ }
+
+ public Float getFloatData() {
+ return floatData;
+ }
+
+ public void setFloatData(Float floatData) {
+ this.floatData = floatData;
+ }
+
+ public Grade getGrade() {
+ return grade;
+ }
+
+ public void setGrade(Grade grade) {
+ this.grade = grade;
+ }
+
+}
Modified: core/trunk/testsuite/src/test/resources/log4j.properties
===================================================================
--- core/trunk/testsuite/src/test/resources/log4j.properties 2010-09-30 13:15:55 UTC (rev
20758)
+++ core/trunk/testsuite/src/test/resources/log4j.properties 2010-09-30 15:18:21 UTC (rev
20759)
@@ -11,4 +11,6 @@
log4j.logger.org.hibernate.hql.ast.QueryTranslatorImpl=trace
log4j.logger.org.hibernate.hql.ast.HqlSqlWalker=trace
log4j.logger.org.hibernate.hql.ast.SqlGenerator=trace
-log4j.logger.org.hibernate.hql.ast.AST=trace
\ No newline at end of file
+log4j.logger.org.hibernate.hql.ast.AST=trace
+log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=trace
+log4j.logger.org.hibernate.type.BasicTypeRegistry=trace
\ No newline at end of file