Request to join Spring Data JPA

I want to write a union like

Select a.id,a.desc,b.desc from A a left join B b on a.MEDIA_ID = b.ID 

I created two objects A and B and created CrudRepository<A,Long> .

Now in crudRepository you need to write a method that can receive data using the union above.

In addition, I created a transition variable in entity A (referred to as "bDescription") how to do this in Spring JPA data.

Note. I need to join to find out the "description" (column in B) for a specific identifier (primary key in B and displayed as "MEDIA_ID" in A) of object B ..

Thanks at Advance

+6
source share
1 answer

Here is an example JOIN request with SpringData p>

 public final static String FIND_WITH_DESC_QUERY = "SELECT a,b.desc as bDescription " + "FROM A a LEFT JOIN a.descriptions b " + "WHERE a.mediaID = :id"; @Query(FIND_WITH_DESC_QUERY) public List<Media> findWithDescription(@Param("id") Long id); 

Note:

  • descriptions is a mapping of the relationship between objects A and B.
  • suppose @OneToMany Set<B> descriptions()

useful link

+4
source

Source: https://habr.com/ru/post/958293/


All Articles