Discriminator column with formula is not inlcuded in insert statment of an Entity in NHibernate.

334 views
Skip to first unread message

Suman

unread,
Mar 22, 2011, 4:20:50 AM3/22/11
to nhusers
My problem is exactly same as described in following post but it
hasn't been answered and it's 4 years old post!!!

https://forum.hibernate.org/viewtopic.php?f=1&t=976655

In summary, I have table per class hierarchy inheritance and I am
using formula in discriminator column. When I try to insert new record
(Entity) the insert fails because the generated sql doesn't include
the discriminator column.

Alternatively I could define another property that maps to the column
and make discriminator's "insert" attribute to false. However I don't
like this approach as it will force me to almost rewrite what formula
was there for in the domain object.

I would really appreciate if anyone else has fixed this problem some
better way.

<class name="Account" table="ACCOUNT" abstract ="true">

<discriminator column ="ACCOUNT_TY" type ="string" formula="(case when
ACCOUNT_TY in ('CASHCARD','ORPHAN','BLACK','EXTERNAL', 'INTERNAL')
then 'OTHER' else ACCOUNT_TY end)" />


<subclass name ="CreditCardAccount" extends ="Account" discriminator-
value="PREPAY" dynamic-update="true" >
<subclass name ="OtherAccount" extends ="Account" discriminator-
value="OTHER" dynamic-update="true">
<subclass name ="PostpayAccount" extends ="Account" discriminator-
value="POSTPAY">

ggeurts

unread,
Mar 22, 2011, 6:08:06 PM3/22/11
to nhusers
Why not add the formula as calculated default to the table definition
or use a trigger? Formula columns are considered read-only. If you
want a calculated value inserted/updated it is time to build it into
your db creation scripts.

Regards,
Gerke.

Fabio Maulo

unread,
Mar 22, 2011, 6:19:52 PM3/22/11
to nhu...@googlegroups.com
The example in our tests is 1632 days old (more than 4 years).
Have a look in NHibernate.Test.NHSpecificTest.NH335


--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.




--
Fabio Maulo

Suman

unread,
Mar 22, 2011, 9:50:11 PM3/22/11
to nhusers
Hi GGeurts and Fabio,

Thanks heaps. Yeah, I resorted to property with private setter and a
protected method on the base class, which is called from sub class
constructor with appropriate discriminator.
It's not the exact way I wanted but it works now.

Thanks heaps.

Fabio Maulo

unread,
Mar 22, 2011, 10:17:25 PM3/22/11
to nhu...@googlegroups.com
Formula in Discriminator is only to give support to legacy not ORM DB.
The best way is refactorize the DB instead fight with it.

--
Fabio Maulo

Reply all
Reply to author
Forward
0 new messages