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 :

Concurrence optimiste avec Hibernate


Sujet :

Hibernate Java

  1. #1
    Membre du Club
    Profil pro
    Développeur informatique
    Inscrit en
    Février 2007
    Messages
    61
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Février 2007
    Messages : 61
    Points : 52
    Points
    52
    Par défaut Concurrence optimiste avec Hibernate
    Bonjour à tous,

    J'aurais besoin d'aide, afin de gérer la concurrence optimiste d'Hibernate. J'ai fait le tour de la documentation officielle, ainsi que d'autre docs diverses, malheureusement, je ne trouve pas réponse à mes questions. Vous êtes donc mon dernier recours!

    Mise en situation:
    Un projet utilisant Spring et Hibernate. Nous avons des objets mappés à des tables Oracle par Hibernate en utilisant des annotations. Voici un exemple:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    @Entity
    @Table(name = "MEDECIN", uniqueConstraints = {@UniqueConstraint(columnNames = {"NO_PERMIS"})})
     
    public class Medecin extends AbstractMedecin {
    (...)
    }
    Dans celle-çi, conformément à la procédure expliquée (très mal) sur le site d'Hibernate, j'ai ajouté un champ VERSION dans la table MEDECIN, et aussi dans mon objet, avec les annotations pour la concurrence optimiste d'Hibernate:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    @Version
    @Column(name="VERSION")
    protected Integer version;
    Dans ma base de donnée Oracle, le champ VERSION est "INTEGER NOT NULL, DEFAULT 0". Le problème, c'est qu'Hibernate n'incrémente pas du tout ce champ lorsqu'une mise à jour est effectuée sur l'objet mappé. Donc la gestion de concurrence optimiste ne peut pas fonctionner.

    Même en spécifiant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    @org.hibernate.annotations.Entity(optimisticLock = org.hibernate.annotations.OptimisticLockType.VERSION)
    ...au debut de ma classe mappé, ça ne fonctionne tout simplement pas (pas d'incrémentation par Hibernate).

    Merci de me diriger vers des pistes. Le principe semble relativement simple... il y a certainement quelque chose qui m'a échappé.

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

    Informations forums :
    Inscription : Juin 2007
    Messages : 74
    Points : 83
    Points
    83
    Par défaut
    Arf, je vois rien qui pourrait empécher l'incrément par Hibernate...
    Tu importes l'annotation Version depuis quel package ?

    Citation Envoyé par whorian Voir le message
    Bonjour à tous,

    J'aurais besoin d'aide, afin de gérer la concurrence optimiste d'Hibernate. J'ai fait le tour de la documentation officielle, ainsi que d'autre docs diverses, malheureusement, je ne trouve pas réponse à mes questions. Vous êtes donc mon dernier recours!

    Mise en situation:
    Un projet utilisant Spring et Hibernate. Nous avons des objets mappés à des tables Oracle par Hibernate en utilisant des annotations. Voici un exemple:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    @Entity
    @Table(name = "MEDECIN", uniqueConstraints = {@UniqueConstraint(columnNames = {"NO_PERMIS"})})
     
    public class Medecin extends AbstractMedecin {
    (...)
    }
    Dans celle-çi, conformément à la procédure expliquée (très mal) sur le site d'Hibernate, j'ai ajouté un champ VERSION dans la table MEDECIN, et aussi dans mon objet, avec les annotations pour la concurrence optimiste d'Hibernate:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    @Version
    @Column(name="VERSION")
    protected Integer version;
    Dans ma base de donnée Oracle, le champ VERSION est "INTEGER NOT NULL, DEFAULT 0". Le problème, c'est qu'Hibernate n'incrémente pas du tout ce champ lorsqu'une mise à jour est effectuée sur l'objet mappé. Donc la gestion de concurrence optimiste ne peut pas fonctionner.

    Même en spécifiant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    @org.hibernate.annotations.Entity(optimisticLock = org.hibernate.annotations.OptimisticLockType.VERSION)
    ...au debut de ma classe mappé, ça ne fonctionne tout simplement pas (pas d'incrémentation par Hibernate).

    Merci de me diriger vers des pistes. Le principe semble relativement simple... il y a certainement quelque chose qui m'a échappé.

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    59
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mars 2006
    Messages : 59
    Points : 76
    Points
    76
    Par défaut
    Comme le dit denisjava, cela dervait fonctionner, j'utilise exactement le même code excepté que mon champ de version est un Timestamp mais la doc de hibernate précise bien que :
    Les numéros de version doivent avoir les types Hibernate long, integer, short, timestamp ou calendar.
    Donc normalement aucun problème pour toi. Maintenant, ça peut être du a un probleme de Session ou de Transaction peut-être ?
    Il faudrait ton fichier de config Spring et hibernate pour plus de détails.
    Tu peux peut-être aussi spécifier le niveau de log de hibernate à "DEBUG" pour avoir plus de précision sur le SQL qu'il génère. (avec log4j : "log4j.logger.org.hibernate.SQL=DEBUG" )

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

    Informations forums :
    Inscription : Juin 2007
    Messages : 74
    Points : 83
    Points
    83
    Par défaut
    Effectivement, tu dis que le numéro de version n'est pas incrémenté, mais l'enregistrement versionné est il mis à jour: une requete UPDATE est elle générée ?
    elle devrait ressember à: UPDATE ... WHERE ID=XX AND VERSION=XX

    Citation Envoyé par YeFFreY Voir le message
    Comme le dit denisjava, cela dervait fonctionner, j'utilise exactement le même code excepté que mon champ de version est un Timestamp mais la doc de hibernate précise bien que :

    Donc normalement aucun problème pour toi. Maintenant, ça peut être du a un probleme de Session ou de Transaction peut-être ?
    Il faudrait ton fichier de config Spring et hibernate pour plus de détails.
    Tu peux peut-être aussi spécifier le niveau de log de hibernate à "DEBUG" pour avoir plus de précision sur le SQL qu'il génère. (avec log4j : "log4j.logger.org.hibernate.SQL=DEBUG" )

  5. #5
    Membre du Club
    Profil pro
    Développeur informatique
    Inscrit en
    Février 2007
    Messages
    61
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Février 2007
    Messages : 61
    Points : 52
    Points
    52
    Par défaut
    Rebonjour messieux,

    1. Effectivement une requête UPDATE est générée.

    2. Le package utilisé pour les annotations @Version et @Column sont:

    import javax.persistence.Version;
    import javax.persistence.Column;

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

    Informations forums :
    Inscription : Juin 2007
    Messages : 74
    Points : 83
    Points
    83
    Par défaut
    Et dans l'update, il tient compte du champ version ? tu peux nous faire un copier coller de la requete sql ?


    Citation Envoyé par whorian Voir le message
    Rebonjour messieux,

    1. Effectivement une requête UPDATE est générée.

    2. Le package utilisé pour les annotations @Version et @Column sont:

    import javax.persistence.Version;
    import javax.persistence.Column;

  7. #7
    Membre du Club
    Profil pro
    Développeur informatique
    Inscrit en
    Février 2007
    Messages
    61
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Février 2007
    Messages : 61
    Points : 52
    Points
    52
    Par défaut
    applicationContext-hibernate.xml:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    <property name="hibernateProperties">
          <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="log4j.logger.org.hibernate.SQL">DEBUG</prop>
          </props>
    </property>
    Exemple de requête SQL pour la modification d'une entité:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Hibernate: update MEDECIN set OI_SPECIALITE=?, NOM=?, REMARQUES=?, NO_PERMIS=?, PRENOM=?, OI_REGION=?, NO_RAMQ=?, OMNI_SPEC=?, ANNEE_GRADUATION=?, REMARQUES_GRADUATION=?, NO_IMMEUBLE=?, NOM_RUE=?, VILLE=?, PROVINCE=?, CODE_POSTAL=?, TELEPHONE=?, OI_TYPE_RUE=?, NO_PERMIS_TEMPORAIRE=? where OI=?
    Donc pas de champ VERSION modifié par Hibernate. C'est comme si l'annotation @Version n'était pas prise en compte.

  8. #8
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    74
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 74
    Points : 83
    Points
    83
    Par défaut
    Dubitatif...
    je ne vois pas de raison...
    fais voir ta classe persistante ainsi que ton main ?

    Citation Envoyé par whorian Voir le message
    applicationContext-hibernate.xml:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    <property name="hibernateProperties">
          <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="log4j.logger.org.hibernate.SQL">DEBUG</prop>
          </props>
    </property>
    Exemple de requête SQL pour la modification d'une entité:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Hibernate: update MEDECIN set OI_SPECIALITE=?, NOM=?, REMARQUES=?, NO_PERMIS=?, PRENOM=?, OI_REGION=?, NO_RAMQ=?, OMNI_SPEC=?, ANNEE_GRADUATION=?, REMARQUES_GRADUATION=?, NO_IMMEUBLE=?, NOM_RUE=?, VILLE=?, PROVINCE=?, CODE_POSTAL=?, TELEPHONE=?, OI_TYPE_RUE=?, NO_PERMIS_TEMPORAIRE=? where OI=?
    Donc pas de champ VERSION modifié par Hibernate. C'est comme si l'annotation @Version n'était pas prise en compte.

  9. #9
    Membre du Club
    Profil pro
    Développeur informatique
    Inscrit en
    Février 2007
    Messages
    61
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Février 2007
    Messages : 61
    Points : 52
    Points
    52
    Par défaut
    Classe de persistance:


    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
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    package ca.qc.op.bo;
    
    // Generated Nov 28, 2007 10:11:48 AM by Hibernate Tools 3.2.0.beta8
    
    import java.util.Set;
    
    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.MappedSuperclass;
    import javax.persistence.OneToMany;
    import javax.persistence.Version;
    
    import org.hibernate.annotations.*;
    /**
     * AbstractMedecin generated by hbm2java
     */
    @SuppressWarnings("hiding")
    @MappedSuperclass
    public abstract class AbstractMedecin implements java.io.Serializable {
    
        // Fields
    
        // Property hashValue
        private int hashValue = 0;
    
        protected Long oi;
    
        protected TypeRue typeRue;
    
        protected Region region;
    
        protected Specialite specialite;
    
        protected String nom;
    
        protected String prenom;
    
        protected String noPermis;
    
        protected boolean noPermisTemporaire;
    
        protected char omniSpec;
    
        protected Short anneeGraduation;
    
        protected String remarquesGraduation;
    
        protected String noRamq;
    
        protected String remarques;
    
        protected String noImmeuble;
    
        protected String nomRue;
    
        protected String ville;
    
        protected String province;
    
        protected String codePostal;
    
        protected String telephone;
    
        protected Set<AvisConformite> avisConformiteSet;
    
        protected Set<AdhesionAmp> adhesionAmpSet;
    
        protected Set<Occupation> occupationSet;
        
        @Version
        @Column(name="VERSION", nullable = false)
        protected Integer version;
        // Constructors
    
        /** default constructor */
        public AbstractMedecin() {}
    
        // Property accessors
        @Id
        @GenericGenerator(name = "generator", strategy = "increment")
        @GeneratedValue(generator = "generator")
        @Column(name = "OI", unique = false, nullable = false, insertable = true, updatable = true, precision = 18, scale = 0)
        public Long getOi() {
            return this.oi;
        }
    
        public void setOi(Long pOi) {
            this.oi = pOi;
            this.hashValue = 0;
        }
    
        @ManyToOne(cascade = {}, fetch = FetchType.LAZY)
        @JoinColumn(name = "OI_REGION", unique = false, nullable = true, insertable = true, updatable = true)
        public Region getRegion() {
            return this.region;
        }
    
        public void setRegion(Region region) {
            this.region = region;
        }
    
        @ManyToOne(cascade = {}, fetch = FetchType.LAZY)
        @JoinColumn(name = "OI_SPECIALITE", unique = false, nullable = true, insertable = true, updatable = true)
        public Specialite getSpecialite() {
            return this.specialite;
        }
    
        public void setSpecialite(Specialite specialite) {
            this.specialite = specialite;
        }
    
        @ManyToOne(cascade = {}, fetch = FetchType.LAZY)
        @JoinColumn(name = "OI_TYPE_RUE", unique = false, nullable = true, insertable = true, updatable = true)
        public TypeRue getTypeRue() {
            return this.typeRue;
        }
    
        public void setTypeRue(TypeRue typeRue) {
            this.typeRue = typeRue;
        }
    
        @Column(name = "NOM", unique = false, nullable = false, insertable = true, updatable = true, length = 50)
        public String getNom() {
            return this.nom;
        }
    
        public void setNom(String pNom) {
            this.nom = pNom;
        }
    
        @Column(name = "PRENOM", unique = false, nullable = false, insertable = true, updatable = true, length = 50)
        public String getPrenom() {
            return this.prenom;
        }
    
        public void setPrenom(String prenom) {
            this.prenom = prenom;
        }
    
        @Column(name = "NO_PERMIS", unique = false, nullable = false, insertable = true, updatable = true, length = 20)
        public String getNoPermis() {
            return this.noPermis;
        }
    
        public void setNoPermis(String noPermis) {
            this.noPermis = noPermis;
        }
    
        @Column(name = "NO_PERMIS_TEMPORAIRE", unique = false, nullable = false, insertable = true, updatable = true, precision = 1, scale = 0)
        public boolean getNoPermisTemporaire() {
            return this.noPermisTemporaire;
        }
    
        public void setNoPermisTemporaire(boolean noPermisTemporaire) {
            this.noPermisTemporaire = noPermisTemporaire;
        }
    
        @Column(name = "OMNI_SPEC", unique = false, nullable = false, insertable = true, updatable = true, length = 1)
        public char getOmniSpec() {
            return this.omniSpec;
        }
    
        public void setOmniSpec(char omniSpec) {
            this.omniSpec = omniSpec;
        }
    
        @Column(name = "ANNEE_GRADUATION", unique = false, nullable = true, insertable = true, updatable = true, precision = 4, scale = 0)
        public Short getAnneeGraduation() {
            return this.anneeGraduation;
        }
    
        public void setAnneeGraduation(Short anneeGraduation) {
            this.anneeGraduation = anneeGraduation;
        }
    
        @Column(name = "REMARQUES_GRADUATION", unique = false, nullable = true, insertable = true, updatable = true, length = 4000)
        public String getRemarquesGraduation() {
            return this.remarquesGraduation;
        }
    
        public void setRemarquesGraduation(String remarquesGraduation) {
            this.remarquesGraduation = remarquesGraduation;
        }
    
        @Column(name = "NO_RAMQ", unique = false, nullable = true, insertable = true, updatable = true, length = 50)
        public String getNoRamq() {
            return this.noRamq;
        }
    
        public void setNoRamq(String noRamq) {
            this.noRamq = noRamq;
        }
    
        @Column(name = "REMARQUES", unique = false, nullable = true, insertable = true, updatable = true, length = 4000)
        public String getRemarques() {
            return this.remarques;
        }
    
        public void setRemarques(String remarques) {
            this.remarques = remarques;
        }
    
        @Column(name = "NO_IMMEUBLE", unique = false, nullable = true, insertable = true, updatable = true, length = 10)
        public String getNoImmeuble() {
            return this.noImmeuble;
        }
    
        public void setNoImmeuble(String noImmeuble) {
            this.noImmeuble = noImmeuble;
        }
    
        @Column(name = "NOM_RUE", unique = false, nullable = true, insertable = true, updatable = true, length = 100)
        public String getNomRue() {
            return this.nomRue;
        }
    
        public void setNomRue(String nomRue) {
            this.nomRue = nomRue;
        }
    
        @Column(name = "VILLE", unique = false, nullable = true, insertable = true, updatable = true, length = 100)
        public String getVille() {
            return this.ville;
        }
    
        public void setVille(String ville) {
            this.ville = ville;
        }
    
        @Column(name = "PROVINCE", unique = false, nullable = true, insertable = true, updatable = true, length = 50)
        public String getProvince() {
            return this.province;
        }
    
        public void setProvince(String province) {
            this.province = province;
        }
    
        @Column(name = "CODE_POSTAL", unique = false, nullable = true, insertable = true, updatable = true, length = 7)
        public String getCodePostal() {
            return this.codePostal;
        }
    
        public void setCodePostal(String codePostal) {
            this.codePostal = codePostal;
        }
    
        @Column(name = "TELEPHONE", unique = false, nullable = true, insertable = true, updatable = true, length = 14)
        public String getTelephone() {
            return this.telephone;
        }
    
        public void setTelephone(String telephone) {
            this.telephone = telephone;
        }
    
        public void setAvisConformiteSet(Set<AvisConformite> avisConformiteSet) {
            this.avisConformiteSet = avisConformiteSet;
        }
    
    
        public void setAdhesionAmpSet(Set<AdhesionAmp> adhesionAmpSet) {
            this.adhesionAmpSet = adhesionAmpSet;
        }
        public void setOccupationSet(Set<Occupation> occupationSet) {
            this.occupationSet = occupationSet;
        }
    
        /**
         * toString
         * 
         * @return String
         */
        @Override
        public String toString() {
            StringBuffer buffer = new StringBuffer();
    
            buffer.append(this.getClass().getName()).append("@").append(Integer.toHexString(this.hashCode())).append(" [");//$NON-NLS-1$//$NON-NLS-2$
            buffer.append("]");//$NON-NLS-1$
    
            return buffer.toString();
        }
    
        @Override
        public boolean equals(Object other) {
            if ((this == other))
                return true;
            if ((other == null))
                return false;
            if (!(other instanceof AbstractMedecin))
                return false;
            AbstractMedecin castOther = (AbstractMedecin)other;
    
            return castOther.getOi().equals(this.getOi());
        }
    
        /**
         * Implementation of the hashCode method conforming to the Bloch pattern
         * with the exception of array properties (these are very unlikely primary
         * key types).
         * 
         * @return int hashCode
         */
        @Override
        public int hashCode() {
            if (this.hashValue == 0) {
                int result = 17;
                int oiValue = this.oi == null ? 0 : this.oi.hashCode();
                result = result * 37 + oiValue;
                this.hashValue = result;
            }
            return this.hashValue;
        }
    }

  10. #10
    Candidat au Club
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 7
    Points : 3
    Points
    3
    Par défaut
    Citation Envoyé par whorian Voir le message
    Classe de persistance:


    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
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    package ca.qc.op.bo;
    
    // Generated Nov 28, 2007 10:11:48 AM by Hibernate Tools 3.2.0.beta8
    
    import java.util.Set;
    
    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.MappedSuperclass;
    import javax.persistence.OneToMany;
    import javax.persistence.Version;
    
    import org.hibernate.annotations.*;
    /**
     * AbstractMedecin generated by hbm2java
     */
    @SuppressWarnings("hiding")
    @MappedSuperclass
    public abstract class AbstractMedecin implements java.io.Serializable {
    
        // Fields
    
        // Property hashValue
        private int hashValue = 0;
    
        protected Long oi;
    
        protected TypeRue typeRue;
    
        protected Region region;
    
        protected Specialite specialite;
    
        protected String nom;
    
        protected String prenom;
    
        protected String noPermis;
    
        protected boolean noPermisTemporaire;
    
        protected char omniSpec;
    
        protected Short anneeGraduation;
    
        protected String remarquesGraduation;
    
        protected String noRamq;
    
        protected String remarques;
    
        protected String noImmeuble;
    
        protected String nomRue;
    
        protected String ville;
    
        protected String province;
    
        protected String codePostal;
    
        protected String telephone;
    
        protected Set<AvisConformite> avisConformiteSet;
    
        protected Set<AdhesionAmp> adhesionAmpSet;
    
        protected Set<Occupation> occupationSet;
        
        @Version
        @Column(name="VERSION", nullable = false)
        protected Integer version;
        // Constructors
    
        /** default constructor */
        public AbstractMedecin() {}
    
        // Property accessors
        @Id
        @GenericGenerator(name = "generator", strategy = "increment")
        @GeneratedValue(generator = "generator")
        @Column(name = "OI", unique = false, nullable = false, insertable = true, updatable = true, precision = 18, scale = 0)
        public Long getOi() {
            return this.oi;
        }
    
        public void setOi(Long pOi) {
            this.oi = pOi;
            this.hashValue = 0;
        }
    
        @ManyToOne(cascade = {}, fetch = FetchType.LAZY)
        @JoinColumn(name = "OI_REGION", unique = false, nullable = true, insertable = true, updatable = true)
        public Region getRegion() {
            return this.region;
        }
    
        public void setRegion(Region region) {
            this.region = region;
        }
    
        @ManyToOne(cascade = {}, fetch = FetchType.LAZY)
        @JoinColumn(name = "OI_SPECIALITE", unique = false, nullable = true, insertable = true, updatable = true)
        public Specialite getSpecialite() {
            return this.specialite;
        }
    
        public void setSpecialite(Specialite specialite) {
            this.specialite = specialite;
        }
    
        @ManyToOne(cascade = {}, fetch = FetchType.LAZY)
        @JoinColumn(name = "OI_TYPE_RUE", unique = false, nullable = true, insertable = true, updatable = true)
        public TypeRue getTypeRue() {
            return this.typeRue;
        }
    
        public void setTypeRue(TypeRue typeRue) {
            this.typeRue = typeRue;
        }
    
        @Column(name = "NOM", unique = false, nullable = false, insertable = true, updatable = true, length = 50)
        public String getNom() {
            return this.nom;
        }
    
        public void setNom(String pNom) {
            this.nom = pNom;
        }
    
        @Column(name = "PRENOM", unique = false, nullable = false, insertable = true, updatable = true, length = 50)
        public String getPrenom() {
            return this.prenom;
        }
    
        public void setPrenom(String prenom) {
            this.prenom = prenom;
        }
    
        @Column(name = "NO_PERMIS", unique = false, nullable = false, insertable = true, updatable = true, length = 20)
        public String getNoPermis() {
            return this.noPermis;
        }
    
        public void setNoPermis(String noPermis) {
            this.noPermis = noPermis;
        }
    
        @Column(name = "NO_PERMIS_TEMPORAIRE", unique = false, nullable = false, insertable = true, updatable = true, precision = 1, scale = 0)
        public boolean getNoPermisTemporaire() {
            return this.noPermisTemporaire;
        }
    
        public void setNoPermisTemporaire(boolean noPermisTemporaire) {
            this.noPermisTemporaire = noPermisTemporaire;
        }
    
        @Column(name = "OMNI_SPEC", unique = false, nullable = false, insertable = true, updatable = true, length = 1)
        public char getOmniSpec() {
            return this.omniSpec;
        }
    
        public void setOmniSpec(char omniSpec) {
            this.omniSpec = omniSpec;
        }
    
        @Column(name = "ANNEE_GRADUATION", unique = false, nullable = true, insertable = true, updatable = true, precision = 4, scale = 0)
        public Short getAnneeGraduation() {
            return this.anneeGraduation;
        }
    
        public void setAnneeGraduation(Short anneeGraduation) {
            this.anneeGraduation = anneeGraduation;
        }
    
        @Column(name = "REMARQUES_GRADUATION", unique = false, nullable = true, insertable = true, updatable = true, length = 4000)
        public String getRemarquesGraduation() {
            return this.remarquesGraduation;
        }
    
        public void setRemarquesGraduation(String remarquesGraduation) {
            this.remarquesGraduation = remarquesGraduation;
        }
    
        @Column(name = "NO_RAMQ", unique = false, nullable = true, insertable = true, updatable = true, length = 50)
        public String getNoRamq() {
            return this.noRamq;
        }
    
        public void setNoRamq(String noRamq) {
            this.noRamq = noRamq;
        }
    
        @Column(name = "REMARQUES", unique = false, nullable = true, insertable = true, updatable = true, length = 4000)
        public String getRemarques() {
            return this.remarques;
        }
    
        public void setRemarques(String remarques) {
            this.remarques = remarques;
        }
    
        @Column(name = "NO_IMMEUBLE", unique = false, nullable = true, insertable = true, updatable = true, length = 10)
        public String getNoImmeuble() {
            return this.noImmeuble;
        }
    
        public void setNoImmeuble(String noImmeuble) {
            this.noImmeuble = noImmeuble;
        }
    
        @Column(name = "NOM_RUE", unique = false, nullable = true, insertable = true, updatable = true, length = 100)
        public String getNomRue() {
            return this.nomRue;
        }
    
        public void setNomRue(String nomRue) {
            this.nomRue = nomRue;
        }
    
        @Column(name = "VILLE", unique = false, nullable = true, insertable = true, updatable = true, length = 100)
        public String getVille() {
            return this.ville;
        }
    
        public void setVille(String ville) {
            this.ville = ville;
        }
    
        @Column(name = "PROVINCE", unique = false, nullable = true, insertable = true, updatable = true, length = 50)
        public String getProvince() {
            return this.province;
        }
    
        public void setProvince(String province) {
            this.province = province;
        }
    
        @Column(name = "CODE_POSTAL", unique = false, nullable = true, insertable = true, updatable = true, length = 7)
        public String getCodePostal() {
            return this.codePostal;
        }
    
        public void setCodePostal(String codePostal) {
            this.codePostal = codePostal;
        }
    
        @Column(name = "TELEPHONE", unique = false, nullable = true, insertable = true, updatable = true, length = 14)
        public String getTelephone() {
            return this.telephone;
        }
    
        public void setTelephone(String telephone) {
            this.telephone = telephone;
        }
    
        public void setAvisConformiteSet(Set<AvisConformite> avisConformiteSet) {
            this.avisConformiteSet = avisConformiteSet;
        }
    
    
        public void setAdhesionAmpSet(Set<AdhesionAmp> adhesionAmpSet) {
            this.adhesionAmpSet = adhesionAmpSet;
        }
        public void setOccupationSet(Set<Occupation> occupationSet) {
            this.occupationSet = occupationSet;
        }
    
        /**
         * toString
         * 
         * @return String
         */
        @Override
        public String toString() {
            StringBuffer buffer = new StringBuffer();
    
            buffer.append(this.getClass().getName()).append("@").append(Integer.toHexString(this.hashCode())).append(" [");//$NON-NLS-1$//$NON-NLS-2$
            buffer.append("]");//$NON-NLS-1$
    
            return buffer.toString();
        }
    
        @Override
        public boolean equals(Object other) {
            if ((this == other))
                return true;
            if ((other == null))
                return false;
            if (!(other instanceof AbstractMedecin))
                return false;
            AbstractMedecin castOther = (AbstractMedecin)other;
    
            return castOther.getOi().equals(this.getOi());
        }
    
        /**
         * Implementation of the hashCode method conforming to the Bloch pattern
         * with the exception of array properties (these are very unlikely primary
         * key types).
         * 
         * @return int hashCode
         */
        @Override
        public int hashCode() {
            if (this.hashValue == 0) {
                int result = 17;
                int oiValue = this.oi == null ? 0 : this.oi.hashCode();
                result = result * 37 + oiValue;
                this.hashValue = result;
            }
            return this.hashValue;
        }
    }
    Tes annotations sont sur les méthodes alors que celle @Version est sur un champ. Hibernate se base sur la position du @Id qui est ici sur une méthode il ne prend en compte que les annotations des méthodes et pas celles des champs. C'est soit toutes les annotations sur les champs soit toutes sur les méthodes.

    Extrait de la doc :

    Selon que vous annotez des champs ou des méthodes, le type d'accès utilisé par Hibernate sera field ou property. La spécification EJB3 exige que vous déclariez les annotations sur le type d'élément qui sera accédé, c'est-à-dire le getter si vous utilisez l'accès property, le champ si vous utilisez l'accès field. Mélanger des EJB3 annotations dans les champs et les méthodes devrait être évité. Hibernate devinera le type d'accès de l'identifiant à partir de la position d'@Id ou d'@EmbeddedId.

  11. #11
    Membre du Club
    Profil pro
    Développeur informatique
    Inscrit en
    Février 2007
    Messages
    61
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Février 2007
    Messages : 61
    Points : 52
    Points
    52
    Par défaut
    MERCI!!!!

  12. #12
    Membre du Club
    Profil pro
    Développeur informatique
    Inscrit en
    Février 2007
    Messages
    61
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Février 2007
    Messages : 61
    Points : 52
    Points
    52
    Par défaut
    Mon champ VERSION est maintenant incrémenté. Cependant, la gestion de la concurrence ne peut pas fonctionner, parce que Hibernate me fait des requêtes UPDATE pour l'affichage d'une entité (read-only).

    C'est pourquoi maintenant, toute modification d'une entité me lance une exception:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: JDBC commit failed
     
    cause mère 
     
    java.sql.SQLException: ORA-02091: transaction annulée
    ORA-00001: violation de contrainte unique (ALT_MEDECIN)

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

Discussions similaires

  1. Réponses: 2
    Dernier message: 22/04/2011, 12h09
  2. [Hibernate] Problème avec Hibernate et Eclipse 3
    Par theseuby dans le forum Eclipse Java
    Réponses: 1
    Dernier message: 30/03/2006, 21h31
  3. Connection de SQL SERVER 2005 EXPRESS avec HIBERNATE
    Par jerome giraud dans le forum MS SQL Server
    Réponses: 1
    Dernier message: 27/02/2006, 09h07
  4. [Plugin][Hibernate]Eclipse avec hibernate(synchronizer)
    Par mg67 dans le forum Eclipse Java
    Réponses: 7
    Dernier message: 23/06/2005, 17h19
  5. Problèmes avec Hibernate (sous Eclipse)
    Par Pierric dans le forum Hibernate
    Réponses: 2
    Dernier message: 07/04/2005, 14h35

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