hibernate多对多关联和spring hibernatetemplate不起作用

发布于 2024-09-18 04:34:31 字数 2449 浏览 1 评论 0原文

我正在使用Spring HibernateTemplate、OpenSessionInViewFilter(实际上我扩展了这个类并创建了自己的类以切换到FLUSH.AUTO模式)和Mysql来实现hibernate多对多关联。但是,当我保存对象时,不会插入相应的多对多表的值。有人可以帮助我吗?谢谢。

映射 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>

这是另一个

<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>

当像这样保存时:

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);

现在menu1和menu2将被保存在MENU表中,但是MENU_CONTENT表没有任何反应; MENU_CONTENT 表没有主键字段,而是 MENU_ID 和 CONTENT_ID 一起为主键。我不知道是不是这个问题。请帮我。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美男兮 2024-09-25 04:34:31

我找到了解决方案。而不是使用 Spring HibernateTemplate。我将它包装在常规会话和事务中,如下所示。

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();

这是我的会话过滤器,它继承自 OpenSessionInViewFilter

public class SessionFilter extends OpenSessionInViewFilter {

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

}

有谁知道一种方法来处理这个问题,而无需自己编写会话管理?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文