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

Java EE Discussion :

EJB3 EntityManager null


Sujet :

Java EE

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    60
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 60
    Par défaut EJB3 EntityManager null
    Bonjour,

    je sèche sur un problème d'entitymanager visiblement null.

    J'utilise :
    - EJB 3
    - JBOSS 5 (avec hibernate comme outil de persistence)
    - une base mysql

    Le projet est vraiment simple (un essai pour m'entraîner) :

    un entity bean book :

    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
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package org.model;
     
    import java.io.Serializable;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
     
    /**
     *
     */
    @Entity
    public class Book implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.TABLE)
        private Long id;    
        private String title;
     
        public Book() {
        }
     
     
     
        public Book(String title){
            this.title = title;
        }
     
        public Long getId() {
            return id;
        }
     
        public void setId(Long id) {
            this.id = id;
        }
     
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.hashCode() : 0);
            return hash;
        }
     
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Book)) {
                return false;
            }
            Book other = (Book) object;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }
     
        @Override
        public String toString() {
            return "org.model.Book[id=" + id + "]";
        }
     
    }
    Une interface remote pour le session bean
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
     
    @Remote
    public interface BookServiceRemote {
        void createOrUpdate(Book book);
        void remove(Book book);
        Book find(Object id);
    }
    Le session bean stateless

    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
     
    package org.model;
     
    import javax.ejb.Stateless;
    import javax.ejb.TransactionAttribute;
    import javax.ejb.TransactionAttributeType;
    import javax.ejb.TransactionManagement;
    import javax.ejb.TransactionManagementType;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
     
    /**
     *
     * @author Onizuka
     */
    @TransactionManagement(TransactionManagementType.CONTAINER)
    @TransactionAttribute (TransactionAttributeType.REQUIRED)
    @Stateless
    public class BookService implements BookServiceRemote {
        @PersistenceContext
        private EntityManager em;
     
        @TransactionAttribute (TransactionAttributeType.REQUIRED)
        public void createOrUpdate(Book book) {
            System.out.println("create or update");
     
            em.merge(book);
        }
     
        @TransactionAttribute (TransactionAttributeType.REQUIRED)
        public void remove(Book book) {
            em.remove(book);
     
        }
     
        @TransactionAttribute (TransactionAttributeType.REQUIRED)
        public Book find(Object id) {
        return null;
        }
     
        // Add business logic below. (Right-click in editor and choose
        // "Insert Code > Add Business Method")
     
    }
    un test Junit pour tester mon ejb

    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
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package org.model;
     
    import java.util.Hashtable;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.rmi.PortableRemoteObject;
    import org.junit.After;
    import org.junit.AfterClass;
    import org.junit.Before;
    import org.junit.BeforeClass;
    import org.junit.Test;
    import static org.junit.Assert.*;
     
    /**
     *
     * @author Onizuka
     */
    public class BookServiceTest {
     
        public BookServiceTest() {
        }
     
        @BeforeClass
        public static void setUpClass() throws Exception {
        }
     
        @AfterClass
        public static void tearDownClass() throws Exception {
        }
     
        @Before
        public void setUp() {
        }
     
        @After
        public void tearDown() {
        }
     
        /**
         * Test of createOrUpdate method, of class BookService.
         */
        @Test
        public void testCreateOrUpdate() {
            try {
                Hashtable ht = new Hashtable();
                ht.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                ht.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
                ht.put("java.naming.provider.url", "jnp://localhost:1099");
                InitialContext ctx;
                ctx = new InitialContext(ht);
                Object ref = ctx.lookup("java:/BookService/remote");
                BookServiceRemote bServ = (BookServiceRemote) PortableRemoteObject.narrow(ref,BookServiceRemote.class);
                Book b = new Book("livre1");
                bServ.createOrUpdate(b);
                /*
                System.out.println("createOrUpdate");
                Book book = null;
                BookService instance = new BookService();
                Book expResult = null;
                Book result = instance.createOrUpdate(book);
                assertEquals(expResult, result);
                // TODO review the generated test code and remove the default call to fail.
                fail("The test case is a prototype.");*/
            } catch (NamingException ex) {
                Logger.getLogger(BookServiceTest.class.getName()).log(Level.SEVERE, null, ex);
            }
     
        }
     
        /**
         * Test of remove method, of class BookService.
         */
        /*
        @Test
        public void testRemove() {
            System.out.println("remove");
            Book book = null;
            BookService instance = new BookService();
            instance.remove(book);
            // TODO review the generated test code and remove the default call to fail.
            fail("The test case is a prototype.");
        }*/
     
        /**
         * Test of find method, of class BookService.
         */
        /*
        @Test
        public void testFind() {
            System.out.println("find");
            Object id = null;
            BookService instance = new BookService();
            Book expResult = null;
            Book result = instance.find(id);
            assertEquals(expResult, result);
            // TODO review the generated test code and remove the default call to fail.
            fail("The test case is a prototype.");
        }*/
     
    }
    mon persistence.xml :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
     
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
      <persistence-unit name="SamplePU" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/samplejndi</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
          <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        </properties>
      </persistence-unit>
    </persistence>
    A l'exécution du test j'obtiens l'erreur

    qui me renvoie sur la ligne :

    pouvez-vous m'aider ?

    merci bcp

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    60
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 60
    Par défaut
    Sitôt rédigé mon message je me rends compte que j'ai oublié de précisé le nom de mon unité de persistence lors de l'injection de mon enititymanage

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     @PersistenceContext (unitName = "SamplePU")
        private EntityManager em;
    Comme par magie, cela marche maintenant !

    désolé pour le post !

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

Discussions similaires

  1. Sring-MVC + JPA = EntityManager null
    Par Dragna_x dans le forum Spring Web
    Réponses: 2
    Dernier message: 25/10/2012, 12h49
  2. EntityManager null ?
    Par Hole_geek dans le forum JPA
    Réponses: 3
    Dernier message: 03/04/2012, 13h35
  3. EntityManager, null pointer exception.
    Par FinalSpirit dans le forum JPA
    Réponses: 7
    Dernier message: 07/07/2009, 11h29
  4. [débutant] EntityManager : null pointer exception
    Par mateu34 dans le forum Glassfish et Payara
    Réponses: 3
    Dernier message: 16/02/2009, 22h58
  5. [EJB3 Entity] EntityManager null
    Par pmartin8 dans le forum Java EE
    Réponses: 2
    Dernier message: 29/04/2007, 17h07

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