Hello, I'm new to Spring and Hibernate.
I have generated with Hibernate Tools a database access module. The generator generates the code of the DAOS and Hibernate Beans.
When I test this module in a simple Java application all works fine, but when I test it in a Spring Web application I get a very strange error. Since my module is an independent jar it should access the database without regarding the circumstance of being executed in a simple Java application or a Web application. The code of my web application is:
Code:@Controller @RequestMapping("/") public class Controller implements ApplicationContextAware { private ApplicationContext applicationContext; @RequestMapping(value = "/purchased/songs", method = RequestMethod.GET) public String home(Model model) { SessionManager.startOperation(); ChargeTryDAOBase ctdb=new ChargeTryDAOBase(); List <ChargeTry> data=ctdb.findByRemoteId("dsfsdfsdf8"); SessionManager.endOperation(); model.addAttribute("result", "data" ); return "home"; } @Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { this.applicationContext = arg0; } }
When running this code on Tomcat I get following error:
When I change some Hibernate dependencies I get following error:Code:org.springframework.web.util.NestedServletException: Handler processing nested exception is java.lang.NoSuchMethodError: org.hibernate.SessionFactory.getCurrentSession()Lorg/hibernate/Session; ..... java.lang.NoSuchMethodError: org.hibernate.SessionFactory.getCurrentSession()Lorg/hibernate/Session;
When I test the above code in a simple Java application all works fine.Code:java.lang.IllegalStateException: Could not locate SessionFactory in JNDI
Is this a spring-hibernate configuration problem?
Is it possible to encapsulate all hibernate configuration in my hibernate.cfg.xml contained in my jar, not having to configure it in my upper Spring layer?
My hibernate.cfg.xml file is:
I think the problem is that I haven't enabled in my hibernate.cfg.xml the datasource for being used in Tomcat, but I'm not sure.Code:<hibernate-configuration> <session-factory> <property name="hibernate.bytecode.use_reflection_optimizer">false</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password" /> <property name="hibernate.connection.url">jdbc:mysql://database:3306/mydb</property> <property name="hibernate.connection.username">content</property> <property name="hibernate.current_session_context_class">thread</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> <property name="hibernate.use_sql_comments">true</property> </session-factory> </hibernate-configuration>
Many thanks for your advice and help.


Reply With Quote