Hello,
I'm using Hibernate with composite keys (in order to map an existing schema).
I've the 3 following tables :
Code:
create table ST_Produit (
   noProd INT not null,
   libelle VARCHAR(32) null,
   primary key (noProd)
)
Code:
create table ST_Lot (
   noProd INT not null,
   noLot INT not null,
   idExt VARCHAR(64) null,
   primary key (noProd, noLot)
)
Code:
create table ST_Piece (
   noProd INT not null,
   noLot INT not null,
   noPiece INT not null,
   idExt VARCHAR(16) null,
   primary key (noProd, noLot, noPiece)
)
and the following mappings :
Code:
<class name="fr.infologic.stocks.modele.Produit" table="ST_Produit" proxy="fr.infologic.stocks.modele.Produit">
   <id column="noProd" name="noProd">
      <generator class="assigned"/>
   </id>
   <property name="libelle" type="string">
      <column name="libelle" length="32"/>
   </property>
</class>
Code:
<class name="fr.infologic.stocks.modele.Lot" table="ST_Lot" proxy="fr.infologic.stocks.modele.Lot">
   <composite-id>
      <key-many-to-one name="prod" column="noProd" class="fr.infologic.stocks.modele.Produit"/>
      <key-property name="noLot" column="noLot"/>
   </composite-id>
   <property name="idExt" type="string">
      <column name="idExt" length="64"/>
   </property>
</class>
Code:
<class name="fr.infologic.stocks.modele.Piece" table="ST_Piece" proxy="fr.infologic.stocks.modele.Piece">
   <composite-id>
      <key-many-to-one name="lot">
         <column name="noProd"/>
         <column name="noLot"/>
      </key-many-to-one>
      <key-property name="noPiece" column="noPiece"/>
   </composite-id>
   <property name="idExt" type="string">
      <column name="idExt" length="16"/>
   </property>
</class>
When I do the following query :
Code:
List pieces = session.find(
            "from fr.infologic.stocks.modele.Piece as piece " +
            "where piece.lot.prod = ?",
            new Integer(64),
            Hibernate.INTEGER
);
the generated SQL is :
Code:
select piece0_.noProd as noProd, piece0_.noLot as noLot, piece0_.noPiece as noPiece, piece0_.idExt as idExt 
from ST_Piece piece0_, ST_Lot lot1_ 
where (lot1_.noProd=?  and piece0_.noProd=lot1_.noProd and piece0_.noLot=lot1_.noLot)
where I expected :
Code:
select piece0_.noProd as noProd, piece0_.noLot as noLot, piece0_.noPiece as noPiece, piece0_.idExt as idExt 
from ST_Piece piece0_ 
where piece0_.noProd=?
as far as noProd is part  of the primary key of ST_Piece table.
My question is : how to manage to have the previous query on Piece class without having a join.
Thanks in advance and sorry for the long message.
Philippe[/code]