org.hibernate.jpa.TypedParameterValue Java Examples

The following examples show how to use org.hibernate.jpa.TypedParameterValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractProducedQuery.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <P> QueryImplementor setParameter(Parameter<P> parameter, P value) {
	getProducer().checkOpen();
	if ( value instanceof TypedParameterValue ) {
		setParameter( parameter, ( (TypedParameterValue) value ).getValue(), ( (TypedParameterValue) value ).getType() );
	}
	else if ( value instanceof Collection && !isRegisteredAsBasicType( value.getClass() ) ) {
		locateListBinding( parameter ).setBindValues( (Collection) value );
	}
	else {
		locateBinding( parameter ).setBindValue( value );
	}

	return this;
}
 
Example #2
Source File: AbstractProducedQuery.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(String name, Object value) {
	getProducer().checkOpen();
	if ( value instanceof TypedParameterValue ) {
		final TypedParameterValue  typedValueWrapper = (TypedParameterValue) value;
		setParameter( name, typedValueWrapper.getValue(), typedValueWrapper.getType() );
	}
	else if ( value instanceof Collection && !isRegisteredAsBasicType( value.getClass() ) ) {
		setParameterList( name, (Collection) value );
	}
	else {
		getQueryParameterBindings().getBinding( name ).setBindValue( value );
	}

	return this;
}
 
Example #3
Source File: AbstractProducedQuery.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(int position, Object value) {
	getProducer().checkOpen();
	if ( value instanceof TypedParameterValue ) {
		final TypedParameterValue typedParameterValue = (TypedParameterValue) value;
		setParameter( position, typedParameterValue.getValue(), typedParameterValue.getType() );
	}
	else if ( value instanceof Collection && !isRegisteredAsBasicType( value.getClass() ) ) {
		setParameterList( getParameterMetadata().getQueryParameter( position ), (Collection) value );
	}
	else {
		getQueryParameterBindings().getBinding( position ).setBindValue( value );
	}
	return this;
}
 
Example #4
Source File: PostgreSQLEnumArrayTypeTest.java    From hibernate-types with Apache License 2.0 6 votes vote down vote up
@Test
public void testTypedParameterValue() {
    UserRole[] userRoles = {UserRole.ROLE_ADMIN, UserRole.ROLE_USER};
    UserRole[] requiredRoles = {UserRole.ROLE_USER};

    doInJPA(entityManager -> {
        UserAccount account = new UserAccount();
        account.setUsername("vladmihalcea.com");
        account.setRoles(userRoles);
        entityManager.persist(account);
    });

    doInJPA(entityManager -> {
        entityManager
        .createQuery(
            "select ua " +
            "from UserAccountEntity ua " +
            "where ua.roles = :roles", UserAccount.class)
        .setParameter("roles", new TypedParameterValue(ROLE_TYPE, requiredRoles))
        .getResultList();
    });
}
 
Example #5
Source File: HQLUtilsTest.java    From yes-cart with Apache License 2.0 6 votes vote down vote up
@Test
public void testCriteriaInTest() throws Exception {

    Object test = HQLUtils.criteriaInTest(null);
    assertTrue(test instanceof TypedParameterValue);
    assertTrue(((TypedParameterValue) test).getType() instanceof IntegerType);
    assertEquals(Integer.valueOf(0), ((TypedParameterValue) test).getValue());

    test = HQLUtils.criteriaInTest(Collections.emptyList());
    assertTrue(test instanceof TypedParameterValue);
    assertTrue(((TypedParameterValue) test).getType() instanceof IntegerType);
    assertEquals(Integer.valueOf(0), ((TypedParameterValue) test).getValue());

    test = HQLUtils.criteriaInTest(Collections.singletonList("aBc"));
    assertTrue(test instanceof TypedParameterValue);
    assertTrue(((TypedParameterValue) test).getType() instanceof IntegerType);
    assertEquals(Integer.valueOf(1), ((TypedParameterValue) test).getValue());

}
 
Example #6
Source File: BindArrayTypeQueryParameterTest.java    From hibernate-types with Apache License 2.0 5 votes vote down vote up
@Test
public void testJPQLWithTypedParameterValue() {
    doInJPA(entityManager -> {
        Event event = entityManager
        .createQuery(
            "select e " +
            "from Event e " +
            "where " +
            "   fn_array_contains(e.values, :arrayValues) = true", Event.class)
        .setParameter("arrayValues", new TypedParameterValue(IntArrayType.INSTANCE, new int[]{2, 3}))
        .getSingleResult();

        assertArrayEquals(new int[]{1, 2, 3}, event.getValues());
    });
}
 
Example #7
Source File: PostgreSQLEnumTest.java    From hibernate-types with Apache License 2.0 5 votes vote down vote up
@Test
public void testTypedParameterValue() {
    doInJPA(entityManager -> {
        entityManager.createQuery("SELECT a FROM Post a WHERE a.status = :paramValue", Post.class)
               .setParameter("paramValue", new TypedParameterValue(POST_STATUS_TYPE, PostStatus.APPROVED))
               .getResultList();
    });
}
 
Example #8
Source File: PostgreSQLEnumTest.java    From hibernate-types with Apache License 2.0 5 votes vote down vote up
@Test
public void testTypedParameterValue() {
    doInJPA(new JPATransactionFunction<Void>() {

        @Override
        public Void apply(EntityManager entityManager) {
            entityManager.createQuery("SELECT a FROM Post a WHERE a.status = :paramValue", Post.class)
                    .setParameter("paramValue", new TypedParameterValue(POST_STATUS_TYPE, PostStatus.APPROVED))
                    .getResultList();
            return null;
        }
    });
}
 
Example #9
Source File: HQLUtils.java    From yes-cart with Apache License 2.0 3 votes vote down vote up
/**
 * Create a test value to check if collection is empty or not.
 *
 * First part of check in form of: ?1 = 0 or e.x in (?2)
 * Sets ?1 to null or "1"
 *
 * @param value raw value
 *
 * @return null or match anywhere
 */
public static Object criteriaInTest(final Collection value) {
    if (CollectionUtils.isEmpty(value)) {
        return new TypedParameterValue(IntegerType.INSTANCE, 0); // no value
    }
    return new TypedParameterValue(IntegerType.INSTANCE, 1);
}