Convert Spring LocalSessionFactoryBean to Hibernate SessionFactory -



Convert Spring LocalSessionFactoryBean to Hibernate SessionFactory -

i trying integrate spring hibernate. however, not able hibernate's sessionfactory object through spring's localsessionfactorybean.

i tried next approaches:

1) utilize either of org.springframework.orm.hibernate3 , org.springframework.orm.hibernate4 localsessionfactorybean class

2) utilize abstractsessionfactorybean class

3) seek sessionfactory=localsessionfactorybean.getobject() sessionfactory=localsessionfactorybean

here's project structure:

not allowed post images till reach 10 credits, sad..

here 's bookservice

bundle com.zzz.service; import org.hibernate.session; import org.hibernate.sessionfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.orm.hibernate3.abstractsessionfactorybean; import org.springframework.orm.hibernate4.localsessionfactorybean; import com.zzz.forms.bookform; public class bookservice { @autowired localsessionfactorybean hibernatesessionfactory; sessionfactory sessionfactory; public localsessionfactorybean gethibernatesessionfactory() { homecoming hibernatesessionfactory; } public void sethibernatesessionfactory(localsessionfactorybean hibernatesessionfactory) { this.hibernatesessionfactory = hibernatesessionfactory; } public sessionfactory getsessionfactory() { homecoming sessionfactory; } public void setsessionfactory(sessionfactory sessionfactory) { this.sessionfactory = sessionfactory; } public void storebookdetails(bookform bookform){ system.out.println("hibernae"); setsessionfactory((sessionfactory)hibernatesessionfactory.getobject()); session session=sessionfactory.opensession(); session.begintransaction(); session.save(bookform); session.gettransaction().commit(); session.close(); system.out.println("hibernae"); } }

here's controller leads service

bundle com.zzz.controllers; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.modelattribute; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.servlet.modelandview; import com.zzz.forms.bookform; import com.zzz.service.bookservice; @controller public class firstpagecontroller { bookservice bookservice; public bookservice getbookservice() { homecoming new bookservice(); } public void setbookservice(bookservice bookservice) { this.bookservice = bookservice; } @requestmapping(value="/firstpage") public modelandview showfirstpage() { homecoming new modelandview("books/book","bookform",new bookform()); } @requestmapping(value="/enterbookdetails") public modelandview enterbookdetails(@modelattribute("bookform") bookform bookform) { getbookservice().storebookdetails(bookform); system.out.println("book details followws"); system.out.println(bookform.getbookid()); system.out.println(bookform.getbookname()); homecoming new modelandview("books/booksubmitted","bookform",bookform); } }

application context:

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:hz="http://www.hazelcast.com/schema/spring" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.hazelcast.com/schema/spring http://www.hazelcast.com/schema/spring/hazelcast-spring-3.2.xsd"> <context:annotation-config/> <context:component-scan base-package="com.zzz.controllers"></context:component-scan> <mvc:annotation-driven/> <bean id="mydatasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="com.mysql.jdbc.driver" /> <property name="url" value="jdbc:mysql://localhost/zz" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <bean id="hibernatesessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean" > <property name="datasource" ref="mydatasource" /> <property name="packagestoscan" value="com.zzz.forms"/> <property name="hibernateproperties"> <value> hibernate.dialect=org.hibernate.dialect.mysql5dialect </value> </property> </bean> </beans>

and web xml

<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0"> <display-name>zzz</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:applicationcontext.xml</param-value> </context-param> </web-app>

the nullpointer looks this

org.springframework.web.util.nestedservletexception: request processing failed; nested exception java.lang.nullpointerexception org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:894) org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:789) javax.servlet.http.httpservlet.service(httpservlet.java:641) javax.servlet.http.httpservlet.service(httpservlet.java:722) root cause java.lang.nullpointerexception com.zzz.service.bookservice.storebookdetails(bookservice.java:38) com.zzz.controllers.firstpagecontroller.enterbookdetails(firstpagecontroller.java:39) sun.reflect.nativemethodaccessorimpl.invoke0(native method) sun.reflect.nativemethodaccessorimpl.invoke(unknown source) sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source)

it seems localsessionfactorybean not getting injected, cannot figure out reason. kind of help appreciated.

the npe has nil hibernate set-up. in controller create new book service using new bookservice() in getbookservice() method. incorrect. need define bean book service in spring configuration, inject bean controller (either straight or using annotation) , utilize bean in controller. defined bookservice property, there.

spring hibernate spring-mvc integration

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -