use of org.hibernate.type.CustomType in project hibernate-orm by hibernate.
the class NestedEmbeddableMetadataTest method testEnumTypeInterpretation.
@Test
public void testEnumTypeInterpretation() {
final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().enableAutoClose().applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop").build();
try {
final Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(Customer.class).buildMetadata();
PersistentClass classMetadata = metadata.getEntityBinding(Customer.class.getName());
Property investmentsProperty = classMetadata.getProperty("investments");
Collection investmentsValue = (Collection) investmentsProperty.getValue();
Component investmentMetadata = (Component) investmentsValue.getElement();
Value descriptionValue = investmentMetadata.getProperty("description").getValue();
assertEquals(1, descriptionValue.getColumnSpan());
Column selectable = (Column) descriptionValue.getColumnIterator().next();
assertEquals(500, selectable.getLength());
Component amountMetadata = (Component) investmentMetadata.getProperty("amount").getValue();
SimpleValue currencyMetadata = (SimpleValue) amountMetadata.getProperty("currency").getValue();
CustomType currencyType = (CustomType) currencyMetadata.getType();
int[] currencySqlTypes = currencyType.sqlTypes(metadata);
assertEquals(1, currencySqlTypes.length);
assertJdbcTypeCode(Types.VARCHAR, currencySqlTypes[0]);
} finally {
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
}
use of org.hibernate.type.CustomType in project hibernate-orm by hibernate.
the class FieldAccessedNestedEmbeddableMetadataTest method testEnumTypeInterpretation.
@Test
@FailureExpected(jiraKey = "HHH-9089")
public void testEnumTypeInterpretation() {
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
try {
final Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(Customer.class).buildMetadata();
PersistentClass classMetadata = metadata.getEntityBinding(Customer.class.getName());
Property investmentsProperty = classMetadata.getProperty("investments");
Collection investmentsValue = (Collection) investmentsProperty.getValue();
Component investmentMetadata = (Component) investmentsValue.getElement();
Value descriptionValue = investmentMetadata.getProperty("description").getValue();
assertEquals(1, descriptionValue.getColumnSpan());
Column selectable = (Column) descriptionValue.getColumnIterator().next();
assertEquals(500, selectable.getLength());
Component amountMetadata = (Component) investmentMetadata.getProperty("amount").getValue();
SimpleValue currencyMetadata = (SimpleValue) amountMetadata.getProperty("currency").getValue();
CustomType currencyType = (CustomType) currencyMetadata.getType();
int[] currencySqlTypes = currencyType.sqlTypes(metadata);
assertEquals(1, currencySqlTypes.length);
assertJdbcTypeCode(Types.VARCHAR, currencySqlTypes[0]);
} finally {
StandardServiceRegistryBuilder.destroy(ssr);
}
}
use of org.hibernate.type.CustomType in project hibernate-orm by hibernate.
the class QueryBuilder method toQuery.
public Query toQuery(Session session) {
final StringBuilder querySb = new StringBuilder();
final Map<String, Object> queryParamValues = new HashMap<>();
build(querySb, queryParamValues);
final Query query = session.createQuery(querySb.toString());
for (Map.Entry<String, Object> paramValue : queryParamValues.entrySet()) {
if (paramValue.getValue() instanceof RevisionType) {
// this is needed when the ClassicQueryTranslatorFactory is used
query.setParameter(paramValue.getKey(), paramValue.getValue(), new CustomType(new RevisionTypeType()));
} else {
query.setParameter(paramValue.getKey(), paramValue.getValue());
}
}
return query;
}
use of org.hibernate.type.CustomType in project hibernate-orm by hibernate.
the class ExtendedEnumTypeTest method assertEnumProperty.
private void assertEnumProperty(Class<?> entityClass, Class<?> typeClass, String propertyName, EnumType expectedType) {
doInJPA(this::entityManagerFactory, entityManager -> {
final SessionFactoryImplementor sessionFactory = entityManager.unwrap(SessionImplementor.class).getSessionFactory();
final EntityPersister entityPersister = sessionFactory.getMetamodel().entityPersister(entityClass);
final EnversService enversService = sessionFactory.getServiceRegistry().getService(EnversService.class);
final String entityName = entityPersister.getEntityName();
final String auditEntityName = enversService.getAuditEntitiesConfiguration().getAuditEntityName(entityName);
final EntityPersister auditedEntityPersister = sessionFactory.getMetamodel().entityPersister(auditEntityName);
final org.hibernate.type.Type propertyType = auditedEntityPersister.getPropertyType(propertyName);
assertTyping(CustomType.class, propertyType);
final UserType userType = ((CustomType) propertyType).getUserType();
assertTyping(typeClass, userType);
assertTyping(org.hibernate.type.EnumType.class, userType);
switch(expectedType) {
case STRING:
assertTrue(!((org.hibernate.type.EnumType) userType).isOrdinal());
break;
default:
assertTrue(((org.hibernate.type.EnumType) userType).isOrdinal());
break;
}
});
}
use of org.hibernate.type.CustomType in project hibernate-orm by hibernate.
the class IdentifierGeneratorHelper method get.
/**
* Extract the value from the result set (which is assumed to already have been positioned to the apopriate row)
* and wrp it in the appropriate Java numeric type.
*
* @param rs The result set from which to extract the value.
* @param identifier The name of the identifier column
* @param type The expected type of the value.
* @param dialect The current database dialect.
*
* @return The extracted value.
*
* @throws SQLException Indicates problems access the result set
* @throws IdentifierGenerationException Indicates an unknown type.
*/
public static Serializable get(ResultSet rs, String identifier, Type type, Dialect dialect) throws SQLException, IdentifierGenerationException {
if (ResultSetIdentifierConsumer.class.isInstance(type)) {
return ((ResultSetIdentifierConsumer) type).consumeIdentifier(rs);
}
if (CustomType.class.isInstance(type)) {
final CustomType customType = (CustomType) type;
if (ResultSetIdentifierConsumer.class.isInstance(customType.getUserType())) {
return ((ResultSetIdentifierConsumer) customType.getUserType()).consumeIdentifier(rs);
}
}
ResultSetMetaData resultSetMetaData = null;
int columnCount = 1;
try {
resultSetMetaData = rs.getMetaData();
columnCount = resultSetMetaData.getColumnCount();
} catch (Exception e) {
// Oracle driver will throw NPE
}
Class clazz = type.getReturnedClass();
if (columnCount == 1) {
if (clazz == Long.class) {
return rs.getLong(1);
} else if (clazz == Integer.class) {
return rs.getInt(1);
} else if (clazz == Short.class) {
return rs.getShort(1);
} else if (clazz == String.class) {
return rs.getString(1);
} else if (clazz == BigInteger.class) {
return rs.getBigDecimal(1).setScale(0, BigDecimal.ROUND_UNNECESSARY).toBigInteger();
} else if (clazz == BigDecimal.class) {
return rs.getBigDecimal(1).setScale(0, BigDecimal.ROUND_UNNECESSARY);
} else {
throw new IdentifierGenerationException("unrecognized id type : " + type.getName() + " -> " + clazz.getName());
}
} else {
try {
return extractIdentifier(rs, identifier, type, clazz);
} catch (SQLException e) {
if (StringHelper.isQuoted(identifier, dialect)) {
return extractIdentifier(rs, StringHelper.unquote(identifier, dialect), type, clazz);
}
throw e;
}
}
}
Aggregations