1

したがって、JPA を機能させるために 1 日を費やしました。

私の設定を投稿します

appContext.xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value ="org.postgresql.Driver"/>
        <property name="url" value ="jdbc:postgresql://localhost:5432/db"/>
        <property name="username" value ="postgres"/>
        <property name="password" value ="pwd"/>
    </bean>


 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property>
    <property name="persistenceUnitName" value="hibernatePersistenceUnit" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
            <property name="showSql" value="true"/>
        </bean>
    </property>
 </bean> -->



<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean> 

    <context:annotation-config />
    <tx:annotation-driven transaction-manager="transactionManager" mode="aspectj" proxy-target-class="true"/>

私の Chain.java エンティティ

@Entity
@Table(name ="Chain")
public class Chain implements Serializable{

      private Long id;
      private String name;



@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator ="Chain_id_seq")
@SequenceGenerator(name="Chain_id_seq", sequenceName="Chain_id_seq")
public Long getId(){ return id;}
public void setId(Long id){ this.id = id;}

@Column(name = "name")
public String getName(){return name;}
public void setName(String value) {this.name = value;}

私の ChainServiceImpl.java クラス

@Service("addChainService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ChainServiceImpl implements ChainService{

    @Autowired
    private ChainDao chainDao;


    @Autowired
    public ChainServiceImpl(ChainDao ChainDao) {
    this.chainDao = ChainDao;
    }

    public ChainServiceImpl() {
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int addCaChain(Chain chain) {
        int id  = chainDao.saveCaChain(chain);
        return id;
    }
}

マイ ChainDaoImpl.java クラス

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

    private EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        this. entityManager = em;
    }




    public int saveChain(Chain chain) {

        entityManager.persist(chain); //em.merge(u); for updates
        entityManager.flush();
        return 9;
    } 
}

例外トレース

javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:733) ~[hibernate-entitymanager-3.5.0-Final.jar:3.5.0-Final]
    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.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365) ~[spring-orm-3.1.3.RELEASE.jar:3.1.3.RELEASE]
    at $Proxy18.flush(Unknown Source) ~[na: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.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240) ~[spring-orm-3.1.3.RELEASE.jar:3.1.3.RELEASE]
    at $Proxy18.flush(Unknown Source) ~[na:na]
    at pl.signet.famoc.postgresql.jpa.dao.CaChainDaoImpl.saveCaChain(CaChainDaoImpl.java:47) ~[CaChainDaoImpl.class:na]
    at pl.signet.famoc.postgresql.jpa.service.SignetCAStorageChainServiceImpl.addCaChain(SignetCAStorageChainServiceImpl.java:33) ~[SignetCAStorageChainServiceImpl.class:na]
    at pl.signet.famoc.ws.spring.endpoint.FamocCaEndpoint.getCAChain(FamocCaEndpoint.java:42) ~[FamocCaEndpoint.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]

entityManager.flush()フラッシュを使用しないと例外が発生します。例外はありませんが、データベースでは何も起こらず、レコードが挿入されません。

誰が私が欠けているのか手がかりを持っていますか?

4

1 に答える 1

2

<context:component-scan>appContext.xmlにが必要だと思います。<context:annotation-config>アプリケーションコンテキストにすでに登録されているBeanのアノテーションをアクティブ化するのは1つだけです。component-scanどちらもパッケージをスキャンしてBeanを登録し、アノテーションをアクティブ化します。component-scanアノテーションがないため、@Transactionalアノテーションはトランザクションでメソッドをラップしていません。

component-scannとannotation-configの違いに関する良い投稿です。

于 2012-12-07T15:58:41.967 に答える