Cannot call senderror() after response has been committed Tomcat Spring MVC - spring

If I am returning a Employee object in my controller class, I get a response from my REST service. But if I am trying to return a List, then I frequently get this error:
Error:cannot call senderror() after the response has been committed
(Also atttached the image)
I am using Eclipse, tomcat, maven and making a spring REST service.
Here is my Controller class code snippetwhich is returning Employee, and everything is fine and I can see my JSON data on browser. But when I change Employee to List as my new return type, I receive the following error and my Eclipse also hangs for few minutes. I think my problem could have to do with the JSON convertor. Please help.
#Controller
#RequestMapping("/emp")
public class EMPController {
#Autowired
EmployeeService empservice;
#RequestMapping("byname3/{name}")
#ResponseBody
public Employee getByName(#PathVariable String name) {
System.out.println("Inside getByName()..successfully..EMPController");
.....
......
My dispatcher-servlet.xml
<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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.spice.controller" />
<context:component-scan base-package="com.spice.daoImpl" />
<context:component-scan base-package="com.spice.serviceImpl " />
<mvc:annotation-driven /> <!-- tHIS HELPS IN AUTOMATIC JSON-TO-JAVA AND VICE VERSA CONVERTUION -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe"></property>
<property name="username" value="tiwari"></property>
<property name="password" value="tiwari"></property>
</bean>
<bean id="mysessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- eARLIER i WAS USING org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean -->
<!-- BUT THIS LEADS TO SOME CACHE PROVIDER CLASS NOT FOUND EXCEPTION -->
<!-- ALSO THIS AnnotationSessionFactoryBean IS NOW REPLACED BY LocalSessionFactoryBean -->
<property name="dataSource" ref="dataSource"></property>
<property name="annotatedClasses">
<list>
<value>com.spice.beans.Employee</value>
<value>com.spice.beans.Address</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<!-- THIS CLASS IS COMPATIBLE WITH ABOVE VERSIONS OF SPRING AND HIBERNATE -->
<property name="sessionFactory" ref="mysessionFactory" />
</bean>
</beans>

Error:cannot call senderror() after the response has been committed
(Also atttached the image)
This error is an aftermath of a different error. The server encountered a main exception with your application and hence it tried to call HttpServletResponse#sendError(), which throws IllegalStateException in turn.

Related

Hibernate5, Spring 4 - org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread

We are upgrading our project from Hibernate 3.6.10 to Hibernate 5.2.0 but getting errors while migrating. I have gone through many posts here on StackOverFlow and Google but did not found any solution.
Consider the code reference this website. I only made the database connections are to PostgreSQL database.
If I run this sample after adding these JARs (ALL Added JAR files screenshot) except the hibernate JAR is 3.6.10-final, It works totally fine. But if I make these changes for hibernate 5 upgrade:
Changing JAR file added as Hibernate-core-5.2.1-Final.jar
Changing reference of hibernate3 files to hibernate5(3 replacements; 1 in EmployeeDao import and another 2 in applicationContext.xml)
It throws the following error:
Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
at org.springframework.orm.hibernate5.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1132)
at org.springframework.orm.hibernate5.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:618)
at org.springframework.orm.hibernate5.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:615)
at org.springframework.orm.hibernate5.HibernateTemplate.doExecute(HibernateTemplate.java:340)
at org.springframework.orm.hibernate5.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:307)
at org.springframework.orm.hibernate5.HibernateTemplate.save(HibernateTemplate.java:615)
at hibernate.test.EmployeeDao.saveEmployee(EmployeeDao.java:12)
at hibernate.test.InsertTest.main(InsertTest.java:21)
And I made these changes as well but got the same error:
1. Commenting this in EmployeeDao:
/*HibernateTemplate template;
public void setTemplate(HibernateTemplate template) {
this.template = template;
} */
and extending HibernateDaoSupport, so that it gives sessionFactory setter.
And replacing below code to:
public void saveEmployee(Employee e){
template.save(e);
}
this:
public void saveEmployee(Employee e){
this.getHibernateTemplate().save(e);
}
After debugging I came to know that in HibernateTemplate class provided by Spring-orm-4.3.1 Jar is throwing the mentioned error.
Here is the snapshot.
Can anyone help me out with this :O. We have stuck here for ages. I appreciate help.
create DataSource.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:properties/database.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
create sessionFactory using the a seperate hibernate.xml file as given below
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.rapidtech.rapidtechorganic.model.Lot</value>
<value>com.rapidtech.rapidtechorganic.model.ProductAvailable</value>
<value>com.rapidtech.rapidtechorganic.model.Client</value>
<value>com.rapidtech.rapidtechorganic.model.Invoice</value>
<value>com.rapidtech.rapidtechorganic.model.ProductBuyed</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
Include both the xml file in your servel-context.xml
<!-- Database Configuration -->
<beans:import resource="classpath:database/DataSource.xml"/>
<beans:import resource="classpath:database/Hibernate.xml"/>
Then in your Abstract class inject sessionFactory
#Resource
private SessionFactory sessionFactory;
public void save(Entity entity){
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(entity);
session.getTransaction().commit();
session.close();
}

No bean named 'entityManagerFactory' is defiend, bean reference issue

I am using spring-framework 4.0.2.RELEASE and hibernate-3.6.4.Final.
This spring data jpa configuration gives me an error:
No bean named 'entityManagerFactory' is defiend.
However I found that if I change bean name emf to entityManagerFactory then, there is no problem.
Can somebody explain why the bean reference does not work here?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-2.5.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- http://drypot.com/post/95?p=6 spring + hibernate: with JPA -->
<tx:annotation-driven />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:packagesToScan="${entitymanager.packagesToScan}" >
<property name="dataSource" ref="dataSource" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<!-- validate | update | create | create-drop -->
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<!--prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop-->
<!-- hibernate ehcache
http://stackoverflow.com/questions/5270998/spring-hibernate-ehcache
-->
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
<prop key="hibernate.cache.factory_class">${hibernate.cache.factory_class}</prop>
</props>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>
</beans>
When working with Spring Data JPA it, by default, looks for a bean named entityManagerFactory. This is the default for both the xml namespace based configuration (<jpa:repositories />) and for the java bases configuration (#EnableJpaRepositories).
If you have a bean with another name you will have to make that clear to Spring Data JPA so that it can select the proper EntityManagerFactory to use.
<task:repositories entity-manager-factory-ref="emf" />
or java config
#EnableJpaRepositories(entityManagerFactoryRef="emf")
Links
Spring Data JPA reference guide
#EnableJpaRepositories javadoc
Try injecting your EntityManagerFactory specifing your bean's name like this:
#Autowired
#Qualifier(value = "emf")
private EntityManagerFactory entityManagerFactory;

An AnnotationConfiguration instance is required to use <myClass>

Below is the EmployeeDaoImpl file which is injecting sessionFactory.
#Repository
public class EmployeeDaoImpl implements EmployeeDao {
#Autowired
private SessionFactory sessionFactory;
#Override
public void addEmployee(TestEmployee employee) {
this.sessionFactory.getCurrentSession().save(employee);
}
#SuppressWarnings("unchecked")
#Override
public List<TestEmployee> getAllEmployees() {
return this.sessionFactory.getCurrentSession().createQuery("from TestEmployee").list();
}
#Override
public void deleteEmployee(Integer employeeId) {
TestEmployee employee = (TestEmployee) sessionFactory.getCurrentSession().load(
TestEmployee.class, employeeId);
if (null != employee) {
this.sessionFactory.getCurrentSession().delete(employee);
}
}
}
Below is my employee-servlet file
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config />
<context:component-scan base-package="com.rights.controller" />
<mvc:annotation-driven />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="employeeDAO" class="com.rights.dao.EmployeeDaoImpl"></bean>
<bean id="employeeManager" class="com.rights.services.EmployeeManagerImpl"></bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
I am getting An AnnotationConfiguration instance is required error. I have searched it on net but not able to solve this. I have the run configuration. I am using hibernate 3 and spring 3.1 JARS. Kindly help
daoin
<context:component-scan base-package="com.rights.controller" />
you seems like you forgot the package of your repository you should add it using :
<context:component-scan base-package="com.rights.controller com.rights.dao" />
in the configuration you posted, the class with annotation #Repository may not be post-processed by spring, if it package is not include in the component scan
I hope it may help you

Hibernate Transactions wont roll-back while testing

I'm working with Spring 3 , and I use Hibernate3 as the O/R mapper.While testing my DAO classes I intended to declare my test classes as #Transactional, so that I could rollback the stuff in my DB. DAO classes work fine, however the rollback never happens. this is how my test class looks like:
#BeforeTransaction
public void createTestData() {
// instantiate User objects
}
#Before
public void insertTestData() {
jdbcTemplate.update("INSERT INTO USER VALUES" + user1);
jdbcTemplate.update("INSERT INTO USER VALUES" + user2);
jdbcTemplate.update("INSERT INTO USER VALUES" + user3);
}
#Test
#Rollback(true)
public void testCheckAvailability() {
boolean trueResult = userDao.checkAvailability("shaaadi");
boolean falseResult = userDao.checkAvailability("elthefar");
assertEquals("Invalid output", true, trueResult);
assertEquals("Invalid output", false, falseResult);
}
}
not to forget about the xml file contents:
<?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:p="http://www.springframework.org/schema/p"
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/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/database.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource">
<property name="annotatedClasses">
<list>
<value>ir.cms.domain.user.User</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.format_sql=true
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
<bean id="userDao"
class="ir.cms.dao.user.impl.UserDaoImpl"
p:sessionFactory-ref="sessionFactory" />
</beans>
Finally , I should add that DAO classes are not transactional , and are only annotated as #Repository.
I appreciate any suggestion :)
My Problem is finally solved. I was using MySQL, and InnoDB mode was off.

The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-driven'

I am trying to configure JSF+Spring+hibernate and I'm tying to run a test but when I use this "tx:annotation-driven" on my application-context.xml file, I get this error:
The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-driven'
Here is my application-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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.6.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.6.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.6.xsd
" xmlns:tool="http://www.springframework.org/schema/tool">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:#192.168.56.101:1521:Gpsi"/>
<property name="username" value="omar"/>
<property name="password" value="omar"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>om.mycompany.model.Course</value>
<value>om.mycompany.model.Student</value>
<value>om.mycompany.model.Teacher</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction.manager="transactionManager"/>
<context:annotation-config/>
<context:component-scan base.package="com.mmycompany"/>
</beans>
and here is my CourseServiceImplTest. I have still not implemented the tests:
public class CourseServiceImplTest {
private static ClassPathXmlApplicationContext context;
private static CourseService courseService;
public CourseServiceImplTest() {
}
#BeforeClass
public static void setUpClass() throws Exception {
context=new ClassPathXmlApplicationContext("application-context.xml");
courseService=(CourseService) context.getBean("courseService");
}
#AfterClass
public static void tearDownClass() throws Exception {
context.close();
}
#Before
public void setUp() {
}
#After
public void tearDown() {
}
/**
* Test of getAllCourses method, of class CourseServiceImpl.
*/
#Test
public void testGetAllCourses() {
System.out.println("getAllCourses");
CourseServiceImpl instance = new CourseServiceImpl();
List expResult = null;
List result = instance.getAllCourses();
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
/**
* Test of getCourse method, of class CourseServiceImpl.
*/
#Test
public void testGetCourse() {
System.out.println("getCourse");
Integer id = null;
CourseServiceImpl instance = new CourseServiceImpl();
Course expResult = null;
Course result = instance.getCourse(id);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
and here is the CourseServiceImpl:
#Service("courseService")
#Transactional
public class CourseServiceImpl implements CourseService{
#Autowired
private SessionFactory sessionFactory;
#Override
public List<Course> getAllCourses() {
return sessionFactory.getCurrentSession().createQuery("from Course").list();
}
#Override
public Course getCourse(Integer id) {
return (Course) sessionFactory.getCurrentSession().get(Course.class, id);
}
#Override
public void save(Course course) {
sessionFactory.getCurrentSession().saveOrUpdate(course);
}
}
You have some errors in your appcontext.xml:
Use *-2.5.xsd
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
Typos in tx:annotation-driven and context:component-scan (. instead of -)
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="com.mmycompany" />
This is for others (like me :) ). Don't forget to add the spring tx jar/maven dependency.
Also correct configuration in appctx is:
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
, by mistake wrong configuration which others may have
xmlns:tx="http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
i.e., extra "/spring-tx-3.1.xsd"
xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
in other words what is there in xmlns(namespace) should have proper mapping in
schemaLocation (namespace vs schema).
namespace here is : http://www.springframework.org/schema/tx
schema Doc Of namespace is : http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
this schema of namespace later is mapped in jar to locate the path of actual xsd located in org.springframework.transaction.config
For me the thing that worked was the order in which the namespaces were defined in the xsi:schemaLocation tag : [ since the version was all good and also it was transaction-manager already ]
The error was with :
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
AND RESOLVED WITH :
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
I'm learning from udemy. I followed every step that my instructor show me to do.
In spring mvc crud section while setting up the devlopment environment i had the same error for:
<mvc:annotation-driven/> and <tx:annotation-driven transaction-manager="myTransactionManager" />
then i just replaced
http://www.springframework.org/schema/mvc/spring-mvc.xsd
with
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
and
http://www.springframework.org/schema/tx/spring-tx.xsd
with
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
actually i visited these two sites
http://www.springframework.org/schema/mvc/ and http://www.springframework.org/schema/tx/
and just added the latest version of spring-mvc and spring-tx i.e, spring-mvc-4.2.xsd and spring-tx-4.2.xsd
So, i suggest to try this.
Hope this helps.
Thank you.
One extra forward slash (/) in front of tx and the *.xml file troubled me for 8 hours!!
My mistake:
http://www.springframework.org/schema/tx/ http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
Correction:
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
Indeed one character less/more manages to keep programmers busy for hours!
In my case this was actually a symptom of the server, hosted on AWS, lacking an IP for the external network. It would attempt to download namespaces from springframework.org and fail to make a connection.
FWIW I had this same issue. Turned out my xsi:schemaLocation entries were incorrect, so I went to the official docs and pasted theirs into mine:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html section 16.5.6
I had to add a couple more but that was ok. Next up is to find out why this fixed the problem...
Make sure that Spring version and xsd version both are same.In my case I am using Spring 4.1.1 so my all xsd should be version *-4.1.xsd
Any one can help for me!!!!!!!!!
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop/ http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee/ http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang/ http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx/ http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config />(ERROR OCCUR)
<context:component-scan base-package="hiberrSpring" /> (ERROR OCCUR)
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages"></property>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties"></bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${com.mysql.jdbc.Driver}"
p:url="${jdbc:mysql://localhost/}" p:username="${root}"
p:password="${rajini}"></bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${org.hibernate.dialect.MySQLDialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="employeeDAO" class="hiberrSpring.EmployeeDaoImpl"></bean>
<bean id="employeeManager" class="hiberrSpring.EmployeeManagerImpl"></bean>
<tx:annotation-driven /> (ERROR OCCUR)
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>

Resources