2009-06-22 89 views
1

好日子,休眠集映射到「SQL SELECT」表

我有一個Hibernate映射它是這樣的

<class name="Person"> 
    <id name="id" type="long" column="person_id" unsaved-value="null"> 
     <generator class="sequence"> 
      <param name="sequence">person_id_seq</param> 
     </generator> 
    </id> 
    ... 
    <set name="thinCollection" table="(select person_id, person_property from some_other_table where another_property = 'something')" fetch="subselect" lazy="false"> 
    <key column="person_id"/> 
    <element column="person_property" type="long"/> 
    </set> 
    ... 
</class> 

現在我的問題是,當一個人對象被刷新,它嘗試執行鍼對Person#thinCollection的集合刪除操作,該操作因嘗試執行delete from (select person_id, person_property from some_other_table where another_property = 'something')而失敗。

因此,我該如何阻止Hibernate執行這些操作(以及更新和插入)?

感謝

+0

我很驚訝,hibernate讓你變得更遠......是甚至是支持的配置? – skaffman 2009-06-22 19:27:19

+0

顯然,它是。 ..直到你得到實體的沖洗,然後你會遇到我剛剛發佈的內容。 – 2009-06-22 20:13:14

回答

0

我目前所做的解決這個問題是創建我自己的persister(這是一個BasicCollectionPersister的子類),它永遠不會插入/更新/刪除。

但我不確定這是否是最好的方法去做這個,或者如果可以只是添加一個神奇的映射屬性,以防止插入/更新/刪除。

[編輯]

我發現很難對我設置映射到一個子查詢,所以我用我自己的自定義BasicCollectionPersister代替。我重寫了#isRowDeleteEnabled()和#isRowInsertEnabled()都始終返回false。並且我重寫#doUpdateRows(..)總是返回0.

0

豈不

cascade="none" 

執行trick? [編輯] Oups,認爲這是NHibernate:P嗯,我希望它仍然會工作:)