Spring Data JPA SubQuery
I´m in a situation where I have two entities, let´s say A and B, in B I have a relationship ManyToOne with A.But in A I dont have the oneToMany. I´m using Spring-Data repositories, and I was wondering If would be possible make a subQuery using Query annotation. So far I see some example of people using CriteriaQuery, but always with JOIN example, and in my case I need the other way around. I need to find all A´s if their id´s are not in B. Any good and elegant solutions?.
Just put @Query on your interface methods and write your JPQL there. Examples here: http://docs.oracle.com/javaee/7/tutorial/doc/persistence-querylanguage005.htm#BNBVL
e.g. SELECT DISTINCT a FROM A a WHERE NOT EXISTS (SELECT b FROM B b WHERE <<put your condition here>>)
You can use NamedNativeQuery of JPA or custom pojo class (with constructor to populate all your required field) but in this case you will get array of object and you have to fech your records from object array.