Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ private static ConcurrentHashMap<Class, Integer> buildJavaClassToJdbcTypeCodeMap
workMap.put( BigDecimal.class, Types.NUMERIC );
workMap.put( BigInteger.class, Types.NUMERIC );
workMap.put( Boolean.class, Types.BIT );
workMap.put( Byte.class, Types.TINYINT );
workMap.put( Short.class, Types.SMALLINT );
workMap.put( Integer.class, Types.INTEGER );
workMap.put( Long.class, Types.BIGINT );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,52 @@ public void testBasicTimestampUsage() {
}
}

@Test
@TestForIssue(jiraKey = "HHH-14021")
public void testBasicByteUsage() {
Configuration cfg = new Configuration();
cfg.addAttributeConverter( EnumToByteConverter.class, false );
cfg.addAnnotatedClass( Tester4.class );
cfg.setProperty( AvailableSettings.HBM2DDL_AUTO, "create-drop" );
cfg.setProperty( AvailableSettings.GENERATE_STATISTICS, "true" );

SessionFactory sf = cfg.buildSessionFactory();

try {
Session session = sf.openSession();
session.beginTransaction();
session.save( new Tester4( 1L, "George", 150, ConvertibleEnum.DEFAULT ) );
session.getTransaction().commit();
session.close();

sf.getStatistics().clear();
session = sf.openSession();
session.beginTransaction();
session.get( Tester4.class, 1L );
session.getTransaction().commit();
session.close();
assertEquals( 0, sf.getStatistics().getEntityUpdateCount() );

session = sf.openSession();
session.beginTransaction();
Tester4 t4 = (Tester4) session.get( Tester4.class, 1L );
t4.convertibleEnum = ConvertibleEnum.VALUE;
session.getTransaction().commit();
session.close();

session = sf.openSession();
session.beginTransaction();
t4 = (Tester4) session.get( Tester4.class, 1L );
assertEquals( ConvertibleEnum.VALUE, t4.convertibleEnum );
session.delete( t4 );
session.getTransaction().commit();
session.close();
}
finally {
sf.close();
}
}

@Test
@TestForIssue(jiraKey = "HHH-8866")
public void testEnumConverter() {
Expand Down Expand Up @@ -392,8 +438,8 @@ public void testEnumConverter() {
StandardServiceRegistryBuilder.destroy( ssr );
}
}



// Entity declarations used in the test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -440,6 +486,8 @@ public static class Tester4 {
private String name;
@Convert( converter = IntegerToVarcharConverter.class )
private Integer code;
@Convert( converter = EnumToByteConverter.class )
private ConvertibleEnum convertibleEnum;

public Tester4() {
}
Expand All @@ -449,6 +497,13 @@ public Tester4(Long id, String name, Integer code) {
this.name = name;
this.code = code;
}

public Tester4(Long id, String name, Integer code, ConvertibleEnum convertibleEnum) {
this.id = id;
this.name = name;
this.code = code;
this.convertibleEnum = convertibleEnum;
}
}

// This class is for mimicking an Instant from Java 8, which a converter might convert to a java.sql.Timestamp
Expand Down Expand Up @@ -609,4 +664,17 @@ public Instant convertToEntityAttribute(Timestamp dbData) {
return Instant.fromJavaMillis( dbData.getTime() );
}
}

@Converter( autoApply = true )
public static class EnumToByteConverter implements AttributeConverter<ConvertibleEnum, Byte> {
@Override
public Byte convertToDatabaseColumn(ConvertibleEnum attribute) {
return attribute == null ? null : (byte) attribute.ordinal();
}

@Override
public ConvertibleEnum convertToEntityAttribute(Byte dbData) {
return dbData == null ? null : ConvertibleEnum.values()[dbData];
}
}
}