My requirement is to have few custom fields in the domain objects. These fields may vary as per the clients.

We are using Spring Data JPA to execute finders. Spring data implicitly provides finders for static fields of the domain and can also handle finders for the fields in the object graph.

I want to know if there is a way to find data on the custom fields? Can someone suggest me a strategy to achieve the same. Below is the sample of my domain class.

public class Employee{

private String name;

private String age;

private Map customeFields; (May vary as per client)

}

I was thinking of overriding QueryLookupStrategy and create my CustomJpaQuery on lines of PartTreeJpaQuery to achieve it. Is there any better approach? Does spring data jpa provides an easy mechanism to override query creation mechanism?

有帮助吗?

解决方案

If you are using hibernate (not sure about other JPA implementations) you may add methods with @Query annotations like this:

@Query("select e from Employee as e where e.customeFields[:key] = :value")
List<Employee> findSomeHow(@Param("key") String key, @Param("value") String value)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top