tags:

views:

272

answers:

3

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

Thanks

+1  A: 

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

http://docs.jboss.org/hibernate/stable/core/reference/en/html/mapping.html

McWafflestix
Thanks, but how do I map the results of my class-level subselect statement with my property?
Franz See
A: 

Wouldn't

cascade="none"

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

cwap
I tried cascade="none" and it does not work. I think it only applies to the entities of the Collection and not to the Collection itself.
Franz See
A: 

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.

[EDIT]

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.

Franz See