Query for join in Spring Data JPA

hibernate, jpa-2.0, spring-data-jpa

Solution

Here is a sample of JOIN query with SpringData

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 the mapping of the relationship between entities A and B.

- this assume `@OneToMany Set<B> descriptions()`

useful link

Problem

I want to write a join like ``` Select a.id,a.desc,b.desc from A a left join B b on a.MEDIA_ID = b.ID ``` I have create two Entities A & B and created `CrudRepository<A,Long>`. Now, in the crudRepository in need to write a method which can get the data using the above join. Also, i created a transient variable in entity A (Names it as 'bDescription) how to achieve this in Spring Data JPA. Note: I need join just to find out 'description'(a column in B) for a particular id(primary key in B and mapped as 'MEDIA_ID' in A) of Entity B.. Thanks in Advance

Original source