Custom coalesce JPA query using SpEL

Viewed 428

I'm using Hibernate with Spring Data and I'm trying to create a custom coalesce query using SpEL in the following fashion:

@Modifying
@Query("UPDATE Order o SET o.serial = COALESCE(?#{#another.serial}, o.serial), " +
        "o.owner = COALESCE(?#{#another.owner}, o.owner)")
Order nullSafeUpdate(@Param("another") Order another);

the result of this is the following

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Using named parameters for method public abstract test.test.test.test.Order test.test.test.test.OrderRepository.nullSafeUpdate(test.test.test.test.Order) but parameter 'another' not found in annotated query 'UPDATE Order o SET o.serial = COALESCE(?#{#another.serial}, o.serial), o.owner = COALESCE(?#{#another.owner}, o.owner)'!

I've already tried replacing ? with : based on these guidelines and removing the #.

The Order entity has both public getters and setters for all variables.

Using Spring Data 1.6.6

UPDATE

Seems like the JpaQueryMethod validator assertParameterNamesInAnnotatedQuery does not even check for parameters included in a SpEL form

if (!annotatedQuery.contains(String.format(":%s", parameter.getName()))) {
            throw new IllegalStateException(String.format(
                    "Using named parameters for method %s but parameter '%s' not found in annotated query '%s'!", method,
                    parameter.getName(), annotatedQuery));
        }
0 Answers
Related