3

I am using JPA repository to access Mysql. I have this query :

@Query("Select s.productName, s.stock from Sell s");

I am getting an execution error because stock is sometime null.
The generated sql translation of this query is :

select sell0_.productName, sell0_.stock_id from T_SELL sell0_ inner join T_STOCK stock0_ on sell0_.stock_id=stock0_.id  

Is there a way to make this JPA query work even if stock_id is null?

user1260928
  • 3,269
  • 9
  • 59
  • 105

2 Answers2

3

Should be able to coalesce

@Query("Select s.productName, coalesce(s.stock,'xxx') from Sell s");

Full man is https://docs.jboss.org/hibernate/orm/4.3/devguide/en-US/html/ch11.html

Hope this helps.

Essex Boy
  • 7,565
  • 2
  • 21
  • 24
1

Try to change relation definition, using optional = true:

@OneToOne(optional = true)
private Stock stock; 

You can even change how this data should be fetched, using a join, a subselect or with a different select. More info here: JPA eager fetch does not join

Community
  • 1
  • 1
Ricardo Vila
  • 1,626
  • 1
  • 18
  • 34