views:

83

answers:

1

I am using Spring HibernateTemplate, OpenSessionInViewFilter(actually I extended this class and created my own to switch to FLUSH.AUTO Mode) and Mysql for implementing hibernate many-to-many association. However when I save an object, corresponding many-to-many table's values are not inserted. Does anybody can help me? Thank you.

here is the mapping xml

<hibernate-mapping>
    <class name="com.intelli.epub.domain.Content" table="CONTENT">
        <id name="id" type="java.lang.Long">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="title" type="java.lang.String">
            <column name="TITLE" />
        </property>
        <property name="text" type="java.lang.String">
            <column name="TEXT" />
        </property>
        <many-to-one name="writer" class="com.intelli.epub.domain.User" fetch="join">
            <column name="WRITER" />
        </many-to-one>
        <property name="createdDate" type="java.util.Date">
            <column name="CREATEDDATE" />
        </property>
        <set name="menus" table="MENU_CONTENT" cascade="all">
            <key column="CONTENT_ID"></key>
            <many-to-many column="MENU_ID" class="com.intelli.epub.domain.Menu"/>
        </set>
    </class>
</hibernate-mapping>

another one:

<hibernate-mapping>
<class name="com.intelli.epub.domain.Menu" table="MENU">
    <id name="id" type="java.lang.Long">
        <column name="ID" />
        <generator class="native" />
    </id>
    <property name="text" type="java.lang.String">
        <column name="TEXT" />
    </property>
    <set name="contents" table="MENU_CONTENT" inverse="true">
        <key column="MENU_ID"></key>
        <many-to-many column="CONTENT_ID" class="com.intelli.epub.domain.Content"/>
    </set>
</class>

and when saving like this:

Content content = new Content();
content.setCreatedDate(new Date());
content.setWriter(some user here);
content.setText("some text here");

Menu menu1 = new Menu("menu1");
Menu menu2 = new Menu("menu2");

Set<Menu> menus = new HashSet();
menus.add(menu1);
menus.add(menu2);

content.setMenus(menus);        
contentDao.saveOrUpdate(content);

Now menu1 and menu2 would be saved in the MENU table, However nothing happens to MENU_CONTENT table; MENU_CONTENT table doesn't have a primary key field, instead MENU_ID and CONTENT_ID are primary key together. I don't know if it's the problem. Please help me. Thank you.

A: 

I found a solution. Instead of using Spring HibernateTemplate. I wrapped it in regular session and transaction like this.

Session session = contentDao.getHibernateTemplate().getSessionFactory().getCurrentSession();    

transaction = session.beginTransaction();

Content content = new Content();
content.setCreatedDate(new Date());
content.setWriter(some user here);
content.setText("some text here");

Menu menu1 = new Menu("menu1");
Menu menu2 = new Menu("menu2");

Set<Menu> menus = new HashSet();
menus.add(menu1);
menus.add(menu2);

content.setMenus(menus);

session.save(content);
transaction.commit(); 
session.close();

And here is my session filter which inherited from OpenSessionInViewFilter

public class SessionFilter extends OpenSessionInViewFilter {

protected Session getSession(SessionFactory sessionFactory)
    throws DataAccessResourceFailureException {
    Session session = super.getSession(sessionFactory);
    session.setFlushMode(FlushMode.AUTO);   
    return session;
}

}

Does anybody know a way to handle this without bothering to write session management myself?

gun-armod