JPQL query with many-to-many relationship

Dima dimmxx :

We have 2 entities Technology and Project with a Many-to-Many relationship, which are linked with an additional reference table.

technologies
 id      name
1000 | digging
2000 | drilling

projects
id    name
10 | London
20 | Madrid

technologies_projects
tech_id     project_id
1000     | 10
2000     | 10
1000     | 20

I can retreive Technology from db with such a query:

@Query("select t from Technology t left join fetch t.projects")
List<Technology> findAll();

JPQL query with left join fetch clause must be used to retreive Technology with collection of projects to avoid lazy initialization exception.

The question is: How must the query be modified do get the list of technologies, used in a certain project? (the query findAllByProject(10) must return technologies 1000 and 2000).

I cannot use native SQL query here because I need join fetch to get collection of projects.

CodeScale :

By adding a where clause on project entity.

@Query("select t from Technology t left join fetch t.projects p where p.id=10")
List<Technology> findAllByProject10();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JPQL query with many to many relationship

From Dev

JPQL Query Error - hibernate many to many relationship

From Dev

How to query one-to-many relationship in JPQL?

From Dev

JPQL for One to Many Relationship

From Java

Many-to-Many query jpql

From Java

Many-to-Many query jpql

From Java

JPQL check many-to-many relationship

From Java

JPQL: query entities with Many-to-Many relationship modeled using a composite key

From Dev

Query many to many relationship with DetachedCriteria

From Dev

how to query a many to many relationship?

From Dev

eloquent: query many to many relationship

From Dev

Query in many to many relationship - mySQL

From Dev

Complex query in a many to many relationship

From Dev

Laravel query on Many to Many relationship

From Dev

JPA or JPQL how to return single row from a many to many relationship

From Dev

JPQL Many to Many with in Clause

From Dev

Many to Many Relationship - how to query all children

From Dev

HQL query for Many to Many Explict relationship

From Dev

ios parse one query many to many relationship

From Dev

Django Many to Many relationship Query Filter

From Dev

Single SQL query on many to many relationship

From Dev

How to query many to many relationship in Firebase for Android?

From Dev

Query from a many to many relationship table

From Dev

Flask selfreferential many to many relationship, how to query

From Java

Problem with many to many relationship query JPA

From Dev

Laravel Many to Many query relationship with where clause

From Dev

SQL query: how to filter a many to many relationship

From Dev

query over many to many relationship in redbeanphp

From Dev

CoreData: Query to one-to-many-to-many relationship

Related Related

HotTag

Archive