I have an application which I am developing Spring with Hibernate. Every thing went fine but when I try to test my transactions with the hibernate classes, it throwing an error saying "org.hibernate.HibernateException: No Session found for current thread". I can not find the error with me. Help would be really appreciated.
My main class :
public static void main(String[] args) {
Patient sac = new Patient();
sac.setPatient_Id("M2314");
sac.setPatient_Age(23);
sac.setPatient_Name("Sac");
sac.setPatient_Address("SGTY NY");
System.out.println("load context");
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml");
PatientService patientService = (PatientService) context.getBean("patientService");
patientService.persistPatient(sac);
context.close();
}
Part of serviceImpl :
#Service("patientService")
public class PatientServiceImpl implements PatientService{
#Autowired
PatientDAO patientdao;
#Override
#Transactional
public void persistPatient(Patient patient) {
patientdao.persistPatient(patient);
}
Part of DAO class :
#Repository("patientdao")
public class PatientDAOImpl implements PatientDAO{
#Autowired
private SessionFactory sessionFactory;
#Override
#Transactional
public void persistPatient(Patient patient) {
sessionFactory.getCurrentSession().persist(patient);
}
My servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.tela.pms" />
<beans:bean id="patientService" class="com.tela.pms.service.impl.PatientServiceImpl"/>
<beans:bean id="patientdao" class="com.tela.pms.dao.impl.PatientDAOImpl"/>
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url" value="jdbc:mysql://localhost:3306/test" />
<beans:property name="username" value="root" />
<beans:property name="password" value="root" />
</beans:bean>
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource"></beans:property>
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>com.tela.pms.domain.Patient</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
</beans:bean>
</beans:beans>
In this case, I am not sure if you have enabled annotation-driven transaction management, as you have not provided the code for SpringBean.xml file.
You need to add this tag <tx:annotation-driven/> tag in your servlet-context.xml
In there also make sure to make it point to your transaction manager as follows
<tx:annotation-driven transaction-manager="transactionManager"/>
Rest of your code looks fine and this error should go away with above changes.
You have #Transactional the when and where but not the how. Add <tx:annotation-driven /> to enable annotation processing for #Transactional.
Adding will be a solution, but I got another related to that. So I had to do this chang in my servlet-context.xml (part of it is here):
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<tx:annotation-driven />
Changes :
add <tx:annotation-driven />. And notice that added http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd to the xsi:schemaLocation in beans:beans declaration. Then it will work fine.
Related
I am getting error
nested exception is java.lang.NoSuchMethodError:
javax/persistence/Table.indexes()[Ljavax/persistence/Index;
my controller-servlet.xml file is
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/jsp/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<beans:property name="url"
value="jdbc:oracle:thin:" />
<beans:property name="username" value="user" />
<beans:property name="password" value="123" />
</beans:bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>com.company.bmdashboard.beans.BatchJob</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="bMDashboardRepository" class="com.company.bmdashboard.repositories.BMDashboardRepositoryImpl">
<beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="bMDashboardService" class="com.company.bmdashboard.services.BMDashboardServiceImpl">
<beans:property name="bMDashboardRepository" ref="bMDashboardRepository"></beans:property>
</beans:bean>
<context:component-scan base-package="com.company.bmdashboard" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
</beans:beans>
The jars file I have are
my RepositoryImpl is
#Repository
public class BMDashboardRepositoryImpl implements BMDashboardRepository{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf){
this.sessionFactory = sf;
}
#Override
public ArrayList<BatchJob> retrieveAllBatchJobs() {
// TODO Auto-generated method stub
System.out.println("Repository Impl: in retrieve all batch jobs");
Session session= sessionFactory.getCurrentSession();
#SuppressWarnings("unchecked")
List<BatchJob> allBatchJobs=session.createQuery("From BatchJob").list();
for(BatchJob bj:allBatchJobs)
{
System.out.println(bj.toString());
}
return (ArrayList<BatchJob>) allBatchJobs;
}
}
When I am publishing the code in webspehre, I am getting this error. I think there is some problem with jar files?
thanks in advance
I found the work around for this.
I was using WAS 8.5 which provides its own JDK which supports JPA 2.0 . #Table.indexes() method was introduced in JPA 2.1 .
Solution for this
1)Upgrade WAS to 9
2)Instead of using #Table annotation try using xml mapping . I used ClassName.hbm.xml.
I have facing some exception in My Spring project ..
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:data/data.xml]
Offending resource: class path resource [spring/application-config.xml]; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx
application-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<beans:property name="maxUploadSize" value="20000000"/>
</beans:bean>
<mvc:annotation-driven>
<mvc:message-converters>
<beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<beans:property name="objectMapper">
<beans:bean class="com.danco.citiestalking.web.jackson.HibernateAwareObjectMapper" />
</beans:property>
</beans:bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:resources mapping="/resources/**" location="/content/" />
<context:component-scan base-package="com.app" />
<beans:bean id="companyFilter" class="com.aap.web.filters.CompanyFilter" />
<beans:import resource="classpath:data/data.xml" />
<beans:import resource="classpath:data/mail.xml" />
<!-- Ascpects -->
<aop:aspectj-autoproxy />
<beans:bean name="errorLogging" class="com.app.ErrorLoggingAspect"/>
<beans:bean name="debugLogging" class="com.app.DebugLoggingAspect"/>
<beans:bean name="traceLogging" class="com.danco.citiestalking.logging.TraceLoggingAspect"/>
<beans:bean class="com.danco.apns.service.Application" id="application">
<beans:constructor-arg type="String" value="${apns.sertificatePath}" />
<beans:constructor-arg type="String" value="${apns.sertificatePassword}" />
<beans:constructor-arg type="String" value="${apns.applicationName}" />
</beans:bean>
<context:component-scan base-package="com.danco.apns" />
<context:property-placeholder
location="classpath:asdasd.properties, classpath:asdasd.properties" ignore-unresolvable="true"/>
</beans:beans>
data.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<beans:bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<beans:property name="ignoreUnresolvablePlaceholders"
value="true" />
<beans:property name="location">
<beans:value>classpath:database.properties</beans:value>
</beans:property>
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory"></beans:property>
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager"
order="100" />
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="configLocation">
<beans:value>classpath:hibernate.cfg.xml</beans:value>
</beans:property>
<beans:property name="configurationClass">
<beans:value>org.hibernate.cfg.AnnotationConfiguration</beans:value>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">${jdbc.dialect} </beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
</beans:beans>
jar using
spring-aop-3.2.9.RELEASE.jar
spring-beans-3.2.8.RELEASE.jar
spring-context-3.2.8.RELEASE.jar
spring-context-support-3.2.9.RELEASE.jar
spring-core-3.2.9.RELEASE.jar
spring-expression-3.2.8.RELEASE.jar
spring-jdbc-3.2.9.RELEASE.jar
spring-orm-3.2.9.RELEASE.jar
spring-tx-3.2.9.RELEASE.jar
spring-web-3.2.8.RELEASE.jar
spring-webmvc-3.2.8.RELEASE.jar
src/main/resource/spring/application-config.xml
src/main/resource/data/data.xml
I want to add webflow to a web. I have the webflow dependency in the pom.xml, I have started to add beans, but seems unable to find any tag, as I see in every tutorial. I have all my beans in servlet-context.xml, and this is it:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- <beans:bean class="my.package.Controllers.LoginController"></beans:bean>-->
<!-- <beans:bean class="my.package.Controllers.NewUserFormController"></beans:bean>-->
<!-- <beans:bean class="my.package.Controllers.QueEsController"></beans:bean>-->
<beans:bean class="my.package.Services.UsuarioServiceImp"></beans:bean>
<beans:bean class="my.package.Dao.UserDaoImp">
<beans:property name="dataSource" ref="dataSource"></beans:property>
</beans:bean>
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver"></beans:property>
<beans:property name="url" value="jdbc:mysql://localhost:3306/myschema"></beans:property>
<beans:property name="username" value="myusername"></beans:property>
<beans:property name="password" value="mypassword"></beans:property>
</beans:bean>
<beans:bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<beans:property name="dataSource" ref="dataSource"></beans:property>
</beans:bean>
<context:component-scan base-package="my.package" />
<beans:bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<beans:property name="mappings">
<beans:value>inicio.do=flowController</beans:value>
</beans:property>
<beans:property name="alwaysUseFullPath" value="true"></beans:property>
</beans:bean>
<beans:bean id="flowcontroller" class="org.springframework.webflow.mvc.servlet.FlowController">
<beans:property name="flowExecutor" ref="flowExecutor"></beans:property>
</beans:bean>
When I try to add any webflow tag, for example:
<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry"/>
It show an error:
Multiple annotations found at this line:
- The prefix "webflow" for element "webflow:flow-executor" is not
bound.
Seems that he can't find any webflow-related tag, despite I have it in the schemaLocation.
Any help? Thank you.
EDIT: Im using 3.1.1-RELEASE version of Spring, as seen on the pom.xml:
<org.springframework-version>3.1.1.RELEASE</org.springframework-version>
Include webflow namespace as shown below:
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd">
It's many similar, but not the same issues, so I couldn't find a solution for this one.
I have Spring + JPA(Hibernate) web-application.
Context configuration (data-context.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<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="1234"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="packagesToScan" value="com.myapp.mvc.logic.domain"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<context:annotation-config/>
<context:component-scan base-package="com.myapp.mvc.logic" />
DispetcherServlet configuration (servlet-context.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd
">
<annotation-driven validator="validator"/>
<resources mapping="/resources/**" location="/resources/" />
<default-servlet-handler/>
<beans:bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver">
<beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
</beans:bean>
<beans:bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" id="tilesConfigurer">
<beans:property name="definitions">
<beans:list>
<beans:value>/WEB-INF/layouts/layouts.xml</beans:value>
<beans:value>/WEB-INF/views/**/views.xml</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<interceptors>
<beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang"/>
</interceptors>
<beans:bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
id="messageSource"
p:basenames="classpath:META-INF/i18n/application, classpath:META-INF/i18n/validation_messages"
/>
<beans:bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver"
id="localeResolver"
p:cookieName="locale"/>
<beans:bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<beans:property name="validationMessageSource" ref="messageSource"/>
</beans:bean>
<context:component-scan base-package="com.myapp.mvc" />
Controller:
#RequestMapping("/users")
#Controller
public class BankAccountCatalogController {
#Inject
private Functions functions;
#RequestMapping(value="/update", method = RequestMethod.POST)
public String catalogBankAccountUpdate(Model uiModel) {
functions.addUser();
return "employee_cabinet/catalog_bank_account";
}
}
Functions.class:
#Service("functions")
#Repository
#Transactional
public class Functions {
#PersistenceContext
private EntityManager em;
#Transactional
public void addUser() {
User user = new User();
user.setFirstName("John");
user.setLastName("John");
user.setMiddleName("John");
user.setEmail("John#gmail.com");
user.setPhone(null);
user.setMd5Password("1234");
em.persist(user);
em.flush();
}
}
When I activate the controller, in em.flush(); line I get an error:
javax.persistence.TransactionRequiredException: no transaction is in progress
On some reason transaction don't active in addUser() function. I tried run function in JUnit environment - it works fine.
Any ideas?
Try removing the EntityManager from the controller and specifying the method as transactional on the controller.
#RequestMapping("/users")
#Controller
public class BankAccountCatalogController {
#Inject
private Functions functions;
#RequestMapping(value="/update", method = RequestMethod.POST)
#Transactional
public String catalogBankAccountUpdate(Model uiModel) {
functions.addUser();
return "employee_cabinet/catalog_bank_account";
}
}
Also make sure the Functions class is specified in the packages you are component scanning, which is com.dominform.mvc.logic.
<context:component-scan base-package="com.dominform.mvc.logic" />
Solved. It was wrong configuration of DispetcherServlet.
Instead of
<context:component-scan base-package="com.myapp.mvc" />
in servlet-context.xml, I wrote
<context:component-scan base-package="com.myapp.mvc.web.controller" />
and it works.
Helpful topic: Spring #Transactional - javax.persistence.TransactionRequiredException
Any ideas, what could cause this error?
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Unable to locate Spring NamespaceHandler for
XML schema namespace [http://www.springframework.org/schema/data/jpa]
Offending resource: ServletContext resource
[/WEB-INF/spring/appServlet/servlet-context.xml]
Here is my 'servle-context.xml' (there are some problems with the indents, but the file is too huge...):
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.epam.mvc3.model" />
<context:component-scan base-package="com.epam.mvc3.controller" />
<!-- JPA -->
<beans:bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<beans:property name="location">
<beans:value>resources/database.properties</beans:value>
</beans:property>
</beans:bean>
<beans:bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName">
<beans:value>${jdbc.driverClassName}</beans:value>
</beans:property>
<beans:property name="url">
<beans:value>${jdbc.url}</beans:value>
</beans:property>
<beans:property name="username">
<beans:value>${jdbc.username}</beans:value>
</beans:property>
<beans:property name="password">
<beans:value>${jdbc.password}</beans:value>
</beans:property>
</beans:bean>
<beans:bean id="myEmf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<beans:property name="dataSource" ref="myDataSource" />
<beans:property name="persistenceUnitName" value="application" />
<beans:property name="persistenceXmlLocation"
value="classpath*:META-INF/persistence.xml" />
<beans:property name="jpaVendorAdapter" ref="hibernateVendor" />
<beans:property name="loadTimeWeaver">
<beans:bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"></beans:bean>
</beans:property>
</beans:bean>
<beans:bean id="hibernateVendor"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<beans:property name="entityManagerFactory" ref="myEmf"></beans:property>
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Spring Data configuration -->
<jpa:repositories base-package="com.epam.mvc3.repository"/>
</beans:beans>
As you can see, i specified the path to the jpa-schema. And I don't know what is the problem
Try to replace
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
by
http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
But if you still use Version 1.0-M1 then try to update first. DATAJPA-21
Make sure you have spring-data-jpa in your dependencies
If you use maven:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>
I fixed this by resolving the missing the spring-tx dependency in our project.
org.springframework:spring-tx
The issue is because the spring-jpa schema cannot be located . You might be having some old jar which does not contain the schema info .
I know this seems silly, but if you are using IDEA and Maven you should check to make sure you have Use plugin registry checked so that the server also has access to your jar files.
I would post a screen shot but I'm new on here and don't have enough reputation points. It does work though and I've verified with the support team at IntelliJ
After facing this same problem over a week, I found a solution. If you are using Spring MVC + JPA + Maven, use this dispatcher-servlet.xml
<?xml version="1.0" encoding="windows-1252"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Use #Component annotations for bean definitions -->
<context:component-scan base-package="PACKAGE WHERE YOU CAN FIND YOUR SOURCE CODE"/>
<!-- Use #Controller annotations for MVC controller definitions -->
<mvc:annotation-driven />
<!-- Add JPA support -->
<bean id="emf" class=
"org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="loadTimeWeaver">
<bean class=
"org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
<!-- Add Transaction support -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf"/>
</bean>
<!-- Use #Transaction annotations for managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- View resolver -->
<bean class=
<!-- HERE YOU REPLACE PROPERTY VALUE TO MATCH YOUR VIEW FOLDER (JSP PAGES) -->
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/web/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
too late, but this solved my problem:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.9.4.RELEASE</version>
</dependency>