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 :

JPA Hibernate Index non crée dans Mysql


Sujet :

JPA Java

  1. #1
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 19
    Points : 0
    Points
    0
    Par défaut JPA Hibernate Index non crée dans Mysql
    J'utilile hibernate pour ma persistence JPA.
    Mysql5.1 pour la base de donnée.

    J'ai un object User avec un identifier et un firstName.
    Dans cet exemple, ces 2 éléments ne sont pas uniques séparément.

    Je voudrais avoir un index sur ces 2 colones.

    J'ai vu une annotion dans hibernate (non standart) qui perment de créer cet index...

    @org.hibernate.annotations.Table(appliesTo="USER", indexes = { @Index(name="element_idx", columnNames = { "firstName", "identifier" } ) } )

    Mais aucun index n'est crée dans Mysql...

    Une Idée ???
    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
     
    package com.alu.ejb.model;
     
    import java.io.Serializable;
     
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;
     
    import org.hibernate.annotations.Index;
     
     
    @Entity
    @Table(name = "USER")
    @org.hibernate.annotations.Table(appliesTo="USER", indexes = { @Index(name="element_idx", columnNames = { "firstName", "identifier" } ) } )
    public class User
        implements Serializable
    {
        private Integer _id;
        private String _email;
        private String _firstName;
        private String _identifier;
     
        public User()
        {
        }
     
        public String getEmail()
        {
            return _email;
        }
     
        @Column(unique=false, nullable = false)
        public String getFirstName()
        {
            return _firstName;
        }
     
        @Id
        @Column(nullable = false)
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        public Integer getId()
        {
            return _id;
        }
     
        @Column(unique = false, nullable = false)
        public String getIdentifier()
        {
            return _identifier;
        }
     
        public void setEmail(String email) {
            this._email = email;
        }
     
        public void setFirstName(String firstName) {
            this._firstName = firstName;
        }
     
        public void setIdentifier(String identifier) {
            this._identifier = identifier;
        }
     
        public void setId(Integer id) {
            this._id = id;
        }
    }

  2. #2
    Futur Membre du Club
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 2
    Points : 5
    Points
    5
    Par défaut
    je pense que l'usage de l'annotation @Id n'est pas compatible avec le fait d'ajouter une clé primaire via l'annotation spécifique d'Hibernate.

    LaTortue

  3. #3
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 19
    Points : 0
    Points
    0
    Par défaut @ID est pour la définition de la clef primaire !!!
    @ID est une annotation pour la clef primaire.
    L'annotation @Index (mise dans l'annotation @table ou non) est pour la creation d'indexes !!
    Donc je ne pense pas que @ID est @Index soient liés...

    Une autre reponse ??

  4. #4
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 16
    Points : 25
    Points
    25
    Par défaut
    Je remonte ce vieux thread qui n'a pas eu de réponse car je rencontre le même problème avec Hibernate 3.4 et Apache Derby 10.6.1.

    Hibernate utilisé comme provider JPA ignorerait-il les annotations permettant d'ajouter des index ?

  5. #5
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 16
    Points : 25
    Points
    25
    Par défaut
    Je n'ai trouvé de réponse satisfaisante nulle part.

    Le JIRA d'Hibernate est remplis de tickets ouverts concernant la création des indexes, certains depuis 2005. Il apparait que cette partie du code n'attire que peu de monde et aurait besoin d'une réecriture accompagnée de tests unitaires. Amateurs ne vous abstenez pas

    La "solution" pour l'instant reste de prendre la main sur la gestion du schéma et de s'occuper de ses indexes soit même, le bon vieux DIY quoi...

  6. #6
    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
    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
    package website.domain.model;
     
    import java.io.Serializable;
    import java.util.List;
    import javax.persistence.Basic;
    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.NamedQueries;
    import javax.persistence.NamedQuery;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    import org.hibernate.annotations.Index;
     
    /**
     *
     * @author funkalee
     */
    @Entity
    @Table(name = "City")
    @NamedQueries({
      @NamedQuery(name = "City.findAll", query = "SELECT c FROM City c"),
      @NamedQuery(name = "City.findByCityID", query = "SELECT c FROM City c WHERE c.cityID = :cityID"),
      @NamedQuery(name = "City.findByCityName", query = "SELECT c FROM City c WHERE c.cityName = :cityName"),
      @NamedQuery(name = "City.findByCityINSEECode", query = "SELECT c FROM City c WHERE c.cityINSEECode = :cityINSEECode")})
    public class City implements Serializable {
     
      private static final long serialVersionUID = 1L;
      public static final String PROP_CITYID = "cityID";
      public static final String PROP_CITYNAME = "cityName";
      public static final String PROP_CITYINSEECODE = "cityINSEECode";
      public static final String PROP_CITY_ZIPCODEID = "zipCode";
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "cityID", nullable = false)
      private Long cityID;
      @Basic(optional = false)
      @Column(name = "cityName", nullable = false, length = 32)
      private String cityName;
      @Column(name = "cityINSEECode", length = 5)
      @Index(name="idx_City_cityINSEECode")
      private String cityINSEECode;
      @JoinColumn(name = "zipCode", referencedColumnName = "zipCodeID", nullable = false)
      @ManyToOne(optional = false)
      private ZipCode zipCode;
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "city")
      private List<Address> addressList;
     
      public City() {
      }
     
      public City(Long cityID) {
        this.cityID = cityID;
      }
     
      public City(Long cityID, String cityName) {
        this.cityID = cityID;
        this.cityName = cityName;
      }
     
      public Long getCityID() {
        return cityID;
      }
     
      public void setCityID(Long cityID) {
        this.cityID = cityID;
      }
     
      public String getCityName() {
        return cityName;
      }
     
      public void setCityName(String cityName) {
        this.cityName = cityName;
      }
     
      public String getCityINSEECode() {
        return cityINSEECode;
      }
     
      public void setCityINSEECode(String cityINSEECode) {
        this.cityINSEECode = cityINSEECode;
      }
     
      public ZipCode getZipCode() {
        return zipCode;
      }
     
      public void setZipCode(ZipCode zipCode) {
        this.zipCode = zipCode;
      }
     
      public List<Address> getAddressList() {
        return addressList;
      }
     
      public void setAddressList(List<Address> addressList) {
        this.addressList = addressList;
      }
     
      @Override
      public int hashCode() {
        int hash = 0;
        hash += (cityID != null ? cityID.hashCode() : 0);
        return hash;
      }
     
      @Override
      public boolean equals(Object object) {
        // this method won't work in the case the id fields are not set
        if (!(object instanceof City)) {
          return false;
        }
        City other = (City) object;
        if ((this.cityID == null && other.cityID != null) || (this.cityID != null && !this.cityID.equals(other.cityID))) {
          return false;
        }
        return true;
      }
     
      @Override
      public String toString() {
        StringBuilder entityToString = new StringBuilder(City.class.getSimpleName());
        entityToString.append(" :[").append(PROP_CITYID).append(" = ");
        entityToString.append(this.getCityID()).append("][").append(PROP_CITYNAME);
        entityToString.append(" = ").append(this.getCityName()).append("][");
        entityToString.append(PROP_CITYINSEECODE).append(" = ");
        entityToString.append(this.getCityINSEECode()).append("][");
        entityToString.append(PROP_CITY_ZIPCODEID).append(" = ");
        entityToString.append(this.getZipCode() == null ? "null"
                : this.getZipCode().getZipCodeID());
        entityToString.append("][").append("memory reference = ");
        entityToString.append(City.class.getName()).append("@");
        entityToString.append(Integer.toHexString(this.hashCode())).append("]");
        return entityToString.toString();
      }
    }

    crée
    Code sql : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    CREATE TABLE `City` (
      `cityID` bigint(20) NOT NULL AUTO_INCREMENT,
      `cityINSEECode` varchar(5) DEFAULT NULL,
      `cityName` varchar(32) NOT NULL,
      `zipCode` bigint(20) NOT NULL,
      PRIMARY KEY (`cityID`),
      KEY `FK200D8B7F099B20` (`zipCode`),
      KEY `idx_City_cityINSEECode` (`cityINSEECode`),
      CONSTRAINT `FK200D8B7F099B20` FOREIGN KEY (`zipCode`) REFERENCES `ZipCode` (`zipCodeID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8

    donc @Index crée bien l'index sur le champ cityINSEECode

    ps : testé sur mysql 5.1 avec <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/> dans mon fichier de persistance, cela créé automatiquement mes index sur clé étrangère sans le spécifier, voir

Discussions similaires

  1. Index non utilisé dans une jointure
    Par lasyan3 dans le forum SQL
    Réponses: 15
    Dernier message: 12/04/2011, 09h06
  2. Index non utilisé dans une requête
    Par tibal dans le forum Administration
    Réponses: 9
    Dernier message: 10/05/2010, 15h29
  3. Que contiennent les index Non Cluster dans SQL 2005
    Par ygrim dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 04/03/2008, 16h01
  4. [debutant] Hibernate : table non crée
    Par lilou77 dans le forum Hibernate
    Réponses: 10
    Dernier message: 06/01/2006, 16h32
  5. [Vb.net] Indexé un objet crée dans une boucle
    Par picpic dans le forum Windows Forms
    Réponses: 10
    Dernier message: 17/12/2003, 14h37

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