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

Persistance des données Java Discussion :

jpa Controller classes from Entity Classes- Netbeans 7.2


Sujet :

Persistance des données Java

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Septembre 2011
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Septembre 2011
    Messages : 33
    Points : 35
    Points
    35
    Par défaut jpa Controller classes from Entity Classes- Netbeans 7.2
    Salut
    j'ai utilisé l'outil de génération ' ENtity classes from database' et jpa Controller classes from Entity Classes de Netbeans 7.2 et j'ai écrit le main suivant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class NewMain {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            UserJpaController jpa= new UserJpaController(null, null);
            jpa.findUserEntities();
        }
    }
    et il m'a affiché les exception suivantes :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    Exception in thread "main" java.lang.NullPointerException
    	at controller.UserJpaController.getEntityManager(UserJpaController.java:36)
    	at controller.UserJpaController.findUserEntities(UserJpaController.java:157)
    	at controller.UserJpaController.findUserEntities(UserJpaController.java:149)
    	at NewMain.main(NewMain.java:21)
    klk pourrait m'aider ici? surtout qu'est ce que je dois fournir en paramètres au constructeur de UserJpaController.
    Merci

  2. #2
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Bonjour,

    Je ne n'arrive pas à trouver le code source correspondant à UserJpaController que tu utilises mais pour cette version, c'est un EntityManagerFactory que tu dois mettre en paramètre. Et dans cette version, il instancie lui même l'entityManagerFactory.


    A+.

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Septembre 2011
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Septembre 2011
    Messages : 33
    Points : 35
    Points
    35
    Par défaut
    Citation Envoyé par andry.aime Voir le message
    Bonjour,

    Je ne n'arrive pas à trouver le code source correspondant à UserJpaController que tu utilises mais pour cette version, c'est un EntityManagerFactory que tu dois mettre en paramètre. Et dans cette version, il instancie lui même l'entityManagerFactory.


    A+.
    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package controller;
     
    import controller.exceptions.NonexistentEntityException;
    import controller.exceptions.PreexistingEntityException;
    import controller.exceptions.RollbackFailureException;
    import java.io.Serializable;
    import java.util.List;
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Query;
    import javax.persistence.EntityNotFoundException;
    import javax.persistence.criteria.CriteriaQuery;
    import javax.persistence.criteria.Root;
    import javax.transaction.UserTransaction;
    import model.Profil;
    import model.User;
     
    /**
     *
     * @author Bitaka-DEV
     */
    public class UserJpaController implements Serializable {
     
        public UserJpaController(UserTransaction utx, EntityManagerFactory emf) {
            this.utx = utx;
            this.emf = emf;
        }
        private UserTransaction utx = null;
        private EntityManagerFactory emf = null;
     
        public EntityManager getEntityManager() {
            return emf.createEntityManager();
        }
     
        public void create(User user) throws PreexistingEntityException, RollbackFailureException, Exception {
            EntityManager em = null;
            try {
                utx.begin();
                em = getEntityManager();
                Profil idProfil = user.getIdProfil();
                if (idProfil != null) {
                    idProfil = em.getReference(idProfil.getClass(), idProfil.getId());
                    user.setIdProfil(idProfil);
                }
                em.persist(user);
                if (idProfil != null) {
                    idProfil.getUserList().add(user);
                    idProfil = em.merge(idProfil);
                }
                utx.commit();
            } catch (Exception ex) {
                try {
                    utx.rollback();
                } catch (Exception re) {
                    throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
                }
                if (findUser(user.getLogin()) != null) {
                    throw new PreexistingEntityException("User " + user + " already exists.", ex);
                }
                throw ex;
            } finally {
                if (em != null) {
                    em.close();
                }
            }
        }
     
        public void edit(User user) throws NonexistentEntityException, RollbackFailureException, Exception {
            EntityManager em = null;
            try {
                utx.begin();
                em = getEntityManager();
                User persistentUser = em.find(User.class, user.getLogin());
                Profil idProfilOld = persistentUser.getIdProfil();
                Profil idProfilNew = user.getIdProfil();
                if (idProfilNew != null) {
                    idProfilNew = em.getReference(idProfilNew.getClass(), idProfilNew.getId());
                    user.setIdProfil(idProfilNew);
                }
                user = em.merge(user);
                if (idProfilOld != null && !idProfilOld.equals(idProfilNew)) {
                    idProfilOld.getUserList().remove(user);
                    idProfilOld = em.merge(idProfilOld);
                }
                if (idProfilNew != null && !idProfilNew.equals(idProfilOld)) {
                    idProfilNew.getUserList().add(user);
                    idProfilNew = em.merge(idProfilNew);
                }
                utx.commit();
            } catch (Exception ex) {
                try {
                    utx.rollback();
                } catch (Exception re) {
                    throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
                }
                String msg = ex.getLocalizedMessage();
                if (msg == null || msg.length() == 0) {
                    String id = user.getLogin();
                    if (findUser(id) == null) {
                        throw new NonexistentEntityException("The user with id " + id + " no longer exists.");
                    }
                }
                throw ex;
            } finally {
                if (em != null) {
                    em.close();
                }
            }
        }
     
        public void destroy(String id) throws NonexistentEntityException, RollbackFailureException, Exception {
            EntityManager em = null;
            try {
                utx.begin();
                em = getEntityManager();
                User user;
                try {
                    user = em.getReference(User.class, id);
                    user.getLogin();
                } catch (EntityNotFoundException enfe) {
                    throw new NonexistentEntityException("The user with id " + id + " no longer exists.", enfe);
                }
                Profil idProfil = user.getIdProfil();
                if (idProfil != null) {
                    idProfil.getUserList().remove(user);
                    idProfil = em.merge(idProfil);
                }
                em.remove(user);
                utx.commit();
            } catch (Exception ex) {
                try {
                    utx.rollback();
                } catch (Exception re) {
                    throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
                }
                throw ex;
            } finally {
                if (em != null) {
                    em.close();
                }
            }
        }
     
        public List<User> findUserEntities() {
            return findUserEntities(true, -1, -1);
        }
     
        public List<User> findUserEntities(int maxResults, int firstResult) {
            return findUserEntities(false, maxResults, firstResult);
        }
     
        private List<User> findUserEntities(boolean all, int maxResults, int firstResult) {
            EntityManager em = getEntityManager();
            try {
                CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
                cq.select(cq.from(User.class));
                Query q = em.createQuery(cq);
                if (!all) {
                    q.setMaxResults(maxResults);
                    q.setFirstResult(firstResult);
                }
                return q.getResultList();
            } finally {
                em.close();
            }
        }
     
        public User findUser(String id) {
            EntityManager em = getEntityManager();
            try {
                return em.find(User.class, id);
            } finally {
                em.close();
            }
        }
     
        public int getUserCount() {
            EntityManager em = getEntityManager();
            try {
                CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
                Root<User> rt = cq.from(User.class);
                cq.select(em.getCriteriaBuilder().count(rt));
                Query q = em.createQuery(cq);
                return ((Long) q.getSingleResult()).intValue();
            } finally {
                em.close();
            }
        }
     
    }

  4. #4
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Tu dois instancier un EntityManagerFactory avec Persistence.createEntityManagerFactory et récupérer UserTransaction depuis ton source de donnée pour les mettre ensuite en paramètre. Fait un recherche sur google.

    A+.

Discussions similaires

  1. JDP Controller classes from Entity Classes- Netbeans 7.2
    Par Linos1003 dans le forum NetBeans
    Réponses: 0
    Dernier message: 05/06/2012, 12h03
  2. Réponses: 0
    Dernier message: 18/07/2008, 12h38
  3. Code genere : Entity class from database
    Par Sourrisseau dans le forum NetBeans
    Réponses: 4
    Dernier message: 14/04/2007, 13h50
  4. JSF Pages from Entity Class
    Par Smix007 dans le forum JSF
    Réponses: 8
    Dernier message: 21/01/2007, 21h12
  5. NewJSF pages from Entity Class
    Par Smix007 dans le forum NetBeans
    Réponses: 17
    Dernier message: 17/01/2007, 21h52

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