Having:
Code:
<class name="Instrument" table="INSTRUMENTS">
<id name="id" column="ID" type="long">
<generator class="native"/>
</id>
<property name="code" column="CODE" type="string" length="5" not-null="true"/>
<property name="description" column="DESCRIPTION" type="string" length="50" not-null="true"/>
<set name="artists" table="ARTISTS_INSTRUMENTS" inverse="true" lazy="true">
<key column="INSTRUMENT_ID" />
<many-to-many class="Artist" column="ARTIST_ID" />
</set>
</class>
<class name="Artist" table="ARTISTS" lazy="true">
<id name="id" column="ID" type="long" unsaved-value="0">
<generator class="native"/>
</id>
<property name="lastName" column="LASTNAME" type="string" length="50"/>
<property name="name" column="NAME" type="string" length="50"/>
<property name="birthYear" column="BIRTH_YEAR" type="integer" length="11"/>
<property name="deathYear" column="DEATH_YEAR" type="integer" length="11"/>
<set name="albums" inverse="true" lazy="true">
<key column="ARTIST_ID" />
<one-to-many class="Album" />
</set>
<set name="instruments" table="ARTISTS_INSTRUMENTS" inverse="true" lazy="true">
<key column="ARTIST_ID" />
<many-to-many class="Instrument" column="INSTRUMENT_ID" />
</set>
</class>
where ARTISTS_INSTRUMENTS is a many-to-many table with ARTISTS and INSTRUMENTS, when I execute this query (to get the list of instrument for an artist with id = 2):
Code:
select elements(artist.instruments) from Artist artist where artist.id=2
Hibernate issues these queries:
1) select instrument2_.ID as x0_0_ from ARTISTS artist0_, ARTISTS_INSTRUMENTS instrument1_, INSTRUMENTS instrument2_ where artist0_.ID=instrument1_.ARTIST_ID and instrument1_.INSTRUMENT_ID=instrument2_.ID and ((artist0_.ID=2 ))
2) select instrument0_.ID as ID0_, instrument0_.CODE as CODE0_, instrument0_.DESCRIPTION as DESCRIPT3_0_ from INSTRUMENTS instrument0_ where instrument0_.ID=?
3) select instrument0_.ID as ID0_, instrument0_.CODE as CODE0_, instrument0_.DESCRIPTION as DESCRIPT3_0_ from INSTRUMENTS instrument0_ where instrument0_.ID=?
I think 2) and 3) are right, but I cannot understand the 1). Why 3 joins? It would have been enough looking into just 1 table (ARTISTS_INSTRUMENTS). Maybe Hibernate has to look into ARTISTS because I asked "from Artist", but why the join with INSTRUMENTS?