Bonjour,

J'ai un problème que je n'arrive pas à résoudre bien que j'ai parcouru la documentation d'hibernate 4.3.5 ( version que j'utilise ) et pas mal de sites sur le sujet je bloque toujours autant. Je vous explique j'ai une classe Team contenant une liste Set d'objets de type Player que je récupère avec la methode getPlayers(). Mais cette liste s'avere être toujours vite bien que l'objet Team lui ne le soit pas après qu'il eut été chargé via la méthode get ou même load de l'objet session (session.get(Team.class, new Integer(1)). Je précise quand même que la base de donnée est loin d'être vide.

Pouvez-vous m'éclairer sur le sujet s'il vous plaît


voici ma Team :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 
 
public class Team {	
 
	private int idteam;
	private String nom;
	private Set<Player> players = new HashSet<Player>();
	private Coach coach;
 
 
	public Team (){
		super();
	}
 
	public Team(int team_Id, String nom,Coach coach, Set<Player> players) {
		super();
		idteam = team_Id;
		this.nom = nom;
		this.players = players;
		this.coach = coach;
	}
 
	public Team(int team_Id, String nom,Coach coach) {
		super();
		idteam = team_Id;
		this.nom = nom;
		this.players = players;
		this.coach = coach;
	}
 
	public int getidteam() {
		return idteam;
	}
 
 
	public void setidteam(int team_Id) {
		idteam = team_Id;
	}
 
	public String getNom() {
		return nom;
	}
 
	public void setNom(String nom) {
		this.nom = nom;
	}
 
	public Set<Player> getPlayers() {
		return players;
		}
 
	public void setPlayers(Set<Player> players) {
		this.players = players;
	}
 
	public Coach getCoach() {
		return coach;
	}
 
	public void setCoach(Coach coach) {
		this.coach = coach;
	}
 
}
ma classe Player :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
 
 
public class Player {
 
	private int Player_Id;
	private Team team;
	private String nom;
	private String prenom;
 
 
	public Player(int player_Id, String nom, String prenom, Team team) {
		super();
		Player_Id = player_Id;
		this.nom = nom;
		this.prenom = prenom;
		this.team=team;
	}
 
	public Player(int player_Id, String nom, String prenom) {
		super();
		Player_Id = player_Id;
		this.nom = nom;
		this.prenom = prenom;
 
	}
 
	public Player() {
		super();
 
	}
 
	public int getPlayer_Id() {
		return Player_Id;
	}
 
	public void setPlayer_Id(int player_Id) {
		Player_Id = player_Id;
	}
 
	public String getNom() {
		return nom;
	}
 
	public void setNom(String nom) {
		this.nom = nom;
	}
 
	public String getPrenom() {
		return prenom;
	}
 
	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}
 
 
 
	public Team getTeam() {
		return team;
	}
 
 
 
	public void setTeam(Team team) {
		this.team = team;
	}
 
 
	public String toString(){
 
		return " " +this.nom +  " " + this.prenom + " |";
	}
 
 
}

mon fichier de configuration Player :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
 
 
<class name="foot.Player" table="PLAYER">
<id name="Player_Id" column="ID_PLAYER" >
<generator class="native"/>
</id>
 
<property name="nom">
<column name="NOM" />
</property>
<property name="prenom"> <column name="PRENOM" />
</property>
 <many-to-one name="team" class="foot.Team"  column="ID_TEAM" cascade="all"  ></many-to-one>

et le fichier de configuration Team :


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
<hibernate-mapping>
<class name="foot.Team" table="TEAM">
<id name="idteam" column="ID_TEAM" >
<generator class="native"/>
</id>
<property name="nom">
<column name="NOM" />
</property>
 
<many-to-one name="coach" class="foot.Coach" column="ID_COACH"  cascade="all" not-null="true"/>
 
<set name="players" cascade="all" inverse="true" >
<key column = "ID_TEAM" not-null="true" />
<one-to-many class="foot.Player"/>
</set>
</class>
</hibernate-mapping>

voici le code permettant de recuperer la liste players :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
 
 Session session = HibernateUtil.getSessionFactory().openSession(); 
 
	    Transaction tx = null; 
	    try { 
	      tx = session.beginTransaction(); 
 
	      Coach coach = new Coach(1,"galles","charles");
 
	      Team team1 = new Team (1,"france",coach);
 
	      Player p1 = new Player(1,"anelka","nicolas");
	      Player p2 = new Player(2,"henry","thierry");
 
 
	     p1.setTeam(team1);
	    session.merge(p1);
	     p2.setTeam(team1);
	     session.merge(p2);	 
	     System.out.println("load Team");
	    Team p = (Team) session.get(Team.class, new Integer(1));
	     System.out.println(p.getNom() + " " +p.getCoach().getNom() + " " + p.getPlayers().size());
 
 
	      session.flush() ;
	      tx.commit();
	    } catch (Exception e) {
	      if (tx != null) {
	        tx.rollback();
	        System.out.println("non inserer");
	      }
	      throw e;
	    } finally { 
	      session.close(); 
 
	    } 
 
	    HibernateUtil.getSessionFactory().close();