-->
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.  [ 4 posts ] 
Author Message
 Post subject: Débutant : problème de mapping, besoin d'aide
PostPosted: Fri Oct 07, 2005 9:06 am 
Newbie

Joined: Fri Oct 07, 2005 8:36 am
Posts: 2
Bonjour,

J'effectue comme travail pour mes cours une appli web de gestion de projet basé sur les EJB3 (donc sur "hibernate anotations" 3.1 beta 5).

A l'état actuel, c'est presque fonctionnel, cependant j'ai un problème bizarre de mapping... dans certains cas, quand je demande une collection de tâches à un objet projet (cf. le diagramme UML et le code plus bas), la collection retournée contient plusieurs foix le même objet tâche (pourtant dans la base de donnée, la table tache est correcte:elle ne contient pas plusieurs fois la même tâche)

En fait, si une tâche possède 2 changement de status, elle sera présente 2 fois dans la collection, etc...

Cela me fait penser à un "produit cartésien", que obtien quand on oublie une condition lors de la jointure de table en SQL...

Comme je suis débutant dans le domaine de java, et encore plus dans le domaine d'hibernate, je me demandais si quelqu'un pourrait jetter un coup d'oeuil à mes relations et me dire ce qu'il en pense. Parce qu'il me semble avoir fait ça correctement d'après la doc... je suis perdu.



Diagramme des relations qui posent problème :

Image

le code des beans :

Code:
@Entity
@Table(name = "PROJET")
public class Project implements java.io.Serializable {
   private static final long serialVersionUID = 1L;

   private int id;

   private String name = "";

   private Collection<Task> tasks = new ArrayList<Task>();

   //private Collection<EtatChange> etatsChange = new ArrayList<EtatChange>();

   public Project() {

   }

   public Project(String nom) {
      this.name = nom;
   }

   @Id(generate = GeneratorType.AUTO)
   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String nom) {
      this.name = nom;
   }

   @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "project")
   public Collection<Task> getTasks() {
      return tasks;
   }

   public void setTasks(Collection<Task> taches) {
      this.tasks = taches;
   }

}
@Entity
@Table(name = "TACHE")
public class Task implements java.io.Serializable {
   private static final long serialVersionUID = 1L;

   private int id;

   private String name;

   private Project project;

   private Collection<StateChange> statesChange = new ArrayList<StateChange>();

   public Task() {
   }

   @Id(generate = GeneratorType.AUTO)
   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String nom) {
      this.name = nom;
   }

   @ManyToOne
   public Project getProject() {
      return project;
   }

   public void setProject(Project projet) {
      this.project = projet;
   }

   @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "task")
   public Collection<StateChange> getStatesChange() {
      return statesChange;
   }

   public void setStatesChange(Collection<StateChange> statutsChange) {
      this.statesChange = statutsChange;
   }

}
@Entity
@Table(name = "STATUTCHANGE")
public class StateChange implements java.io.Serializable {
   private static final long serialVersionUID = 1L;

   private int id;

   private Date dateChange;

   private Task task;

   private State state;

   public StateChange() {

   }

   @Id(generate = GeneratorType.AUTO)
   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public Date getDateChange() {
      return dateChange;
   }

   public void setDateChange(Date dateChange) {
      this.dateChange = dateChange;
   }

   @ManyToOne
   public State getState() {
      return state;
   }

   public void setState(State statutTache) {
      this.state = statutTache;
   }

   @ManyToOne
   public Task getTask() {
      return this.task;
   }

   public void setTask(Task tache) {
      this.task = tache;
   }
}
@Entity
@Table(name = "STATUTTACHE")
public class State implements java.io.Serializable {
   private static final long serialVersionUID = 1L;

   private int id;

   private String name;

   private boolean open;

   public State() {
   }

   public State(String nom) {
      this.setName(nom);
   }

   @Id(generate = GeneratorType.AUTO)
   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String nom) {
      this.name = nom;
   }

   public boolean isOpen() {
      return open;
   }

   public void setOpen(boolean open) {
      this.open = open;
   }

}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 9:53 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Tu charges de manière eager plus d'une collection par entité, ce qui pose un problème de distinction dans le produit cartésien retourné.
Deux solutions :
- ne charge en eager qu'une collection par entity
- n'utilise pas Collection mais Set ce qui vraisemblablement à plus de sens dans ton cas

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 09, 2005 9:30 am 
Newbie

Joined: Fri Oct 07, 2005 8:36 am
Posts: 2
emmanuel wrote:
Tu charges de manière eager plus d'une collection par entité, ce qui pose un problème de distinction dans le produit cartésien retourné.
Deux solutions :
- ne charge en eager qu'une collection par entity
- n'utilise pas Collection mais Set ce qui vraisemblablement à plus de sens dans ton cas


Merci bcp !! C'était exactement ça le problème... En utilisant des Set à la place des collections, le problème ne se pose plus.

J'ai du passer complètement à côté en lisant la doc, mais bon mon anlais c'est pas ça ....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2005 5:45 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
C'est pas dans la doc (seulement un warning), c'est implicite ;-)

_________________
Emmanuel


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

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.