2010-09-10 56 views
0

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

这里是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将被保存在我NU表,但是没有任何事情发生在MENU_CONTENT表中; MENU_CONTENT表没有主键字段,而是MENU_ID和CONTENT_ID是主键。我不知道这是否是问题。请帮帮我。谢谢。

+0

你可以验证哪种语言?我看到“nhibernate”(.Net)但不确定。 – 2010-09-10 07:21:54

+0

这是Java。我找到了解决方案。但我想知道是否有更好的解决方案。我会告诉你我做了什么。 – beku8 2010-09-10 09:17:33

回答

0

我找到了解决方案。而不是使用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; 
} 

}

有谁知道一种方法来处理这个也懒得写会话管理自己?