IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Hibernate Java Discussion :

Contrainte d'intégrite Hibernate


Sujet :

Hibernate Java

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Août 2008
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2008
    Messages : 16
    Par défaut Contrainte d'intégrite Hibernate
    Bonjour,
    Voila mon problème, j'ai 2 tables Lot et Fonction avec l'id du lot en foreignKey dans la table fonction.

    Au niveau des Entités j'ai tout bien déclaré avec dans la classe Lot
    @OneToMany(mappedBy = "lot", cascade = { CascadeType.REMOVE })
    private Set<Fonction> fonctions = new HashSet<Fonction>();
    Quand je créé un lot puis une fonction, et que je cherche a supprimer ce lot assez rapidement j'ai une erreur de contrainte d'intégrité qui est lancé. Car Hibernate lance les requêtes suivantes :
    select projet0_.PRO_ID as PRO1_0_, projet0_.PRO_VERSION as PRO2_0_, projet0_.PRO_NOM as PRO3_0_ from T_PROJET_PRO projet0_ order by projet0_.PRO_NOM
    2008-08-29 11:29:19 959 DEBUG - update T_LOT_LOT set LOT_VERSION=?, LOT_NOM=?, PRO_ID=? where LOT_ID=? and LOT_VERSION=?
    2008-08-29 11:29:19 997 DEBUG - delete from T_LOT_LOT where LOT_ID=? and LOT_VERSION=?
    Par contre si j'attends un certain temps, la suppression du lot se fait correctement car Hibernate lance les requêtes suivantes :
    select fonctions0_.LOT_ID as LOT4_1_, fonctions0_.FCT_ID as FCT1_1_, fonctions0_.FCT_ID as FCT1_9_0_, fonctions0_.FCT_VERSION as FCT2_9_0_, fonctions0_.FCT_NOM as FCT3_9_0_, fonctions0_.LOT_ID as LOT4_9_0_ from T_FONCTION_FCT fonctions0_ where fonctions0_.LOT_ID=?
    2008-08-29 11:32:50 715 DEBUG - select ftc0_.FCT_ID as FCT2_3_, ftc0_.TYP_ID as TYP1_3_, ftc0_.CPX_ID as CPX3_3_, ftc0_.TYP_ID as TYP1_2_2_, ftc0_.FCT_ID as FCT2_2_2_, ftc0_.CPX_ID as CPX3_2_2_, ftc0_.FTC_NB_JH as FTC4_2_2_, type1_.TYP_ID as TYP1_5_0_, type1_.TYP_VERSION as TYP2_5_0_, type1_.TYP_NOM as TYP3_5_0_, complexite2_.CPX_ID as CPX1_7_1_, complexite2_.CPX_VERSION as CPX2_7_1_, complexite2_.CPX_NOM as CPX3_7_1_ from TJ_FCT_TYP_CPX_FTC ftc0_ left outer join T_TYPE_TYP type1_ on ftc0_.TYP_ID=type1_.TYP_ID left outer join T_COMPLEXITE_CPX complexite2_ on ftc0_.CPX_ID=complexite2_.CPX_ID where ftc0_.FCT_ID=?
    2008-08-29 11:32:50 728 DEBUG - select etapes0_.LOT_ID as LOT2_2_, etapes0_.ETP_ID as ETP1_2_, etapes0_.ETP_ID as ETP1_1_1_, etapes0_.LOT_ID as LOT2_1_1_, etapes0_.JEL_COEF as JEL3_1_1_, etape1_.ETP_ID as ETP1_8_0_, etape1_.ETP_VERSION as ETP2_8_0_, etape1_.ETP_NOM as ETP3_8_0_, etape1_.ETP_POURCENTAGE as ETP4_8_0_, etape1_.PHA_ID as PHA5_8_0_ from TJ_ETP_LOT_JEL etapes0_ left outer join T_ETAPE_ETP etape1_ on etapes0_.ETP_ID=etape1_.ETP_ID where etapes0_.LOT_ID=?
    2008-08-29 11:32:50 730 DEBUG - select ltc0_.LOT_ID as LOT2_3_, ltc0_.TYP_ID as TYP1_3_, ltc0_.CPX_ID as CPX3_3_, ltc0_.TYP_ID as TYP1_10_2_, ltc0_.LOT_ID as LOT2_10_2_, ltc0_.CPX_ID as CPX3_10_2_, ltc0_.LTC_COEF as LTC4_10_2_, type1_.TYP_ID as TYP1_5_0_, type1_.TYP_VERSION as TYP2_5_0_, type1_.TYP_NOM as TYP3_5_0_, complexite2_.CPX_ID as CPX1_7_1_, complexite2_.CPX_VERSION as CPX2_7_1_, complexite2_.CPX_NOM as CPX3_7_1_ from TJ_LOT_TYP_CPX_LTC ltc0_ left outer join T_TYPE_TYP type1_ on ltc0_.TYP_ID=type1_.TYP_ID left outer join T_COMPLEXITE_CPX complexite2_ on ltc0_.CPX_ID=complexite2_.CPX_ID where ltc0_.LOT_ID=?
    2008-08-29 11:32:50 732 DEBUG - update T_LOT_LOT set LOT_VERSION=?, LOT_NOM=?, PRO_ID=? where LOT_ID=? and LOT_VERSION=?
    2008-08-29 11:32:50 734 DEBUG - update T_FONCTION_FCT set FCT_VERSION=?, FCT_NOM=?, LOT_ID=? where FCT_ID=? and FCT_VERSION=?
    2008-08-29 11:32:50 737 DEBUG - delete from T_FONCTION_FCT where FCT_ID=? and FCT_VERSION=?
    2008-08-29 11:32:50 739 DEBUG - delete from T_LOT_LOT where LOT_ID=? and LOT_VERSION=?
    J'aimerai savoir si c'est normal. Et s'il s'agit d'une configuration que j'aurais mal faite quelque part.

  2. #2
    Membre chevronné
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    383
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 383
    Par défaut
    Tu as essayé avec CascadeType.DELETE_ORPHAN ?

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Août 2008
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2008
    Messages : 16
    Par défaut
    ça signifie quoi "CascadeType.DELETE_ORPHAN" ?

    J'ai essayé mais il ne connait pas. J'ai aussi essayé CascadeType.ALL

  4. #4
    Membre chevronné
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    383
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 383
    Par défaut
    Ca veut dire qu'il va supprimer les fils si le père est supprimé

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Août 2008
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2008
    Messages : 16
    Par défaut
    En fait, à cette endroit là
    // relation inverse Lot (one) -> Fonction (many)
    @OneToMany(mappedBy = "lot", cascade = { CascadeType.REMOVE })
    private Set<Fonction> fonctions = new HashSet<Fonction>();
    Il veut quelquechose du type javax.persistence

    Or CascadeType.DELETE_ORPHAN est de type org.hibernate.annotations, c'est pourquoi il en veut pas.

    Je ne peux utiliser que
    CascadeType.REMOVE
    CascadeType.REFRESH
    CascadeType.PERSIST
    CascadeType.MERGE
    CascadeType.ALL

  6. #6
    Membre chevronné
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    383
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 383
    Par défaut
    Tu as essayé avec ça :
    @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)

  7. #7
    Membre averti
    Profil pro
    Inscrit en
    Août 2008
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2008
    Messages : 16
    Par défaut
    Finalement après pas mal de test. Ce qui solutionne mon problème, c'est qu'au moment de la création d'une fonction, il faut que j'utilise la méthode addFonction de la classe Lot. Ainsi dès la création de la fonction, celle ci est associée au lot, et après la suppression d'un lot (quand celle ci est rapide après la création) fonctionne correctement.

    En tout cas merci d'avoir répondu à mon post.

    Si quelqu'un a le même problème, voici ma classe Fonction

    import java.io.Serializable;
    import java.util.HashSet;
    import java.util.Set;

    import javax.persistence.*;


    @Entity
    @Table(name="T_FONCTION_FCT")

    public class Fonction implements Serializable{
    //Champs
    @Id
    @Column(name = "FCT_ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name = "FCT_VERSION", nullable = false)
    @Version
    private int version;

    @Column(name = "FCT_NOM", length = 255, nullable = false)
    private String nom;

    // relation principale Fonction (many) -> Lot (one)
    // implémentée par une clé étrangère (LOT_ID) dans Fonction
    // 1 Fonction a nécessairement 1 Lot (nullable=false)
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "LOT_ID", nullable = false)
    private Lot lot;

    //relation inverse Fonction (one) -> FonctionTypeComplexite (many)
    //cascade suppression Fonction -> supression FonctionTypeComplexite
    //TODO verifier la cascade
    @OneToMany(mappedBy = "fct", cascade = { CascadeType.REMOVE })
    private Set<FonctionTypeComplexite> ftc = new HashSet<FonctionTypeComplexite>();


    //Constructeurs
    public Fonction(){

    }

    public Fonction(String nom){
    setNom(nom);
    }

    //Getters et Setters
    public Integer getId() {
    return id;
    }

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

    public String getNom() {
    return nom;
    }

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

    public Lot getLot() {
    return lot;
    }

    public void setLot(Lot lot) {
    this.lot = lot;
    }

    public int getVersion() {
    return version;
    }

    public void setVersion(int version) {
    this.version = version;
    }

    public Set<FonctionTypeComplexite> getFtc() {
    return ftc;
    }

    public void setFtc(Set<FonctionTypeComplexite> ftc) {
    this.ftc = ftc;
    }


    }
    Ma classe Lot
    package abaque.metier;

    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;

    import javax.persistence.*;

    import sun.util.logging.resources.logging;


    @Entity
    @Table(name="T_LOT_LOT")

    public class Lot{

    //Champs
    @Id
    @Column(name = "LOT_ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name = "LOT_VERSION", nullable = false)
    @Version
    private int version;

    @Column(name = "LOT_NOM", length = 255, nullable = false)
    private String nom;

    // relation principale Lot (many) -> Projet (one)
    // implémentée par une clé étrangère (PRO_ID) dans Lot
    // 1 Lot a nécessairement 1 Projet (nullable=false)
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "PRO_ID", nullable = false)
    private Projet projet;

    // relation inverse Lot (one) -> Fonction (many)
    @OneToMany(fetch=FetchType.LAZY, mappedBy = "lot", cascade = { CascadeType.REMOVE })
    private Set<Fonction> fonctions = new HashSet<Fonction>();

    // relation inverse Lot (one) -> PhaseLot (many)
    // cascade suppression Lot -> supression PhaseLot
    @OneToMany(mappedBy = "lot2", cascade = { CascadeType.REMOVE })
    private Set<EtapeLot> etapes = new HashSet<EtapeLot>();

    //relation inverse Lot (one) -> LotTypeComplexite (many)
    //cascade suppression Phase -> supression LotTypeComplexite
    //TODO verifier la cascade
    @OneToMany(mappedBy = "lot3", cascade = { CascadeType.REMOVE })
    private Set<LotTypeComplexite> ltc = new HashSet<LotTypeComplexite>();


    //Constructeurs
    public Lot(){

    }

    public Lot (String nom){
    setNom(nom);
    }

    //association bidirectionnelle Lot <--> Fonction
    public void addFonction(Fonction fct){
    // la fonction est ajoutée dans la collection des fonctions du lot
    fonctions.add(fct);
    // la fonction change de lot
    fct.setLot(this);

    }

    //Getters et Setters
    public Integer getId() {
    return id;
    }

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

    public String getNom() {
    return nom;
    }

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

    public int getVersion() {
    return version;
    }

    public void setVersion(int version) {
    this.version = version;
    }

    public Projet getProjet() {
    return projet;
    }

    public void setProjet(Projet projet) {
    this.projet = projet;
    }

    public Set<Fonction> getFonctions() {
    return fonctions;
    }

    public void setFonctions(Set<Fonction> fonctions) {
    this.fonctions = fonctions;
    }

    public Set<EtapeLot> getEtapes() {
    return etapes;
    }

    public void setEtapes(Set<EtapeLot> etapes) {
    this.etapes = etapes;
    }

    public Set<LotTypeComplexite> getLtc() {
    return ltc;
    }

    public void setLtc(Set<LotTypeComplexite> ltc) {
    this.ltc = ltc;
    }

    }
    et la méthode qui est appelée lors de la création d'une fonction :
    public Fonction createFonction(Fonction fct){
    tx.begin();
    Lot lot = em.find(Lot.class, fct.getLot().getId());
    lot.addFonction(fct);
    em.persist(fct);
    tx.commit();

    return fct;
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Merise : Contrainte d'intégrite fonctionnelle
    Par new_wave dans le forum Décisions SGBD
    Réponses: 2
    Dernier message: 22/06/2022, 12h51
  2. contrainte d'intégrité super dur a gérer !
    Par RockLee69 dans le forum Oracle
    Réponses: 3
    Dernier message: 30/11/2005, 16h02
  3. Réponses: 5
    Dernier message: 26/10/2005, 15h43
  4. [debutant] Contraintes d'intégrité définies sur un objet
    Par maysa dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 25/05/2004, 15h57
  5. Question sur les contraintes d'intégrités
    Par eGGyyS dans le forum PostgreSQL
    Réponses: 3
    Dernier message: 27/04/2004, 14h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo