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

JPA Java Discussion :

Pertinence d'ignorer mon id et ma version de hashCode


Sujet :

JPA Java

  1. #1
    Membre confirmé
    Avatar de Khaled.Noordin
    Homme Profil pro
    Inscrit en
    Janvier 2005
    Messages
    354
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2005
    Messages : 354
    Points : 497
    Points
    497
    Billets dans le blog
    1
    Par défaut Pertinence d'ignorer mon id et ma version de hashCode
    Bonjour/Bonsoir
    Je me pose la question si il est utile de garder l'identifiant de mon entité et sa version dans mon hashCode de la même façon pour equals et par extension pour compareTo.
    J'utilise les fonction du package org.apache.commons.lang.builder pour implémenter mes méthodes.
    Et pourquoi, car mon identifiant n'est pas naturel et dépend de la persistance et ne me semble pas un candidat à la comparaison ou à la génération de hash de l'ensemble des éléments naturellement unique.
    voici mon code
    Code java : 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
    package kn.organization.domain;
     
    import org.apache.commons.lang.builder.EqualsBuilder;
    import org.apache.commons.lang.builder.HashCodeBuilder;
    import org.apache.commons.lang.builder.ToStringBuilder;
     
     
    import javax.validation.constraints.NotNull;
    import javax.validation.constraints.Size;
    import java.io.Serializable;
    import java.util.Date;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.NamedQueries;
    import javax.persistence.NamedQuery;
    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;
    import javax.persistence.Version;
    import org.apache.commons.lang.builder.ToStringStyle;
     
    /**
     *
     * @author Khaled.Noordin
     */
    @Entity
    @Table(name = "foo")
    @NamedQueries({
     @NamedQuery(name = "Foo.findAll", query = "SELECT f FROM Foo f"),
     @NamedQuery(name = "Foo.findByBar", query = "SELECT f FROM Foo f WHERE f.bar = :bar"),
     @NamedQuery(name = "Foo.findByBaz", query = "SELECT f FROM Foo f WHERE f.baz = :baz"),
     @NamedQuery(name = "Foo.findByStartDate", query = "SELECT f FROM Foo f WHERE f.startDate = :startDate"),
     @NamedQuery(name = "Foo.findBetweenStartDates", query = "SELECT f FROM Foo f WHERE f.startDate between :date1 and :date2")
    })
    public class Foo implements Serializable {
     
     private static final long serialVersionUID = -1512051317276019769L;
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     private Long id;
     @Version
     private Integer version;
     @NotNull
     @Size(min = 1, max = 6)
     private String bar;
     @Column(name = "baz", unique = true)
     private Long baz;
     @Temporal(TemporalType.TIMESTAMP)
     @NotNull
     @Column(name = "start_date")
     private Date startDate;
     
     public Foo() {
      this.id = null;
      this.bar = null;
      this.baz = null;
      this.startDate = null;
     }
     
     public Foo(String bar, Long baz, Date startDate) {
      this();
      this.bar = bar;
      this.baz = baz;
      this.startDate = startDate;
     }
     
     public Foo(Long id, Integer version, String bar, Long baz, Date startDate) {
      this.id = id;
      this.version = version;
      this.bar = bar;
      this.baz = baz;
      this.startDate = startDate;
     }
     
     public String getBar() {
      return bar;
     }
     
     public void setBar(String bar) {
      this.bar = bar;
     }
     
     public Long getId() {
      return id;
     }
     
     public void setId(Long id) {
      this.id = id;
     }
     
     public Long getBaz() {
      return baz;
     }
     
     public void setBaz(Long baz) {
      this.baz = baz;
     }
     
     public Date getStartDate() {
      return startDate;
     }
     
     public void setStartDate(Date startDate) {
      this.startDate = startDate;
     }
     
     public Integer getVersion() {
      return version;
     }
     
     public void setVersion(Integer version) {
      this.version = version;
     }
     
     @Override
     public String toString() {
      return ToStringBuilder.reflectionToString(
              this,
              ToStringStyle.SHORT_PREFIX_STYLE);
     }
     
     /**
      * 
      * @return 
      */
     @Override
     public int hashCode() {
      return HashCodeBuilder.reflectionHashCode(
              this,
              new String[]{
               "id",
               "version"
              });
     }
     
     /**
      * 
      * @param object
      * @return 
      */
     @Override
     public boolean equals(Object object) {
      return ( !( object instanceof Foo )
              || object == null ) ? false
              : EqualsBuilder.reflectionEquals(
              this,
              object,
              new String[]{
               "id"
              });
     }
    }

    cordialement Khaled.Noordin

  2. #2
    Membre confirmé
    Avatar de Khaled.Noordin
    Homme Profil pro
    Inscrit en
    Janvier 2005
    Messages
    354
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2005
    Messages : 354
    Points : 497
    Points
    497
    Billets dans le blog
    1
    Par défaut
    modification de code
    ne pas tenir compte du constructeur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     public Foo(Long id, Integer version, String bar, Long baz, Date startDate) {
      this.id = id;
      this.version = version;
      this.bar = bar;
      this.baz = baz;
      this.startDate = startDate;
     }

  3. #3
    Membre confirmé
    Avatar de Khaled.Noordin
    Homme Profil pro
    Inscrit en
    Janvier 2005
    Messages
    354
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2005
    Messages : 354
    Points : 497
    Points
    497
    Billets dans le blog
    1
    Par défaut
    Ma solution est de toujours ignorer id et version dans un equals

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

Discussions similaires

  1. Stupid Run, mon deuxième jeu (nouvelle version!!!)
    Par lypnol dans le forum Développement 2D, 3D et Jeux
    Réponses: 2
    Dernier message: 13/04/2012, 13h10
  2. Réponses: 0
    Dernier message: 06/02/2012, 09h08
  3. Problème avec mon nouveau PC HP version Windows 7
    Par haby86 dans le forum Windows 7
    Réponses: 8
    Dernier message: 29/10/2010, 10h07
  4. Comment ignorer un répertoire dans le versionning ?
    Par elitost dans le forum Eclipse Java
    Réponses: 5
    Dernier message: 05/09/2007, 17h23
  5. mon casse-tête des versions php-Mysql
    Par marcel marie dans le forum Langage
    Réponses: 4
    Dernier message: 03/09/2007, 08h10

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