简体   繁体   English

空指针异常EntityManager弹簧

[英]Null Pointer Exception EntityManager spring

i'm getting a null pointer exception when i'm trying to persist. 当我尝试保留时,我得到了空指针异常。 i'm going to list files that i think related with the error . 我将列出与错误有关的文件。 can someone help me ? 有人能帮我吗 ?

this is my class opmanagerImpl 这是我班的opmanagerImpl

package com.ensi.dao;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.ensi.entitis.opération;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class opmanagerImpl implements opmanager {
    @PersistenceContext(unitName="ERP_PCD")
    private EntityManager em;
    public opmanagerImpl(){};
    public void creerOpération(opération op) {
        if(em==null)System.out.print("error ");
            em.persist(op);
        }
    }

this is my persistence.xml 这是我的persistence.xml

<persistence-unit name="ERP_PCD" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:/DefaultDS</jta-data-source>
    <properties>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="hibernate.connection.url"      value="jdbc:postgresql://localhost:5432/persistance" />
        <property name="hibernate.connection.username" value="postgres" />
        <property name="hibernate.connection.password" value="ensien" />
    </properties>
</persistence-unit>

this is my applicationContext.xml 这是我的applicationContext.xml

<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.postgresql.jdbc.Driver"></property>
    <property name="url" value="jdbc:postgresql://localhost:5432/persistance"></property>
    <property name="username" value="postgres"></property>
    <property name="password" value="ensien"></property>
</bean>

<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    <property name="defaultDataSource" ref="datasource"></property>
    <property name="persistenceXmlLocations">
        <list>
            <value>classpath*:META-INF/persistence.xml</value>
        </list>
    </property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="ERP_PCD"></property>
</bean>

<bean id="em" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean> 
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean name="dao" class="com.ensi.dao.opmanagerImpl"> </bean>
<bean name="metier"  class="org.ensi.metier.TestImpl">
    <property name="op" ref="dao"></property>
</bean>
<context:annotation-config></context:annotation-config>

the main: 主要的:

package com.ensi.dao;
import org.ensi.entitis.opération;
import org.hibernate.validator.internal.util.privilegedactions.GetConstructor;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Maintest {
    public static void main(String[] args) {
        opmanagerImpl xx = new opmanagerImpl();
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:META-                  INF/applicationContext1.xml");
        opération op=new opération(15, 'f', 120, "bonjour", null, 12, 15);
        xx.creerOpération(op);
        //System.out.println("hiiiiiiiiiiii");
    }
}

Results: 结果:

252 [main] INFO org.springframework.context.support.ClassPathXmlApplicationContext -     Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1b61d282:    startup date [Sat May 03 02:43:19 CEST 2014]; root of context hierarchy
493 [main] INFO org.springframework.beans.factory.support.DefaultListableBeanFactory -   Pre-instantiating singletons in   org.springframework.beans.factory.support.DefaultListableBeanFactory@5ab6e2e3: defining   beans []; root of factory hierarchy
Exception in thread "main" java.lang.NullPointerException   
at com.ensi.dao.opmanagerImpl.creerOpération(opmanagerImpl.java:19) 
at com.ensi.dao.Maintest.main(Maintest.java:16) error `

T̶r̶y̶ ̶c̶h̶a̶n̶g̶i̶n̶g̶ ̶ T̶r̶y̶̶c̶h̶a̶n̶g̶i̶n̶g̶

̶@̶P̶e̶r̶s̶i̶s̶t̶e̶n̶c̶e̶C̶o̶n̶t̶e̶x̶t̶(̶u̶n̶i̶t̶N̶a̶m̶e̶=̶"̶E̶R̶P̶_̶P̶C̶D̶"̶)̶
̶p̶r̶i̶v̶a̶t̶e̶ ̶E̶n̶t̶i̶t̶y̶M̶a̶n̶a̶g̶e̶r̶ ̶e̶m̶;̶

T̶o̶

@̶A̶u̶t̶o̶w̶i̶r̶e̶d̶ ̶
p̶r̶i̶v̶a̶t̶e̶ ̶E̶n̶t̶i̶t̶y̶M̶a̶n̶a̶g̶e̶r̶ ̶e̶m̶;̶

The problem is that you're manually creating opmanagerImpl xx instance: 问题是您正在手动创建opmanagerImpl xx实例:

public static void main(String[] args) {
    opmanagerImpl xx = new opmanagerImpl();
    //rest of the code...
}

When using Spring, you should not create your beans manually, instead let Spring create them and obtain an instance of the bean through the application context. 使用Spring时,您不应手动创建bean,而应让Spring创建它们并通过应用程序上下文获取bean的实例。 Change the code to: 将代码更改为:

public static void main(String[] args) {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:META-INF/applicationContext1.xml");
    opmanagerImpl xx = ctx.getBean("dao", opmanagerImpl .class);
    opération op = new opération(15, 'f', 120, "bonjour", null, 12, 15);
    xx.creerOpération(op);
    //System.out.println("hiiiiiiiiiiii");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM