Salut Nicola,
Tu as raison. Voici le code exemple...
1°) Schema de la base de données:
create table TEAM (
TEAM_ID bigint generated by default as identity(start with 1),
COACH_ID bigint,
TEAM_NAME varchar(255),
NB_WON integer,
NB_LOST integer,
NB_PLAYED integer,
primary key (TEAM_ID)
);
create table PLAYER (
PLAYER_ID bigint generated by default as identity(start with 1),
PLAYER_NAME varchar(255),
PLAYER_NUMBER integer,
BIRTHDAY timestamp,
HEIGHT float,
WEIGHT float,
TEAM_ID bigint,
primary key (PLAYER_ID)
);
create table COACH_TEAM (
COACH_ID bigint not null,
TEAM_ID bigint not null,
YEAR integer not null,
PRIMARY KEY (COACH_ID,TEAM_ID)
);
create table COACH (
COACH_ID bigint generated by default as identity(start with 1),
COACH_NAME varchar(255),
PRIMARY KEY (COACH_ID)
);
[J'ai enlevé les contraintes d'intégrité]
2°) Mapping Hibernate utilisé pour la classe Team
<hibernate-mapping package="model">
<class name="Team" table="TEAM" lazy="true">
<id name="id" column="TEAM_ID">
<generator class="native"/>
</id>
<property name="name" column="TEAM_NAME"/>
<property name="nbWon" column="NB_WON"/>
<property name="nbLost" column="NB_LOST"/>
<property name="nbPlayed" column="NB_PLAYED"/>
<set name="coachs" table="COACH_TEAM" cascade="save-update" fetch="select">
<key column="TEAM_ID"/>
<many-to-many column="COACH_ID" class="Coach" fetch="select"/-->
<composite-element class="CoachTeam">
<parent name="team"/>
<property column="YEAR" name="year"/>
<many-to-one name="coach" column="COACH_ID" cascade="save-update" />
</composite-element>
</set>
<list name="players" inverse="true" cascade="save-update">
<key column="TEAM_ID" />
<list-index column="PLAYER_NUMBER"/>
<one-to-many class="Player" />
</list>
</class>
</hibernate-mapping>
3°) Code utilisé
Team team=(Team)session.load(Team.class,new Long(3));
System.out.println("team = "+team);
System.out.println("## team.getPlayers()");
List players=team.getPlayers();
for(Iterator i=players.iterator();i.hasNext();) {
Player player=(Player)i.next();
System.out.println("player = "+player);
}
System.out.println("## -----------------");
System.out.println("## team.getCoachs()");
Set coachs=team.getCoachs();
for(Iterator i=coachs.iterator();i.hasNext();) {
CoachTeam coachTeam=(CoachTeam)i.next();
Coach coach=coachTeam.getCoach();
int year=coachTeam.getYear();
System.out.println("year = "+year);
System.out.println("coach = "+coach);
}
System.out.println("## -----------------");
4°) Log d'Hibernate avec sql output activé:
Hibernate: select team0_.TEAM_ID as TEAM1_0_, team0_.TEAM_NAME as TEAM2_0_0_, team0_.NB_WON as NB3_0_0_, team0_.NB_LOST as NB4_0_0_, team0_.NB_PLAYED as NB5_0_0_ from TEAM team0_ where team0_.TEAM_ID=?
team = model.Team@81a197
## team.getPlayers()
Hibernate: select players0_.TEAM_ID as TEAM7_1_, players0_.PLAYER_ID as PLAYER1_1_, players0_.PLAYER_NUMBER as PLAYER3_1_, players0_.PLAYER_ID as PLAYER1_0_, players0_.PLAYER_NAME as PLAYER2_2_0_, players0_.PLAYER_NUMBER as PLAYER3_2_0_, players0_.BIRTHDAY as BIRTHDAY2_0_, players0_.HEIGHT as HEIGHT2_0_, players0_.WEIGHT as WEIGHT2_0_, players0_.TEAM_ID as TEAM7_2_0_ from PLAYER players0_ where players0_.TEAM_ID=?
player = model.Player@16917ee
## -----------------
## team.getCoachs()
Hibernate: select coachs0_.TEAM_ID as TEAM1_0_, coachs0_.YEAR as YEAR0_, coachs0_.COACH_ID as COACH3_0_ from COACH_TEAM coachs0_ where coachs0_.TEAM_ID=?
year = 1999
Hibernate: select coach0_.COACH_ID as COACH1_0_, coach0_.COACH_NAME as COACH2_3_0_ from COACH coach0_ where coach0_.COACH_ID=?
coach = model.Coach@1fe8ce8
## -----------------
Hibernate: delete from COACH_TEAM where TEAM_ID=? and YEAR=? and COACH_ID=?
Hibernate: insert into COACH_TEAM (TEAM_ID, YEAR, COACH_ID) values (?, ?, ?)
Merci par avance pour votre aide.
Thierry
|