Search in sources :

Example 1 with BindableType

use of org.hibernate.query.BindableType in project hibernate-orm by hibernate.

the class AbstractCommonQueryContract method setProperties.

@Override
public CommonQueryContract setProperties(Object bean) {
    final Class<?> clazz = bean.getClass();
    for (String paramName : getParameterMetadata().getNamedParameterNames()) {
        try {
            final PropertyAccess propertyAccess = BuiltInPropertyAccessStrategies.BASIC.getStrategy().buildPropertyAccess(clazz, paramName, true);
            final Getter getter = propertyAccess.getGetter();
            final Class<?> retType = getter.getReturnTypeClass();
            final Object object = getter.get(bean);
            if (Collection.class.isAssignableFrom(retType)) {
                setParameterList(paramName, (Collection<?>) object);
            } else if (retType.isArray()) {
                setParameterList(paramName, (Object[]) object);
            } else {
                BindableType<Object> type = determineType(paramName, retType);
                setParameter(paramName, object, type);
            }
        } catch (PropertyNotFoundException pnfe) {
        // ignore
        }
    }
    return this;
}
Also used : PropertyNotFoundException(org.hibernate.PropertyNotFoundException) Getter(org.hibernate.property.access.spi.Getter) BindableType(org.hibernate.query.BindableType) PropertyAccess(org.hibernate.property.access.spi.PropertyAccess)

Example 2 with BindableType

use of org.hibernate.query.BindableType in project hibernate-orm by hibernate.

the class ConverterTest method testJPQLUpperAttributeValueBindParameterType.

@Test
public void testJPQLUpperAttributeValueBindParameterType(EntityManagerFactoryScope scope) {
    scope.inTransaction(entityManager -> {
        // tag::basic-attribute-converter-query-parameter-converter-object-example[]
        SessionFactoryImplementor sessionFactory = entityManager.getEntityManagerFactory().unwrap(SessionFactoryImplementor.class);
        final MappingMetamodelImplementor mappingMetamodel = sessionFactory.getRuntimeMetamodels().getMappingMetamodel();
        Type captionType = mappingMetamodel.getEntityDescriptor(Photo.class).getPropertyType("caption");
        Photo photo = (Photo) entityManager.createQuery("select p " + "from Photo p " + "where upper(caption) = upper(:caption) ", Photo.class).unwrap(Query.class).setParameter("caption", new Caption("Nicolae Grigorescu"), (BindableType) captionType).getSingleResult();
        // end::basic-attribute-converter-query-parameter-converter-object-example[]
        assertEquals("Dorobantul", photo.getName());
    });
}
Also used : BindableType(org.hibernate.query.BindableType) Type(org.hibernate.type.Type) Query(org.hibernate.query.Query) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) Test(org.junit.jupiter.api.Test)

Aggregations

BindableType (org.hibernate.query.BindableType)2 PropertyNotFoundException (org.hibernate.PropertyNotFoundException)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 MappingMetamodelImplementor (org.hibernate.metamodel.spi.MappingMetamodelImplementor)1 Getter (org.hibernate.property.access.spi.Getter)1 PropertyAccess (org.hibernate.property.access.spi.PropertyAccess)1 Query (org.hibernate.query.Query)1 Type (org.hibernate.type.Type)1 Test (org.junit.jupiter.api.Test)1