Tutorial :Hibernate set mapped to a 'sql select' table


Good day,

I have a hibernate mapping which goes something like this

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

Now my problem is, when a Person object gets flushed, it tries to execute a Collection Remove Action against Person#thinCollection, which fails because it's trying to execute delete from (select person_id, person_property from some_other_table where another_property = 'something').

Thus in line with that, how do I stop Hibernate from executing such actions (as well as update and inserts) ?



I believe you want to use a subselect for your query, thus rendering it readonly.





Do the trick ? [EDIT] Oups, thought it was NHibernate :P Well, I hope it would still work :)


What I currently did to solve this is to create my own persister (which is a subclass of BasicCollectionPersister) which never does an insertion/update/deletion.

But I am not sure if this is the best way to go about this or if could simply just add a magic mapping attribute to prevent the insertion/update/deletion.


I found it hard to map my Set to a subselect so I used my own custom BasicCollectionPersister instead. I overriden #isRowDeleteEnabled() and #isRowInsertEnabled() to both always return false. And I overriden #doUpdateRows(..) to always return 0.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »