diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/JdbcTypeJavaClassMappings.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/JdbcTypeJavaClassMappings.java index 1bb11378f2..b0a3bb885b 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/JdbcTypeJavaClassMappings.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/JdbcTypeJavaClassMappings.java @@ -85,6 +85,7 @@ public class JdbcTypeJavaClassMappings { jdbcJavaClassMappings.put( String.class, Types.VARCHAR ); jdbcJavaClassMappings.put( BigDecimal.class, Types.NUMERIC ); jdbcJavaClassMappings.put( Boolean.class, Types.BIT ); + jdbcJavaClassMappings.put( Short.class, Types.SMALLINT ); jdbcJavaClassMappings.put( Integer.class, Types.INTEGER ); jdbcJavaClassMappings.put( Long.class, Types.BIGINT ); jdbcJavaClassMappings.put( Float.class, Types.REAL ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/SmallIntToShortClassMappingTest.java b/hibernate-core/src/test/java/org/hibernate/test/type/SmallIntToShortClassMappingTest.java new file mode 100644 index 0000000000..39c4795545 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/type/SmallIntToShortClassMappingTest.java @@ -0,0 +1,87 @@ +/* + * 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.test.type; + +import javax.persistence.AttributeConverter; +import javax.persistence.Column; +import javax.persistence.Convert; +import javax.persistence.Converter; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.dialect.H2Dialect; +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.junit.Assert.assertEquals; + +@RequiresDialect(H2Dialect.class) +public class SmallIntToShortClassMappingTest extends BaseEntityManagerFunctionalTestCase { + + @Override + public Class[] getAnnotatedClasses() { + return new Class[] { + Event.class, + }; + } + + @Test + @TestForIssue(jiraKey = "HHH-12115") + public void testShortType() { + doInJPA( this::entityManagerFactory, entityManager -> { + Event event = new Event(); + event.id = 1; + event.registrationNumber = "123"; + + entityManager.persist( event ); + } ); + + doInJPA( this::entityManagerFactory, entityManager -> { + Event event = entityManager.find( Event.class, (short) 1 ); + assertEquals( "123", event.registrationNumber ); + } ); + } + + @Entity(name = "Event") + @Table(name = "event") + public static class Event { + + @Id + @Column(columnDefinition = "SMALLINT") + private Short id; + + @Column(columnDefinition = "SMALLINT") + @Convert(converter = ShortToString.class) + private String registrationNumber; + } + + @Converter + public static class ShortToString implements AttributeConverter { + @Override + public Short convertToDatabaseColumn(String attribute) { + if ( attribute == null ) { + return null; + } + + return Short.valueOf( attribute ); + } + + @Override + public String convertToEntityAttribute(Short dbData) { + if ( dbData == null ) { + return null; + } + + return String.valueOf( dbData ); + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/type/descriptor/sql/JdbcTypeJavaClassMappingsTest.java b/hibernate-core/src/test/java/org/hibernate/type/descriptor/sql/JdbcTypeJavaClassMappingsTest.java new file mode 100644 index 0000000000..c1d091fcef --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/type/descriptor/sql/JdbcTypeJavaClassMappingsTest.java @@ -0,0 +1,42 @@ +/* + * 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.descriptor.sql; + +import java.sql.Types; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * @author Tiger Wang + */ +public class JdbcTypeJavaClassMappingsTest { + + @Before + public void before() throws Exception { + } + + @After + public void after() throws Exception { + } + + @Test + public void testDetermineJdbcTypeCodeForJavaClass() throws Exception { + int jdbcTypeCode = JdbcTypeJavaClassMappings.INSTANCE.determineJdbcTypeCodeForJavaClass( Short.class ); + assertEquals( jdbcTypeCode, Types.SMALLINT ); + } + + @Test + public void testDetermineJavaClassForJdbcTypeCodeTypeCode() throws Exception { + Class javaClass = JdbcTypeJavaClassMappings.INSTANCE.determineJavaClassForJdbcTypeCode( Types.SMALLINT ); + assertEquals( javaClass, Short.class ); + } + +}