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 / J2EE utilisation de find()


Sujet :

JPA Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Janvier 2012
    Messages
    56
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : Associations - ONG

    Informations forums :
    Inscription : Janvier 2012
    Messages : 56
    Par défaut JPA / J2EE utilisation de find()
    Bonjour !

    après plusieurs jours de recherches infructueuses sur divers forum et tutoriels, j'en suis réduit à poster ce message sos :

    je n'arrive désespérément pas à utiliser correctement la méthode find() dans mon application.

    j'ai suivi consciencieusement le tutoriel suivant :
    http://wwwdi.supelec.fr/hardebolle/t...JEE_43-jpa.php

    mais bien évidemment, il me manque certainement LE pré-requis nécessaire...

    Quelqu'un peut-il m'aider ?

    Au besoin je peux envoyer le code source de mon application pour tenter de comprendre...


    Merci beaucoup d'avance

    jean-Michel

  2. #2
    Membre éprouvé Avatar de anisj1m
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juillet 2006
    Messages
    1 067
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2006
    Messages : 1 067
    Par défaut
    euhhhh tu n'as mentionné aucune erreur
    Et quelle est votre erreur

  3. #3
    Membre Expert
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2007
    Messages
    2 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 2 938
    Par défaut
    Citation Envoyé par jmdel5327 Voir le message
    Bonjour !

    après plusieurs jours de recherches infructueuses sur divers forum et tutoriels, j'en suis réduit à poster ce message sos :

    je n'arrive désespérément pas à utiliser correctement la méthode find() dans mon application.

    j'ai suivi consciencieusement le tutoriel suivant :
    http://wwwdi.supelec.fr/hardebolle/t...JEE_43-jpa.php

    mais bien évidemment, il me manque certainement LE pré-requis nécessaire...

    Quelqu'un peut-il m'aider ?

    Au besoin je peux envoyer le code source de mon application pour tenter de comprendre...


    Merci beaucoup d'avance

    jean-Michel
    Peux tu nous poster ton code ne marchant pas, et les traces d'erreur d'éxécution?

  4. #4
    Membre confirmé
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Janvier 2012
    Messages
    56
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : Associations - ONG

    Informations forums :
    Inscription : Janvier 2012
    Messages : 56
    Par défaut voilà mon code :
    fichier categorie.java
    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package model;
     
    import java.beans.PropertyChangeListener;
    import java.beans.PropertyChangeSupport;
    import java.io.Serializable;
    //import javax.persistence.Entity;
    import javax.persistence.*;
    //import javax.persistence.Table;
     
    @Entity
    @Table(name = "categorie")
     
     
     
    public class categorie implements Serializable {
        @Id
     
        public int catid;
        public String catnom;
     
     
    public static final String PROP_SAMPLE_PROPERTY = "sampleProperty";
        private String sampleProperty;
        private PropertyChangeSupport propertySupport;
     
        public categorie() {
            propertySupport = new PropertyChangeSupport(this);
        }
     
        public String getSampleProperty() {
            return sampleProperty;
        }
     
        public void setSampleProperty(String value) {
            String oldValue = sampleProperty;
            sampleProperty = value;
            propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, sampleProperty);
        }
     
        public void addPropertyChangeListener(PropertyChangeListener listener) {
            propertySupport.addPropertyChangeListener(listener);
        }
     
        public void removePropertyChangeListener(PropertyChangeListener listener) {
            propertySupport.removePropertyChangeListener(listener);
        } 
     
        public void setId(int tpid) {
           catid = tpid; 
        }
        public int getId(){
            return catid;
        }
     
        public void setCatNom(String tpcatnom){
            catnom = tpcatnom;
        }
        public String getCatNom(){
            return catnom;
        }
     
        public int getCatid() {
            return catid;
        }
     
        public void setCatid(int catid) {
            this.catid = catid;
        }
     
    }
    fichier CatalogManager.java
    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
    package logic;
     
    import java.beans.*;
    import java.io.Serializable;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ApplicationScoped;
    import java.util.ArrayList;
    import javax.annotation.PostConstruct;
    import javax.ejb.EJB;
    import model.categorie;
     
     
     
    @ManagedBean
    @ApplicationScoped
    @EJB    
     
     
     
    /**
     *
     * @author jm
     */
     
     
     
    public class CatalogManager implements Serializable {
     
        private categorieFacade catfacade;
        private ArrayList<categorie> arListCategorie = new ArrayList<categorie>()  ;
        private String transfert;
     
        private int transCategorieId;
        private String transCategorieNom;
     
        public void setTransfert(String transfert){
            this.transfert = transfert;
        }
     
        public String getTransfert(){
            return transfert;
        }   
     
        public void setTransCategorieId(int transCategorieId){
            this.transCategorieId = transCategorieId;
        }
        public int getTransCategorieId(){
            return transCategorieId;
        }
     
        public void setTransCategorieNom(String transCategorieNom){
            this.transCategorieNom = transCategorieNom;
        }
        public String getTransCategorieNom(){
            return transCategorieNom;
        }
     
     
        public void setArListCategorie(ArrayList<categorie> arListCategorie) {
           this.arListCategorie = arListCategorie; 
        }
     
        public ArrayList<categorie> getArListCategorie(){
            return arListCategorie;
        }
     
        public void CreateCategorie(){     
     
            categorie transCategorie = new categorie();
            transCategorie.catid = transCategorieId;
            transCategorie.catnom = transCategorieNom;
     
            arListCategorie.add(transCategorie);
     
        }
     
        public void ModifTransfert(){     
            //this.transfert=transfert;
        }    
     
        public static final String PROP_SAMPLE_PROPERTY = "sampleProperty";
        private String sampleProperty;
        private PropertyChangeSupport propertySupport;
     
        public CatalogManager() {
            propertySupport = new PropertyChangeSupport(this);
        } 
     
        public String getSampleProperty() {
            return sampleProperty;
        }
     
        public void setSampleProperty(String value) {
            String oldValue = sampleProperty;
            sampleProperty = value;
            propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, sampleProperty);
        }
     
        public void addPropertyChangeListener(PropertyChangeListener listener) {
            propertySupport.addPropertyChangeListener(listener);
        }
     
        public void removePropertyChangeListener(PropertyChangeListener listener) {
            propertySupport.removePropertyChangeListener(listener);
        }
     
        @PostConstruct
            public void initcategorie(){
            transfert = "salut !";
     
     
           //categorie nouveau = new categorie();
     
            //nouveau.catid=1;
            //nouveau.catnom = "vapeur";      
            //this.arListCategorie.add(nouveau);
     
     
            //categorie plus = new categorie();
            //plus.catid=2;
            //plus.catnom = "diesel";  
            //this.arListCategorie.add(plus);
            //String prov=null;
            //categorie data = new categorie();
            int ptl=1;
     
     
            categorie data=catfacade.find(ptl);
            this.arListCategorie.add(data);
     
     
        }
     
     
    }
    fichier categorieFacade.java
    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package logic;
     
    import javax.ejb.Stateless;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import model.categorie;
     
    /**
     *
     * @author jm
     */
    @Stateless
    public class categorieFacade extends AbstractFacade<categorie> {
        @PersistenceContext(unitName = "geRezaPU")
        private EntityManager em;
     
        @Override
        protected EntityManager getEntityManager() {
            return em;
        }
     
        public categorieFacade() {
            super(categorie.class);
        }
     
    }
    Voilà, je pense avoir mis le nécessaire...
    C'est cette ligne là
    categorie data=catfacade.find(ptl);
    du fichierCatalogManager.java qui me pose du soucis.

    catfacade.find(ptl)

    Dans cette expression, il manque certainement un argument, puisque ça plante à l'exécution avec comme message :
    Erreur lors de l?injection de ressources dans le bean géré «catalogManager»
    Caused by: java.lang.NullPointerException
    at logic.CatalogManager.initcategorie(CatalogManager.java:128)

    Ou alors, il y a quelque chose que je n'ai pas compris du tout...

    Merci encore de m'aider !!!!

    jean-Michel

    Je m'en souviendrai, quand plus tard, devenu pro moi aussi, je pourrais aider des débutants en Java...

  5. #5
    Membre confirmé
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Janvier 2012
    Messages
    56
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : Associations - ONG

    Informations forums :
    Inscription : Janvier 2012
    Messages : 56
    Par défaut
    Et quand j'essaye ça :
    categorie data=catfacade.find(categorie.class, ptl);

    Netbeans me dit :
    method find in class logic.AbstractFacade<T> cannot be applied to given types;
    required: java.lang.Object
    found: java.lang.Class<model.categorie>,int
    reason: actual and formal argument lists differ in length

    ???

  6. #6
    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
    Il faudrait le code de AbstractFacade
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  7. #7
    Membre confirmé
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Janvier 2012
    Messages
    56
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : Associations - ONG

    Informations forums :
    Inscription : Janvier 2012
    Messages : 56
    Par défaut
    Code de AbstractFacade.java

    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package logic;
     
    import java.util.List;
    import javax.persistence.EntityManager;
     
    /**
     *
     * @author jm
     */
    public abstract class AbstractFacade<T> {
        private Class<T> entityClass;
     
        public AbstractFacade(Class<T> entityClass) {
            this.entityClass = entityClass;
        }
     
        protected abstract EntityManager getEntityManager();
     
        public void create(T entity) {
            getEntityManager().persist(entity);
        }
     
        public void edit(T entity) {
            getEntityManager().merge(entity);
        }
     
        public void remove(T entity) {
            getEntityManager().remove(getEntityManager().merge(entity));
        }
     
        public T find(Object id) {
            return getEntityManager().find(entityClass, id);
        }
     
        public List<T> findAll() {
            javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
            cq.select(cq.from(entityClass));
            return getEntityManager().createQuery(cq).getResultList();
        }
     
        public List<T> findRange(int[] range) {
            javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
            cq.select(cq.from(entityClass));
            javax.persistence.Query q = getEntityManager().createQuery(cq);
            q.setMaxResults(range[1] - range[0]);
            q.setFirstResult(range[0]);
            return q.getResultList();
        }
     
        public int count() {
            javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
            javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
            cq.select(getEntityManager().getCriteriaBuilder().count(rt));
            javax.persistence.Query q = getEntityManager().createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        }
     
    }

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

Discussions similaires

  1. JPA / J2EE utilisation de create()
    Par kayri dans le forum JPA
    Réponses: 2
    Dernier message: 04/02/2013, 10h33
  2. utilisation de find pour trouver date
    Par ouioui_cmoi dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 28/03/2008, 14h23
  3. Utilisation de find
    Par Fry dans le forum Administration système
    Réponses: 2
    Dernier message: 02/11/2006, 15h39
  4. [C++ .NET] Comment utiliser fonction Find ?
    Par thecrax dans le forum Framework .NET
    Réponses: 3
    Dernier message: 17/08/2006, 09h02
  5. [VBA-E] Pb avec l'utilisation de .Find
    Par belfaigore dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 06/07/2006, 07h38

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