HHH-12115 - attribute converter add mapping of Java Short type to JDBC SMALLINT type
This commit is contained in:
parent
f0d49c5be4
commit
6b1f3e51a9
|
@ -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 );
|
||||
|
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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<String, Short> {
|
||||
@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 );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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 );
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue