-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Should <subclass> honor <sql-insert>?
PostPosted: Wed Apr 18, 2007 11:51 am 
Newbie

Joined: Tue Apr 10, 2007 11:12 am
Posts: 2
Using NHibernate 1.2.0CR1 I am trying to use the table per class hierarchy inheritance mapping strategy. Because of our database structure I need to use a view to pull all of the necessary pieces together, which is why I placed the view name in for the table name.
I am able to retrieve data from the database but when I attempt to save new PropertyValue instances I get the following SQL exception:
Code:
View or function 'VW_PROPERTYVALUE' is not updatable because the modification affects multiple base tables.


I've tried moving the <sql-insert> out of the subclass mapping and into the base class mapping with no effect. Should the <sql-insert> be honored by the <class> or <subclass> elements? Why is NHibernate trying to insert into the view?

Here is my mapping for this particular class:
Code:
  <class name="PropertyValue" table="VW_PROPERTYVALUE">
    <id name="Id" column="PROPERTYVALUE_PK" type="long" unsaved-value="-1">
      <generator class="native">
        <param name="sequence">SEQ_PK</param>
      </generator>
    </id>
    <discriminator column="CLRTYPE" type="String" />

    <many-to-one name="PropertyDefinition" class="PropertyDefinition" column="PROPERTYDEF_FK" lazy="false" />

    <any name="Entity" meta-type="long" id-type="long">
      <meta-value class="User" value="1" />
      <column name="ENTITYTYPE_FK" />
      <column name="ENTITY_FK" />
    </any>

    <subclass name="IntegerPropertyValue" extends="PropertyValue" discriminator-value="System.Int32">
      <property name="Data" column="IntValue" type="int" />
      <sql-insert>insert into TBL_PROPERTYVALUE (PROPERTYDEF_FK, ENTITY_FK, IntValue) VALUES (?, ?, ?)</sql-insert>
    </subclass>
    <subclass name="DateTimePropertyValue" discriminator-value="System.DateTime">
      <property name="Data" column="DateTimeValue" type="DateTime" />
    </subclass>
    <subclass name="StringPropertyValue" discriminator-value="System.String">
      <property name="Data" column="StringValue" type="string" />
    </subclass>
  </class>


Can anyone help?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 3:39 am 
Newbie

Joined: Sat Sep 03, 2005 4:37 pm
Posts: 2
I'm afraid your problem has nothing to do with hibernate.
It seems that you just cannot perform update/insert queries on your view.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 6:08 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Not sure what DB you're using, but SQL Server does not allow an update to a view where the view spans multiple tables if the updated fields in the update statement includes fields that are from more than one underlying table.

HTH,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 7:50 pm 
Contributor
Contributor

Joined: Sun Jun 26, 2005 5:03 am
Posts: 51
Location: London, UK
One trick you could pull here is to use an INSTEAD OF trigger on the view to partition the various bits into the appropriate tables.

I'm doing this on a current project as a way of generating a complete version history of an entity and it works fine.

_________________
Paul Hatcher
NHibernate Team


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 4:54 pm 
Newbie

Joined: Tue Apr 10, 2007 11:12 am
Posts: 2
I am fully aware that you can't insert back into a view that spans multiple tables... I'm not that much of a newbie at this stuff.

I have since tabled the portion of my project that my original post related to. The schema design was bad to start with, making the use of NHibernate (or any other ORM) with this particular product feature difficult if not impossible.

I know that I cannot insert back into a view that spans multiple tables. What I am curious to know is why NHibernate doesn't recognize the <sql-insert> and <sql-update> elements in my class and sub-class mappings. No matter what I did with these elements NHibernate would always generate its own SQL for inserts and updates. Is there really no way to override NHibernate's default behavior in this situation?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.