javax.persistence.TransactionRequiredException: No transactional EntityManager available
org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:275)
com.sun.proxy.$Proxy25.persist(Unknown Source)
com.cnp.incident.dao.FluxEntreesDAO.ajouterFlux(FluxEntreesDAO.java:37)
com.cnp.incident.services.ServiceFluxEntrees.ajouterFlux(ServiceFluxEntrees.java:33)
com.cnp.incident.controller.CreationManuelleFluxEntreesSorties.creer(CreationManuelleFluxEntreesSorties.java:64)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}"/>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:database.properties</value>
</list>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.cnp.incident"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Unite de persistence -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.cnp.incident"/>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="${hibernate.dialect}"/>
<entry key="hibernate.query.factory_class" value="org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory"/>
<entry key="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}"/>
<!-- A utiliser pour faciliter le debug -->
<entry key="hibernate.show_sql" value="${hibernate.show_sql}"/>
<entry key="hibernate.format_sql" value="${hibernate.format_sql}"/>
<!-- activation cache d'entités et de query -->
</map>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<!-- on référence la datasource "lazy" -->
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
package com.cnp.incident.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.springframework.stereotype.Repository;
import com.cnp.incident.entite.FluxEntrees;
@Repository
public class FluxEntreesDAO implements IfluxEntreesDAO {
@PersistenceContext
private EntityManager entityManager;
//implémentation de la methode listeFlux de la DAO
public List<FluxEntrees> listeFlux() {
final CriteriaBuilder lCriteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<FluxEntrees> lCriteriaQuery = lCriteriaBuilder.createQuery(FluxEntrees.class);
final Root<FluxEntrees> lRoot = lCriteriaQuery.from(FluxEntrees.class);
lCriteriaQuery.select(lRoot);
final TypedQuery<FluxEntrees> lTypedQuery = entityManager.createQuery(lCriteriaQuery);
return lTypedQuery.getResultList();
}
//methode d'ajout de flux en base de données
public void ajouterFlux(final FluxEntrees pFlux) {
entityManager.persist(pFlux);
}
}
package com.cnp.incident.services;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cnp.incident.dao.IfluxEntreesDAO;
import com.cnp.incident.entite.FluxEntrees;
@Service
public class ServiceFluxEntrees implements IServiceFluxEntrees{
@Autowired
private IfluxEntreesDAO dao;
@Transactional(readOnly=true)
public List<FluxEntrees> listeFlux() {
return dao.listeFlux();
}
@Transactional
public void ajouterFlux(Date mois, Integer entree, Integer sorties, String type) {
final FluxEntrees fluxEntrees = new FluxEntrees();
fluxEntrees.setMois(mois);
fluxEntrees.setEntrees(entree);
fluxEntrees.setSorties(sorties);
fluxEntrees.setType(type);
dao.ajouterFlux(fluxEntrees);
}
}*
答案 0 :(得分:0)
尝试对事务管理器配置进行这些更改(我只列出需要更改的部分):
<tx:annotation-driven transaction-manager="txManager"/>
...
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>