hi,
Im new to hibernate, and im trying to add hibernate persistance to my spring app. My spring app is just a simple form with 5 or so fields which i want to store in a table. Im getting the following error and my code is below that. Also if you see any other errors, especially in that way i have configured my app, please mention. Thanks
Code:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'photoinfoForm' defined in ServletContext resource [/WEB-INF/photomapper-servlet.xml]: Cannot resolve reference to bean 'photoMan' while setting bean property 'photoManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'photoMan' defined in ServletContext resource [/WEB-INF/photomapper-servlet.xml]: Cannot resolve reference to bean 'photoManDao' while setting bean property 'photoManagerDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'photoManDao' defined in ServletContext resource [/WEB-INF/photomapper-servlet.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: net/sf/hibernate/Session
the form bean Photo
Code:
package src.bus;
import java.io.Serializable;
import java.util.Date;
public class Photo implements Serializable {
private String location; //where photo was taken
private String photographer; //map to username
private String datetaken;
private String description;
public String getDatetaken() {
return datetaken;
}
public void setDatetaken(String datetaken) {
this.datetaken = datetaken;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getPhotographer() {
return photographer;
}
public void setPhotographer(String photographer) {
this.photographer = photographer;
}
}
my controller
Code:
package src.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;
import src.bus.Photo;
import src.bus.PhotoManager;
public class PhotoinfoController extends SimpleFormController{
private PhotoManager pm = new PhotoManager();
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors) {
Photo bean = (Photo)command;
pm.setPhoto(bean);
// write collected form data to the database table
//return super.onSubmit(request, response, command, errors);
return new ModelAndView(new RedirectView(getSuccessView()));
}
}
my hibernate doa and doa interface
Code:
import java.util.List;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import org.hibernate.Query;
import org.hibernate.Session;
import src.bus.Photo;
public class PhotoinfoDaoHib extends HibernateDaoSupport implements PhotoinfoDao {
public Photo getPhotoinfo(int id) {
Session session = HibernateUtil.currentSession();
session.beginTransaction();
Query query = session.createQuery("from Photoinfo pi where pi.photoinfoId = :id");
query.setInteger("id", id);
HibernateUtil.closeSession();
return (Photo) query.uniqueResult();
}
public List getPhotoinfoList() {
// TODO Auto-generated method stub
return null;
}
public void addPhotoinfo(Photo p) {
getHibernateTemplate().saveOrUpdate(p);
}
}
--
package src.db;
import java.util.List;
import src.bus.Photo;
public interface PhotoinfoDao {
public Photo getPhotoinfo(int id);
public List getPhotoinfoList();
public void addPhotoinfo(Photo p);
}
hibernate.cfg.xml
Code:
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>photoinfo.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean>
my dispatcher servlet: photomapper-servlet
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for "photomapper" DispatcherServlet.
-->
<beans>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/photoinfo.htm">photoinfoForm</prop>
</props>
</property>
</bean>
<bean id="photoinfoValidator"
class="src.validator.PhotoinfoValidator" />
<bean id="photoinfoForm"
class="src.controller.PhotoinfoController">
<property name="sessionForm">
<value>true</value>
</property>
<property name="commandName">
<value>photo</value>
</property>
<property name="commandClass">
<value>src.bus.Photo</value>
</property>
<property name="validator">
<ref bean="photoinfoValidator" />
</property>
<property name="formView">
<value>photoinfo</value>
</property>
<property name="successView">
<value>successful</value>
</property>
<property name="photoManager">
<ref bean="photoMan"/>
</property>
</bean>
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="photoManDao" class="src.db.PhotoinfoDaoHib">
<property name="dataSource">
<ref bean="myDataSource"/>
</property>
</bean>
<bean id="photoMan" class="src.bus.PhotoManager">
<property name="photoManagerDao">
<ref bean="photoManDao"/>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
web.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
<servlet>
<servlet-name>photomapper</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>photomapper</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/spring</taglib-uri>
<taglib-location>/WEB-INF/spring.tld</taglib-location>
</taglib>
</web-app>
photoinfo.hbm.xml
Code:
<hibernate-mapping>
<class name="Photo" table="Photoinfo">
<id name="photoinfoId" column="photoinfoId"
type="java.lang.Integer" unsaved-value="-1">
<generator class="native"></generator>
</id>
<property name="location" column="location" type="string" />
<property name="photographer" column="photographer"
type="string" />
<property name="datetaken" column="datetaken" type="string" />
<property name="description" column="description" type="string" />
</class>
</hibernate-mapping>