12
public interface ItemRepository extends JpaRepository<Item, Integer> {
   List<Item> findByNameAndDescriptionIsNullOrDescription(String name, String desc); 
 }

In the native Query, the where condition of findByNameAndDescriptionIsNullOrDescription is translated to

where item0_.NAME=? and (item0_.DESC is null) or item0_.DESC=? 

I need to have where item0_.NAME=? and (item0_.DESC is null or item0_.DESC=?)

I am using the following

  • spring-data-commons-1.6.3.RELEASE.jar

  • spring-data-jpa-1.4.3.RELEASE.jar

  • (i)hibernate-entitymanager-4.2.14.SP1-redhat-1.jar, (ii) hibernate-jpa-2.0-api-1.0.1.Final-redhat-2.jar offered by JBOSS EAP 6.3

S.A.Norton Stanley
  • 1,833
  • 3
  • 23
  • 37
lab bhattacharjee
  • 1,617
  • 2
  • 17
  • 33

3 Answers3

4

Probably you can't do that.

Spring Data querydsl is used only for simple SQL queries and if you need something more complex use @Query annotation.

It also would be easier for other developers to understand what query you used instead of analyzing your method name.

Orest
  • 6,548
  • 10
  • 54
  • 84
1

And operator has priority over to or operator. Changing this priority or using pharenthesis in JPA queries is not possible but you can try to spread and operator to over others:

findByNameAndDescriptionOrNameOrDescriptionIsNull

Now your logic evulated from:

item0_.NAME=? and (item0_.DESC is null or item0_.DESC=?)

to:

(item0_.NAME=? and item0_.DESC=?) or (item0_.NAME=? and item0_.DESC is null)
Emin Bilgiç
  • 972
  • 6
  • 23
0

What is the need to add 2nd argument desc.. Just use below:

List findByNameAndDescription(String name, String desc);

Even if u pass desc argument null, u ll get desired results.

Mayank Porwal
  • 276
  • 1
  • 3
  • 13