hello, I encountered an common error with spring + hibernate, but cant fix it :(
i have 2 tables with one-to-many relation: each Bsc have many Cell
my hibernate config
<class name="Bsc" table="bsc">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="name" />
<set name="cells">
<key column="bsc_id" />
<one-to-many class="Cell" />
</set>
</class>
<class name="Cell" table="cell">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="name" column="name" />
<many-to-one name="bsc" column="bsc_id" not-null="true" />
</class>
my dao
public class BscDaoImpl extends HibernateDaoSupport implements BscDao {
@Override
public Bsc get(int id) {
return (Bsc) getHibernateTemplate().get(Bsc.class, id);
}
@Override
public void save(Bsc bsc) {
getHibernateTemplate().saveOrUpdate(bsc);
}
}
my controller
public class BscFormController extends SimpleFormController {
private BscDao bscDao;
public void setBscDao(BscDao bscDao) {
this.bscDao = bscDao;
}
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
String id = request.getParameter("id");
if (!StringUtils.isBlank(id)) {
return bscDao.get(new Integer(id));
}
return new Bsc();
}
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
Bsc bsc = (Bsc) command;
String success = getSuccessView();
bscDao.save(bsc);
return new ModelAndView(success);
}
}
in my view, i have a form to create/edit bsc info. In addition, i want list all cells of this bsc, so i config lazy load in web.xml
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>mySessionFactory</param-value>
</init-param>
</filter>
my problem: i cant save object when press Save button (it's save ok if i remove OpenSessionInViewFilter config).
thanks
Quan
i have added TransactionManager config
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
but it still dont work. I miss something ?