Bonjour,
J'utilise actuellement hibernate 4.3.5 final pour la persistance d'objets en base, j'ai une classe Team qui contient un Set de type Player et une classe Player contenant un objet de type Team. Le tout avec une relation de type one-to-many (Team 1 -- * Player) or j'aurais aimé persister une instance Team qui contient un Set d'objets de type Player mais malgré mes nombreuses tentatives je bloque
voici le code :
Player.java
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 package foot; 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; } }
Team.java
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 package foot; import java.util.HashSet; import java.util.Set; public class Team { private int idteam; private String nom; private Set<Player> players; 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; } }
Player.hbm.xml
Team.hbm.xml
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 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping > <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" not-null="true"></many-to-one> </class> </hibernate-mapping>
Et le code effectuant la persistance des données
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 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <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" /> <one-to-many class="foot.Player"/> </set> </class> </hibernate-mapping>
voici ce que j'obtiens lors de l'exécution du code :
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 Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Coach coach = new Coach(1,"galles","charles"); Set<Player> players = new HashSet<Player>(); players.add(new Player(1,"anelka","nicolas")); players.add(new Player(2,"henry","thierry")); players.add(new Player(3,"zinedine","zidane")); players.add(new Player(4,"barthez","fabien")); Team team1 = new Team (1,"france",coach); team1.setPlayers(players); session.merge(team1); session.flush() ; tx.commit(); ...
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 Hibernate: create table COACH (ID_COACH integer not null auto_increment, NOM varchar(255), PRENOM varchar(255), primary key (ID_COACH)) Hibernate: create table PLAYER (ID_PLAYER integer not null auto_increment, NOM varchar(255), PRENOM varchar(255), ID_TEAM integer not null, primary key (ID_PLAYER)) Hibernate: create table PRODUCT (ID_PRODUIT bigint not null, NOM varchar(255), DESCRIPTION varchar(255), PRIX double precision, primary key (ID_PRODUIT)) Hibernate: create table TEAM (ID_TEAM integer not null auto_increment, NOM varchar(255), ID_COACH integer not null, primary key (ID_TEAM)) Hibernate: alter table PLAYER add constraint FK_cy0c8kjv1lt1e3p8xc9fvlvho foreign key (ID_TEAM) references TEAM (ID_TEAM) Hibernate: alter table TEAM add constraint FK_l9anvcxm58gxtu644cvk0cmbb foreign key (ID_COACH) references COACH (ID_COACH) Hibernate: select team0_.ID_TEAM as ID_TEAM1_3_2_, team0_.NOM as NOM2_3_2_, team0_.ID_COACH as ID_COACH3_3_2_, coach1_.ID_COACH as ID_COACH1_0_0_, coach1_.NOM as NOM2_0_0_, coach1_.PRENOM as PRENOM3_0_0_, players2_.ID_TEAM as ID_TEAM4_3_4_, players2_.ID_PLAYER as ID_PLAYE1_1_4_, players2_.ID_PLAYER as ID_PLAYE1_1_1_, players2_.NOM as NOM2_1_1_, players2_.PRENOM as PRENOM3_1_1_, players2_.ID_TEAM as ID_TEAM4_1_1_ from TEAM team0_ inner join COACH coach1_ on team0_.ID_COACH=coach1_.ID_COACH left outer join PLAYER players2_ on team0_.ID_TEAM=players2_.ID_TEAM where team0_.ID_TEAM=? Hibernate: select coach0_.ID_COACH as ID_COACH1_0_0_, coach0_.NOM as NOM2_0_0_, coach0_.PRENOM as PRENOM3_0_0_ from COACH coach0_ where coach0_.ID_COACH=? Hibernate: insert into COACH (NOM, PRENOM) values (?, ?) Hibernate: insert into TEAM (NOM, ID_COACH) values (?, ?) Hibernate: select player0_.ID_PLAYER as ID_PLAYE1_1_0_, player0_.NOM as NOM2_1_0_, player0_.PRENOM as PRENOM3_1_0_, player0_.ID_TEAM as ID_TEAM4_1_0_ from PLAYER player0_ where player0_.ID_PLAYER=? non inserer Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value : foot.Player.team
Merci d'avance.
Partager