0

returnPersist()のメソッドの使用に問題があります。ここに私のコードがあります。EntityManagerNullPointerExpception

@Repository("chainDao")
public class ChainDaoImpl implements ChainDao{

    private EntityManager em;

    @PersistenceContext(unitName="hibernatePersistenceUnit")
    public void setEntityManager(EntityManager em) {
    this.em = em;
    }

    public int saveChain(Chain chain) {
        em.persist(chain);
        return 0;
    }
}

期待はem.persist(chain)

これが私のpersistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

<persistence-unit name="hibernatePersistenceUnit" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.hbm2ddl.auto" value="none" />
</properties>
</persistence-unit>

</persistence>

とのフラグメントapplicationContext.xml

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="${database.driver}"
p:jdbcUrl="${database.url}"
p:user="${database.user}"
p:password="${database.password}"
p:acquireIncrement="5"
p:idleConnectionTestPeriod="60"
p:maxPoolSize="100"
p:maxStatements="50"
p:minPoolSize="10" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property>
<property name="persistenceUnitName" value="hibernatePersistenceUnit" />
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
<property name="showSql" value="true"/>
</bean>
</property>
</bean>

痕跡:

java.lang.NullPointerException: null
    at pl.postgresql.jpa.dao.ChainDaoImpl.saveCaChain(ChainDaoImpl.java:33) ~[CaChainDaoImpl.class:na]
    at pl.postgresql.jpa.service.StorageServiceImpl.addChain(StorageServiceImpl.java:50) ~[StorageServiceImpl.class:na]
    at pl.camel.jms.service.InputDatabaseServiceImpl.inputChain(InputDatabaseServiceImpl.java:48) ~[InputDatabaseServiceImpl.class:na]
    at pl.camel.jms.connector.EndpointHelper.run(EndpointHelper.java:49) ~[EndpointHelper.class:na]
    at pl.ws.spring.endpoint.CaEndpoint.getCAChain(CaEndpoint.java:63) ~[CaEndpoint.class:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_09]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_09]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_09]
    at java.lang.reflect.Method.invoke(Method.java:601) ~[na:1.7.0_09]
    at org.springframework.ws.server.endpoint.MethodEndpoint.invoke(MethodEndpoint.java:134) ~[spring-ws-core-2.1.1.RELEASE.jar:na]
    at org.springframework.ws.server.endpoint.adapter.DefaultMethodEndpointAdapter.invokeInternal(DefaultMethodEndpointAdapter.java:229) ~[spring-ws-core-2.1.1.RELEASE.jar:na]
    at org.springframework.ws.server.endpoint.adapter.AbstractMethodEndpointAdapter.invoke(AbstractMethodEndpointAdapter.java:53) ~[spring-ws-core-2.1.1.RELEASE.jar:na]
    at org.springframework.ws.server.MessageDispatcher.dispatch(MessageDispatcher.java:233) [spring-ws-core-2.1.1.RELEASE.jar:na]
    at org.springframework.ws.server.MessageDispatcher.receive(MessageDispatcher.java:173) [spring-ws-core-2.1.1.RELEASE.jar:na]
    at org.springframework.ws.transport.support.WebServiceMessageReceiverObjectSupport.handleConnection(WebServiceMessageReceiverObjectSupport.java:88) [spring-ws-core-2.1.1.RELEASE.jar:na]
    at org.springframework.ws.transport.http.WebServiceMessageReceiverHandlerAdapter.handle(WebServiceMessageReceiverHandlerAdapter.java:59) [spring-ws-core-2.1.1.RELEASE.jar:na]
    at org.springframework.ws.transport.http.MessageDispatcherServlet.doService(MessageDispatcherServlet.java:221) [spring-ws-core-2.1.1.RELEASE.jar:na]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) [spring-webmvc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789) [spring-webmvc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641) [servlet-api.jar:na]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) [servlet-api.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [catalina.jar:7.0.30.A]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.30.A]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [catalina.jar:7.0.30.A]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) [catalina.jar:7.0.30.A]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) [catalina.jar:7.0.30.A]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) [catalina.jar:7.0.30.A]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) [catalina.jar:7.0.30.A]
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929) [catalina.jar:7.0.30.A]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [catalina.jar:7.0.30.A]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) [catalina.jar:7.0.30.A]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002) [tomcat-coyote.jar:7.0.30.A]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585) [tomcat-coyote.jar:7.0.30.A]
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) [tomcat-coyote.jar:7.0.30.A]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [na:1.7.0_09]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [na:1.7.0_09]
    at java.lang.Thread.run(Thread.java:722) [na:1.7.0_09]

何が欠けているのですか?

4

1 に答える 1

2

挿入されるエンティティ マネージャーは単なるプロキシです。@Transactional実際のエンティティ マネージャの実装は、エンティティ マネージャを使用するメソッドに関連付けられたアノテーションの伝播属性値に基づいて、実行時に挿入されます。そこで、以下のようにメソッドsaveChain@Transactionalアノテーションを関連付けます。

@Transactional
public int saveChain(Chain chain) {
    em.persist(chain);
    return 0;
}

このアノテーションのドキュメントを読み、ユース ケースに関連する値でアノテーション属性を設定することを強くお勧めします。

編集

春のDIフレームワークはsetEntityManager、属性名がem. 属性またはセッターの名前を変更するか、注釈を属性に移動して試してemください。entityManagersetEntityManagersetEm@PersistenceContext

于 2012-12-06T10:25:30.477 に答える