4 years after the initial publication, some changes occurred. Using spring 4 and Hibernate 4, you can now fool Hibernate using the SpEL expression. For example:
Listing:
package com.mycompany.enums public enum Status { INITIAL, PENDING, REJECTED, APPROVED, SHIPPED, DELIVERED, COMPLETE; }
Here is a wrapper class called "Filter", which we will move on to the repository filtering method.
package com.mycompany.enums public class Filter implements Serializable { private Integer id; private Status status;
Finally, in the repository we can now use the Filter class as the only parameter and make the request translate what seems to be a mixture of literals and SpEL expressions to the Status object:
Repository:
package com.mycompany.repository @Repository public interface OrderRepository extends CrudRepository<Order, Integer> { @Query("SELECT o from Order o " + "WHERE o.id = COALESCE(:#{#filter.id},o.id) " + "AND o.status = COALESCE(:#{T(com.mycompany.enums.Status).#filter.statusName},o.status)") public List<Order> getFilteredOrders(@Param(value = "filter") Filter filter); }
This works fine, but for some odd reason, I still don't understand if you turn on SQL debugging in Hibernate and turn on bind logging, you won’t be able to see that Hibernate binds this expression to variable requests.
Tom Silverman Apr 30 '16 at 14:52 2016-04-30 14:52
source share