当我试图坚持时,我得到一个空指针异常。 我要列出我认为与错误相关的文件。 有人能帮助我吗?
这是我的类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);
}
}
这是我的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>
这是我的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>
主要:
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");
}
}
结果:
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 `
答案 0 :(得分:0)
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̶;̶
要
@̶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̶;̶
问题是您手动创建opmanagerImpl xx实例:
public static void main(String[] args) {
opmanagerImpl xx = new opmanagerImpl();
//rest of the code...
}
使用Spring时,不应该手动创建bean,而是让Spring创建它们并通过应用程序上下文获取bean的实例。将代码更改为:
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");
}