下面列出了怎么用org.hibernate.jpa.TypedParameterValue的API类实例代码及写法,或者点击链接到github查看源代码。
@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;
}
@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;
}
@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;
}
@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();
});
}
@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());
}
@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());
});
}
@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();
});
}
@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;
}
});
}
/**
* 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);
}