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 :

hibernate recherche multicritere


Sujet :

Hibernate Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Juillet 2007
    Messages
    30
    Détails du profil
    Informations forums :
    Inscription : Juillet 2007
    Messages : 30
    Par défaut hibernate recherche multicritere
    bonsoire,je veux realiser une recherche multicritere ave hibernate voila mon code dao
    ===========
    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
    package Vehicule;
    // default package
    
    import Affectation.Affectation;
    import Catigorie.Catigorie;
    import Exercice.BaseHibernateDAO;
    import Exercice.Exercice;
    import HibernateSessionFactory.HibernateSessionFactory;
    import Reforme.Reforme;
    
    import java.util.Date;
    import java.util.List;
    import java.util.Set;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.hibernate.Hibernate;
    import org.hibernate.HibernateException;
    import org.hibernate.LockMode;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    import org.hibernate.criterion.Example;
    import org.hibernate.exception.ConstraintViolationException;
    
    
    /**
    * Data access object (DAO) for domain model class Vehicule.
    * @see .Vehicule
    * @author MyEclipse - Hibernate Tools
    */
    public class VehiculeDAO extends BaseHibernateDAO {
    
    private static final Log log = LogFactory.getLog(VehiculeDAO.class);
    
    //property constants
    public static final String NMOTEUR = "nmoteur";
    public static final String NCHASSIS = "nchassis";
    public static final String OBSERVATIONS = "observations";
    public static final String MARQUE = "marque";
    public static final String PUISSANCE = "puissance";
    public static final String TYPE_COMBUSTIBLE = "typeCombustible";
    
    
    public void save(Vehicule transientInstance) {
    log.debug("saving Vehicule instance");
    try {
    getSession().save(transientInstance);
    log.debug("save successful");
    } catch (RuntimeException re) {
    log.error("save failed", re);
    throw re;
    }
    }
    
    public void delete(Vehicule persistentInstance) {
    log.debug("deleting Vehicule instance");
    try {
    Transaction tx = getSession().beginTransaction();
    getSession().delete(persistentInstance);
    tx.commit();
    log.debug("delete successful");
    } catch (RuntimeException re) {
    log.error("delete failed", re);
    throw re;
    }
    }
    
    public List RechercheAvancer(Integer nvehicule,Integer nreforme, Integer ncategorie, Integer nmoteur, 
    Integer nchassis, Date dateMiseEnService, String marque, Integer puissance,
    String typeCombustible) { 
    try {
    List results;
    results=this.findByMarque(marque);
    results=this.findByTypeCombustible(typeCombustible);
    results=this.findByNchassis(nchassis);
    results=this.findByNmoteur(nmoteur);
    results=this.findByPuissance(puissance);
    
    
    /*List results = getSession()
    .createQuery("from Vehicule where Nvehicule ="+nvehicule+"and Nreforme ="+nreforme+"and Ncategorie ="+ncategorie
    +"and Nmoteur ="+nmoteur+"and Nchassis ="+nchassis+"and Date_mise_en_service ="+dateMiseEnService
    +"and Marque ="+marque+"and Puissance ="+puissance+"and Type_combustible ="+typeCombustible).list();*/
    
    
    return results;
    } catch (RuntimeException re) {
    throw re;
    }
    } public List getListeVehicule() { 
    try {
    List results = getSession()
    .createQuery("from Vehicule").list();
    return results;
    } catch (RuntimeException re) {
    throw re;
    }
    }
    public List getListeReforme() { 
    try {
    List results = getSession()
    .createQuery("from Reforme").list();
    return results;
    } catch (RuntimeException re) {
    throw re;
    }
    } 
    public List getListeCategorie() { 
    try {
    List results = getSession()
    .createQuery("from Catigorie").list();
    return results;
    } catch (RuntimeException re) {
    throw re;
    }
    }
    
    public Vehicule findById( java.lang.Integer id) {
    log.debug("getting Vehicule instance with id: " + id);
    try {
    Vehicule instance = (Vehicule) getSession()
    .get("Vehicule.Vehicule", id);
    return instance;
    } catch (RuntimeException re) {
    log.error("get failed", re);
    throw re;
    }
    }
    
    public String updateVehicule(Integer nvehicule,Integer nreforme, Integer ncategorie, Integer nmoteur, 
    Integer nchassis, Date dateMiseEnService, String observations, String marque, Integer puissance,
    String typeCombustible) {
    
    try {
    
    Vehicule vehicule =this.findById(nvehicule);;
    Reforme reforme=new Reforme();
    Catigorie catigorie=new Catigorie();
    reforme.setNreforme(nreforme);
    catigorie.setNcategorie(ncategorie);
    Session session = HibernateSessionFactory.getSession();
    vehicule.setNvehicule(nvehicule);
    vehicule.setReforme(reforme);
    vehicule.setCatigorie(catigorie);
    vehicule.setNmoteur(nmoteur);
    vehicule.setNchassis(nchassis);
    vehicule.setDateMiseEnService(dateMiseEnService);
    vehicule.setObservations(observations);
    vehicule.setMarque(marque);
    vehicule.setPuissance(puissance);
    vehicule.setTypeCombustible(typeCombustible);
    Transaction tx = session.beginTransaction();
    getSession().update(vehicule);
    tx.commit();
    
    return null;
    } catch (ConstraintViolationException e2) {
    return e2.getSQLException().toString();
    } catch (HibernateException e) {
    return e.getMessage();
    } finally {
    try {
    HibernateSessionFactory.closeSession();
    } catch (HibernateException e1) {
    e1.printStackTrace();
    }
    }
    
    }
    
    public String addVehicule(Integer nvehicule, Integer nreforme, Integer ncatigorie, Integer nmoteur, 
    Integer nchassis, Date dateMiseEnService, String observations, String marque, Integer puissance,
    String typeCombustible) {
    
    try {
    
    Vehicule vehicule = new Vehicule();
    Reforme reforme=new Reforme();
    Catigorie catigorie=new Catigorie();
    reforme.setNreforme(nreforme);
    catigorie.setNcategorie(ncatigorie);
    Session session = HibernateSessionFactory.getSession();
    vehicule.setNvehicule(nvehicule);
    vehicule.setReforme(reforme);
    vehicule.setCatigorie(catigorie);
    vehicule.setNmoteur(nmoteur);
    vehicule.setNchassis(nchassis);
    vehicule.setDateMiseEnService(dateMiseEnService);
    vehicule.setObservations(observations);
    vehicule.setMarque(marque);
    vehicule.setPuissance(puissance);
    vehicule.setTypeCombustible(typeCombustible);
    Transaction tx = session.beginTransaction();
    getSession().save(vehicule);
    tx.commit();
    return null;
    } catch (ConstraintViolationException e2) {
    return e2.getSQLException().toString();
    } catch (HibernateException e) {
    return e.getMessage();
    } finally {
    try {
    HibernateSessionFactory.closeSession();
    } catch (HibernateException e1) {
    e1.printStackTrace();
    }
    }
    
    }
    
    public List findByExample(Vehicule instance) {
    log.debug("finding Vehicule instance by example");
    try {
    List results = getSession()
    .createCriteria("Vehicule")
    .add(Example.create(instance))
    .list();
    log.debug("find by example successful, result size: " + results.size());
    return results;
    } catch (RuntimeException re) {
    log.error("find by example failed", re);
    throw re;
    }
    } 
    
    public List findByProperty(String propertyName, Object value) {
    log.debug("finding Vehicule instance with property: " + propertyName
    + ", value: " + value);
    try {
    String queryString = "from Vehicule as model where model." 
    + propertyName + "= ?";
    Query queryObject = getSession().createQuery(queryString);
    queryObject.setParameter(0, value);
    return queryObject.list();
    } catch (RuntimeException re) {
    log.error("find by property name failed", re);
    throw re;
    }
    }
    
    public List findByNmoteur(Object nmoteur) {
    return findByProperty(NMOTEUR, nmoteur);
    }
    
    public List findByNchassis(Object nchassis) {
    return findByProperty(NCHASSIS, nchassis);
    }
    
    public List findByObservations(Object observations) {
    return findByProperty(OBSERVATIONS, observations);
    }
    
    public List findByMarque(Object marque) {
    return findByProperty(MARQUE, marque);
    }
    
    public List findByPuissance(Object puissance) {
    return findByProperty(PUISSANCE, puissance);
    }
    
    public List findByTypeCombustible(Object typeCombustible) {
    return findByProperty(TYPE_COMBUSTIBLE, typeCombustible);
    }
    
    public Vehicule merge(Vehicule detachedInstance) {
    log.debug("merging Vehicule instance");
    try {
    Vehicule result = (Vehicule) getSession()
    .merge(detachedInstance);
    log.debug("merge successful");
    return result;
    } catch (RuntimeException re) {
    log.error("merge failed", re);
    throw re;
    }
    }
    
    public void attachDirty(Vehicule instance) {
    log.debug("attaching dirty Vehicule instance");
    try {
    getSession().saveOrUpdate(instance);
    log.debug("attach successful");
    } catch (RuntimeException re) {
    log.error("attach failed", re);
    throw re;
    }
    }
    
    public void attachClean(Vehicule instance) {
    log.debug("attaching clean Vehicule instance");
    try {
    getSession().lock(instance, LockMode.NONE);
    log.debug("attach successful");
    } catch (RuntimeException re) {
    log.error("attach failed", re);
    throw re;
    }
    }
    }
    mon methode est en rouge ca fonctionne si l'utilisateur saisie tous les paramettre, je veux une methode generale qui fonctione meme si en a saisie seulement 1 ou 2 ou 3 parametere
    merci

  2. #2
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 313
    Billets dans le blog
    1
    Par défaut
    Tu peux faire quelque chose comme ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    public List requeteVariable(String mnemo, String[] types)
    {
       Criteria query = getSession().createCriteria(Variable.class);
     
       if ( mnemo != null ) query.add(Expression.ilike("varMnemo1", mnemo));
       if ( types != null ) query.add(Expression.in("typNom", types));
     
       query.addOrder( Order.asc("varMnemo1") );
       return query.list();
    }
    a+
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  3. #3
    Membre averti
    Inscrit en
    Juillet 2007
    Messages
    30
    Détails du profil
    Informations forums :
    Inscription : Juillet 2007
    Messages : 30
    Par défaut
    merci OButterlin ou bien sans changer ma methode il faut juste ajoutter les teste sur les parametres qui son null est ca marche 100 pour 100

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

Discussions similaires

  1. Réponses: 13
    Dernier message: 22/06/2010, 16h08
  2. Réponses: 1
    Dernier message: 22/02/2006, 19h01
  3. Formulaire de recherche multicriteres par cafeine
    Par jo-popo dans le forum Access
    Réponses: 8
    Dernier message: 12/01/2006, 00h53
  4. Réponses: 6
    Dernier message: 05/01/2006, 14h09
  5. Recherche multicritere
    Par beLz dans le forum Requêtes
    Réponses: 3
    Dernier message: 02/09/2005, 18h24

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