Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1065873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:55:16+00:00 2026-05-16T19:55:16+00:00

I am using Spring HibernateTemplate, OpenSessionInViewFilter(actually I extended this class and created my own

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T19:55:16+00:00Added an answer on May 16, 2026 at 7:55 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Spring's declarative transactions (the @Transactional annotation) in aspectj mode. It works
Using Spring 3 I have two forms: adding Item and invalidating Item I have
I am using Spring Roo. There is table:table and table:column tags. How to display
I'm using Spring's ContextLoaderListener to initialise a web services client, but if the wsdl
I am using Spring (with Jackson) and jQuery to pass a form as an
I am using Spring 3.0. Can i read a property file from a localdrive
I'm developing webapp using spring 3 mvc and using annotation-based controllers. I want to
I'm using Spring 3.0 along with Spring Security. I've always used the following configuration:
Hi I am using Spring Roo to create a service to access the data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.