-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Persister un Set d'objets
PostPosted: Wed Apr 23, 2014 9:41 am 
Newbie

Joined: Fri Apr 18, 2014 7:50 pm
Posts: 1
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:


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 Team getTeam() {
      return team;
   }

   public void setTeam(Team team) {
      this.team = team;
   }
}




Team.java

Code:


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 Set<Player> getPlayers() {
      return players;
      }

   public void setPlayers(Set<Player> players) {
      this.players = players;
   }

...


Player.hbm.xml
Code:

<many-to-one name="team" class="foot.Team"  column="ID_TEAM" ></many-to-one>




team.hbm.xml

Code:


      <set name="players" cascade="all, delete-orphan" inverse="true">
         <key column="ID_TEAM" not-null="true" />
         <one-to-many class="foot.Player" />
      </set>



main.java

Code:
   Coach coach = new Coach(1, "galles", "charles");
         Team team1 = new Team(1, "france", coach);
                        team1 = (Team) session.merge(team1); // je rends l'objet team1 persistent
         Set<Player> players = new HashSet<Player>();
         Player pl1 = new Player(1, "anelka", "nicolas");
         pl1.setTeam(team1);

         players.add(pl1);
         pl1 = new Player(2, "henry", "thierry");
         pl1.setTeam(team1);
         players.add(pl1);
         pl1 = new Player(3, "zinedine", "zidane");
         pl1.setTeam(team1);
         players.add(pl1);
         pl1 = new Player(4, "barthez", "fabien");
         pl1.setTeam(team1);
         players.add(pl1);

         team1.setPlayers(players);
         // session.merge(team1);


sortie console :

Code:



Hibernate: alter table PLAYER drop foreign key FK_cy0c8kjv1lt1e3p8xc9fvlvho
Hibernate: alter table TEAM drop foreign key FK_l9anvcxm58gxtu644cvk0cmbb
Hibernate: drop table if exists COACH
Hibernate: drop table if exists PLAYER
Hibernate: drop table if exists PRODUCT
Hibernate: drop table if exists TEAM
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, 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 (?, ?)
affichage equipe : [ zinedine zidane |,  barthez fabien |,  henry thierry |,  anelka nicolas |]
Hibernate: update PLAYER set NOM=?, PRENOM=?, ID_TEAM=? where ID_PLAYER=?
non inserer
Exception in thread "main" org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
   at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:81)
   at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:73)
   at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:63)
   at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3281)
   at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3183)
   at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3525)
   at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:159)
   at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
   at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
   at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
   at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
   at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
   at dao.Test.main(Test.java:113)



Je ne vois pas du tout si c'est un problème d'utilisation de la relation ou autre chose. Pouvez-vous m'aider s'il vous plaît. Merci d'avance.

_________________
back to the futur


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.