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

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



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.

Leave a Reply

Your email address will not be published. Required fields are marked *