views:

264

answers:

4

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 ?

+1  A: 

Did you configure a TransactionManager in your Spring application context ? By the way, if you're on a JDK5+, you'd better use annotations (you can decrease your code size by a 2 factor)

Chris
QuanNH
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 ?
QuanNH
A: 

Did anybody solve this problem? I've got the same.

Wojciech
A: 

my current config

web.xml

<!-- lazyLoad-->
<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>sessionFactoryBeanName</param-name>
        <param-value>mySessionFactory</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

spring config

    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory" />
</bean>

<aop:config>
    <aop:advisor pointcut="execution(* vn.com.vhc.cbs.dao.*.*(..))"
        advice-ref="txAdvice" />
</aop:config>

<tx:advice id="txAdvice">
    <tx:attributes>
        <tx:method name="save*" />
        <tx:method name="update*" />
        <tx:method name="remove*" />
        <tx:method name="*" read-only="true" />
    </tx:attributes>
</tx:advice>

hope this help

QuanNH
A: 

Thanks it helped. Is it possible to configure it somehow with annotations?

Wojciech