{"id":325,"date":"2009-12-02T14:16:31","date_gmt":"2009-12-02T12:16:31","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=325"},"modified":"2016-09-09T10:32:33","modified_gmt":"2016-09-09T08:32:33","slug":"many-to-one-with-non-unique-columns","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/many-to-one-with-non-unique-columns","title":{"rendered":"Many-to-one with non unique columns"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2009\/12\/6387611d.png\" alt=\"6387611d\" title=\"6387611d\" width=\"331\" height=\"69\" class=\"alignnone size-full\" \/><\/p>\n<p>Tables look as follows:<\/p>\n<p>User<br \/>\n+ID<br \/>\n+PERSON_ID<br \/>\n+PRIMARY_USER<\/p>\n<p>DeputyRole<br \/>\n+PERSON_ID<\/p>\n<p>Disclaimer: Yes I know the db schema is broken, but we can not do anything about it.<\/p>\n<p>Why didn&#8217;t we use standard propery-ref and column mapping but formula instead?<\/p>\n<ol>\n<li> USER.PERSON_ID is not unique in USER table.<\/li>\n<li> USER.PERSON_ID AND USER.PRIMARY_USER = &#8216;Y&#8217; is unique in USER table.<\/li>\n<li> We are using formula to get single record from USER (ID) with specified PERSON_ID and PRIMARY_USER = &#8216;Y&#8217; <\/li>\n<\/ol>\n<p>In DeputyRole.hbm.xml we have:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;many-to-one name=&quot;User&quot;\r\n        class=&quot;DomainModel.Users.Entities.User&quot;\r\n        lazy=&quot;false&quot;\r\n        formula = &quot;(SELECT u.ID FROM USER u WHERE\r\n                                u.PRIMARYUSERID = 'Y'\r\n                                AND u.PERSON_ID = PERSON_ID)&quot;\r\n\/&gt;\r\n<\/pre>\n<p>Can this be done in better way?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tables look as follows: User +ID +PERSON_ID +PRIMARY_USER DeputyRole +PERSON_ID Disclaimer: Yes I know the db schema is broken, but we can not do anything about it. Why didn&#8217;t we use standard propery-ref and column mapping but formula instead? USER.PERSON_ID is not unique in USER table. USER.PERSON_ID AND USER.PRIMARY_USER = &#8216;Y&#8217; is unique in USER [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[38],"class_list":["post-325","post","type-post","status-publish","format-standard","hentry","category-programming","tag-nhibernate"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/325"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=325"}],"version-history":[{"count":1,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/325\/revisions"}],"predecessor-version":[{"id":5060,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/325\/revisions\/5060"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}