简体   繁体   中英

I don't understand why Hibernate creates a query with two joins

I have two entities with a ManyToMany relationship.

@Entity
@Table(name = "KATEGORIA_CENOWA_WODA_SCIEKI")
public class KategoriaCenowaWodaScieki extends BaseEntity {

   private static final long serialVersionUID = -113600254855102844L;

  @EqualsAndHashCode.Exclude
  @ManyToMany(mappedBy = "kategoriaCenowaList")
  private Set<KatalogSubstancjiWodaScieki> katalogSubstancjiList = new HashSet<>();

and

@Entity
@Table(name = "KATALOG_SUBSTANCJI_WODA_SCIEKI")
public class KatalogSubstancjiWodaScieki extends BaseEntity {

  private Integer kodSubstancji;

  @Column(nullable = false) 
  private String nazwaSubstancji;

  @EqualsAndHashCode.Exclude
  @ManyToMany
  @JoinTable(name = "KATALOG_SUBSTANCJI_WODA_SCIEKI_KATEGORIA_CENOWA_WODA_SCIEKI",
    joinColumns = {
      @JoinColumn(name = "KATALOG_SUBSTANCJI_ID")},
    inverseJoinColumns = {
      @JoinColumn(name = "KATEGORIA_CENOWA_ID")})
  private Set<KategoriaCenowaWodaScieki> kategoriaCenowaList = new HashSet<>();

and I have created a query with one join

@Query("select s.kategoriaCenowaList from KatalogSubstancjiWodaScieki s " +
        "inner join s.kategoriaCenowaList c " +
        " where s = :katalogSubstancji " ) 
Optional<KategoriaCenowaWodaScieki> findByKatalogSubstancjiAndOkresObowiazywaniaAndKatalogRZGW(
        KatalogSubstancjiWodaScieki katalogSubstancji);

I don't understand why a generated SQL contains two physical joins. Maybe someone could explain this?

select
    kategoriac4_.ID as ID1_46_,
    kategoriac4_.CREATED as CREATED2_46_,
    kategoriac4_.UPDATED as UPDATED3_46_,
    kategoriac4_.KATALOG_RZGW_ID as KATALOG_9_46_,
    kategoriac4_.kod as kod4_46_,
    kategoriac4_.nazwa as nazwa5_46_,
    kategoriac4_.dataDo as dataDo6_46_,
    kategoriac4_.dataOd as dataOd7_46_,
    kategoriac4_.stawka as stawka8_46_
from
    KATALOG_SUBSTANCJI_WODA_SCIEKI katalogsub0_
      inner join
    KATALOG_SUBSTANCJI_WODA_SCIEKI_KATEGORIA_CENOWA_WODA_SCIEKI kategoriac1_
    on katalogsub0_.ID=kategoriac1_.KATALOG_SUBSTANCJI_ID
        inner join
    KATEGORIA_CENOWA_WODA_SCIEKI kategoriac2_
    on kategoriac1_.KATEGORIA_CENOWA_ID=kategoriac2_.ID

Because of @ManyToMany . That annotation only ever works with 2 joins, just like it does in SQL.

You can't query data in a Many-To-Many relationship without 2 joins. The first join goes to the join table which holds references to the 2 base tables with the actual data you want to join. The second join then queries the actual data from the second table.

KATALOG_SUBSTANCJI_WODA_SCIEKI is the base table. Then you join KATALOG_SUBSTANCJI_WODA_SCIEKI_KATEGORIA_CENOWA_WODA_SCIEKI which holds the references between the entities and then KATEGORIA_CENOWA_WODA_SCIEKI holds the second set of actual data.

Even without your join in the query, hibernate should perform these 2 joins to fully load an entity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM