JPA - how to prevent an unnecessary join while querying many to many relationships

luke657

I have entities Product and Category that are joined with a simple many to many relationship. I want to get a paginated list of products filtered by a single category. I'm trying to write a Spring Data JPA automatic method or a JPQL query method that would produce the SQL similar to the following:

select [...] FROM ProductToCategory ptc INNER JOIN Product p ON ptc.product_id=p.id WHERE ptc.category_id=?  LIMIT ? OFFSET ?

Since the ProductToCategory join table isn't a JPA entity and I can't reference it in JPQL, the closest thing I could come up with was:

@Query("SELECT p FROM Category c INNER JOIN c.products p WHERE c=:category")
Page<Product> findByCategories(@Param("category") Category category, Pageable pageable);

But the resulting SQL produces a redundant join with the Category table and applies the where clause there, instead of on category id in the ProductToCategory table. Is there a way to do this without resorting to native SQL?

Alan Hay

Only way I can see is to map an entity to the join table and replace the many-to-many Product<>Category with one-to-many relationships pointing to this new entity from both Product and Category.

Such a change would actually be in line with Hibernate best practices:

https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/best-practices.html

Do not use exotic association mappings: Practical test cases for real many-to-many associations are rare. Most of the time you need additional information stored in the "link table". In this case, it is much better to use two one-to-many associations to an intermediate link class. In fact, most associations are one-to-many and many-to-one. For this reason, you should proceed cautiously when using any other association style.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Play - Querying many to many relationships

From Dev

Querying across many-to-many relationships

From Dev

Laravel, querying through many-to-many relationships

From Dev

Prevent duplicate entries in a join table in a many-to-many relationship in JPA

From Dev

Prevent duplicate entries in a join table in a many-to-many relationship in JPA

From Dev

How to differentiate a many-to-many relationships while designing a database?

From Dev

doubts many to many relationships in hibernate JPA

From Dev

doubts many to many relationships in hibernate JPA

From Dev

Querying Many to Many relationships Entitty Framework (doing wrong?? )

From Dev

How to join/populate 3 tables with one to many relationships in MongoDB/Mongoose?

From Dev

Ebean and many-to-many relationships - how?

From Dev

How to upload an image to a form with many to many relationships?

From Dev

How to work with many has many through relationships

From Dev

how to work with many to many relationships in Microsoft access?

From Dev

How to query many to many relationships on django?

From Dev

How to insert to into many to many relationships. SQLite

From Dev

How to join many to many in createQuery()

From Dev

JPA- Showing User Role relationships with Many to Many Hashset ENUMs

From Dev

SQL Query for multiple relationships to exist in a many to many JOIN

From Dev

Issue with many to many relationships

From Dev

querying current and previous relationships using has_many through: in Rails

From Dev

EF LEFT OUTER JOIN instead of INNER JOIN in one to many relationships

From Dev

Querying data from tables having one-to-many and many-to-one relationships based on the relationship

From Dev

jpa - How to create a many to many relationship with an IdClass?

From Dev

How to delete entity with many to many relation? JPA

From Dev

How to JOIN tables in many-to-many relationship

From Dev

merge in jpa many-to-many with extra column in join table

From Dev

merge in jpa many-to-many with extra column in join table

From Dev

No new entry in join table of many to many relationship JPA

Related Related

  1. 1

    Play - Querying many to many relationships

  2. 2

    Querying across many-to-many relationships

  3. 3

    Laravel, querying through many-to-many relationships

  4. 4

    Prevent duplicate entries in a join table in a many-to-many relationship in JPA

  5. 5

    Prevent duplicate entries in a join table in a many-to-many relationship in JPA

  6. 6

    How to differentiate a many-to-many relationships while designing a database?

  7. 7

    doubts many to many relationships in hibernate JPA

  8. 8

    doubts many to many relationships in hibernate JPA

  9. 9

    Querying Many to Many relationships Entitty Framework (doing wrong?? )

  10. 10

    How to join/populate 3 tables with one to many relationships in MongoDB/Mongoose?

  11. 11

    Ebean and many-to-many relationships - how?

  12. 12

    How to upload an image to a form with many to many relationships?

  13. 13

    How to work with many has many through relationships

  14. 14

    how to work with many to many relationships in Microsoft access?

  15. 15

    How to query many to many relationships on django?

  16. 16

    How to insert to into many to many relationships. SQLite

  17. 17

    How to join many to many in createQuery()

  18. 18

    JPA- Showing User Role relationships with Many to Many Hashset ENUMs

  19. 19

    SQL Query for multiple relationships to exist in a many to many JOIN

  20. 20

    Issue with many to many relationships

  21. 21

    querying current and previous relationships using has_many through: in Rails

  22. 22

    EF LEFT OUTER JOIN instead of INNER JOIN in one to many relationships

  23. 23

    Querying data from tables having one-to-many and many-to-one relationships based on the relationship

  24. 24

    jpa - How to create a many to many relationship with an IdClass?

  25. 25

    How to delete entity with many to many relation? JPA

  26. 26

    How to JOIN tables in many-to-many relationship

  27. 27

    merge in jpa many-to-many with extra column in join table

  28. 28

    merge in jpa many-to-many with extra column in join table

  29. 29

    No new entry in join table of many to many relationship JPA

HotTag

Archive