2010-09-10 55 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; 
} 

}

有誰知道一種方法來處理這個也懶得寫會話管理自己?