JPA EntityManager ist null

Diskutiere EntityManager ist null im Data Tier Forum; Hallo ich versuche mit einer einfachen Testanwendung ein Objekt in der Datenbank zu speichern. Ich bekomme für den EntityManager aber eine...

  1. Manuba
    Manuba Neues Mitglied
    Hallo

    ich versuche mit einer einfachen Testanwendung ein Objekt in der Datenbank zu speichern. Ich bekomme für den EntityManager aber eine Nullpointer Exception und kann den Fehler nicht finden.
    Google hat mir bei dem Fehler gesagt, dass es meist an einer fehlenden Injection einer Klasse liegt. hier habe ich aber in der beans.xml discovery mode auf "all", daher sollten die Injections ja funktionieren.
    Habe ich vielleicht in der pom.xml und persistence.xml Fehler, da schon beim deployen Fehler auftreten.

    Entity
    Code (Java):
    @Named
    @Entity
    @Table(name = "Test")
    public class Test implements Serializable {


        private static final long serialVersionUID = 1L;
        @Id
        @Basic(optional = false)
        @NotNull
        @Column(name = "idTest")
        private Integer idTest;
        @Size(max = 45)
        @Column(name = "Name")
        private String name;

        public Test() {
        }

        public Test(Integer idTest) {
            this.idTest = idTest;
        }

        public Integer getIdTest() {
            return idTest;
        }

        public void setIdTest(Integer idTest) {
            this.idTest = idTest;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public int hashCode() {
            int hash = 0;
            hash += (idTest != null ? idTest.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 Test)) {
                return false;
            }
            Test other = (Test) object;
            if ((this.idTest == null && other.idTest != null) || (this.idTest != null && !this.idTest.equals(other.idTest))) {
                return false;
            }
            return true;
        }

        @Override
        public String toString() {
            return "com.mycompany.Test[ idTest=" + idTest + " ]";
        }
     
    }
     
    DAOImpl
    Code (Java):
    @Named
    public class TestDAOImpl implements TestDAO {
     
        @PersistenceContext
        private EntityManager em;
     
     

        @Override
        public Test create(Test test) {
            em.persist(test);
            return test;
        }

        @Override
        public Test update(Test test) {
            return
                    em.merge(test);
        }

        @Override
        public void remove(int id) {
            Test testToBeDeleted = getTest(id);
            em.remove(testToBeDeleted);
        }

        @Override
        public Test getTest(int id) {
            return
                em.find(Test.class, id);
        }

        @Override
        public List<Test> getAllTests() {
            return
                    em.createQuery("SELECT t FROM Test t", Test.class).
                        getResultList();
        }
     
    }
     
    TestBean
    Code (Java):
    @Named("TestBeans")
    @RequestScoped
    public class TestBean {
     
        @Inject
         private Test test;

        public Test getTest() {
            return test;
        }

        public void setTest(Test test) {
            this.test = test;
        }

     
     
        public void createTest() {
                    TestDAOImpl testDAO = new TestDAOImpl();
                    testDAO.create(this.test);
        }
     
    }
    index.xhtml
    Code (Java):
    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://xmlns.jcp.org/jsf/html">
        <h:head>
            <title>DB Connection Test</title>
        </h:head>
        <h:body>
            <h:form>
             
                <h:outputLabel for="tname">Test Name:</h:outputLabel>
                <h:inputText id="tname" value="#{TestBeans.test.name}" />
                <h:commandButton value="Test speichern" action="#{TestBeans.createTest()}" />
             
            </h:form>
        </h:body>
    </html>
    persistence.xml
    Code (Text):
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
      <persistence-unit name="my_pu">
          <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>jdbc/myDataSource</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.show_sql" value="true" />
        </properties>
      </persistence-unit>
    </persistence>
    beans.xml
    Code (Text):
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                               http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
           version="1.1" bean-discovery-mode="all">
    </beans>
     
    pom.xml
    Code (Java):
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    Deploy
    Code (Text):
    Warnung:   Exception while dispatching an event
    java.lang.NullPointerException
        at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:76)
        at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:118)
        at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1602)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:210)
        at org.hibernate.jpa.internal.EntityManagerImpl.<init>(EntityManagerImpl.java:91)
        at org.hibernate.jpa.internal.EntityManagerFactoryImpl.internalCreateEntityManager(EntityManagerFactoryImpl.java:345)
        at org.hibernate.jpa.internal.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:313)
        at org.glassfish.persistence.jpa.JPADeployer$2.visitPUD(JPADeployer.java:451)
        at org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:510)
        at org.glassfish.persistence.jpa.JPADeployer.iterateInitializedPUsAtApplicationPrepare(JPADeployer.java:492)
        at org.glassfish.persistence.jpa.JPADeployer.event(JPADeployer.java:398)
        at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:131)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:487)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
        at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:535)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:360)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:534)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:565)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:557)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:360)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:109)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
        at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
        at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
        at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189)
        at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
        at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
        at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
        at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
        at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
        at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
        at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
        at java.lang.Thread.run(Thread.java:745)
    Beim Ausführen
    Code (Text):
    Warnung:   Exception while dispatching an event
    java.lang.NullPointerException
        at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:76)
        at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:118)
        at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1602)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:210)
        at org.hibernate.jpa.internal.EntityManagerImpl.<init>(EntityManagerImpl.java:91)
        at org.hibernate.jpa.internal.EntityManagerFactoryImpl.internalCreateEntityManager(EntityManagerFactoryImpl.java:345)
        at org.hibernate.jpa.internal.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:313)
        at org.glassfish.persistence.jpa.JPADeployer$2.visitPUD(JPADeployer.java:451)
        at org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:510)
        at org.glassfish.persistence.jpa.JPADeployer.iterateInitializedPUsAtApplicationPrepare(JPADeployer.java:492)
        at org.glassfish.persistence.jpa.JPADeployer.event(JPADeployer.java:398)
        at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:131)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:487)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
        at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:535)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:360)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:534)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:565)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:557)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:360)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:109)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
        at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
        at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
        at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189)
        at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
        at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
        at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
        at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
        at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
        at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
        at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
        at java.lang.Thread.run(Thread.java:745)
     
    Zuletzt bearbeitet: 21. Apr. 2016
  2. Vielleicht hilft dir dieser Kurs hier weiter --> (hier klicken)
  3. thecain
    thecain Aktives Mitglied
    Die Verbindung zur Db in der Standalone xml o.ä. je nach appl. server. stimmt?
     
  4. Manuba
    Manuba Neues Mitglied
    Ja, ich habe die Verbindung über das Glassfish Admin Panel getestet und es funktioniert.
     
Die Seite wird geladen...

EntityManager ist null - Ähnliche Themen

Mein EntityManager oder EntityManagerFactory liefern ständig null zurück
Mein EntityManager oder EntityManagerFactory liefern ständig null zurück im Forum Data Tier
EntityManager null?
EntityManager null? im Forum Datenbankprogrammierung
NullPointerException bei VORHANDENEM EntityManager-Objekt!!!
NullPointerException bei VORHANDENEM EntityManager-Objekt!!! im Forum Datenbankprogrammierung
injizierter EntityManager (Seam) null
injizierter EntityManager (Seam) null im Forum Data Tier
Injected EntityManager und EntityManagerFactory ist null
Injected EntityManager und EntityManagerFactory ist null im Forum Data Tier
Thema: EntityManager ist null