{"id":653,"date":"2019-11-26T02:35:29","date_gmt":"2019-11-26T02:35:29","guid":{"rendered":"http:\/\/bruceng.com\/dev\/?p=653"},"modified":"2019-11-26T02:37:47","modified_gmt":"2019-11-26T02:37:47","slug":"hibernate-decimal-precision-is-wrong","status":"publish","type":"post","link":"https:\/\/bruceng.com\/dev\/?p=653","title":{"rendered":"Hibernate mapping for decimal scale is wrong"},"content":{"rendered":"\n<p>When mapping a SQL table to a model using Hibernate or NHibernate, if you configure the mapping like following<\/p>\n\n\n\n<p>&lt;property name=&#8221;MyDecCol&#8221; type=&#8221;Decimal&#8221; precision=&#8221;28&#8243; scale=&#8221;15&#8243;&gt;<br>&lt;column name=&#8221;MyDecCol&#8221; sql-type=&#8221;decimal&#8221; not-null=&#8221;false&#8221; \/&gt;<br>&lt;\/property&gt;<\/p>\n\n\n\n<p>For some reason, Hibernate\/NHibernate will treat the field as a decimal with scale = 5 (5 decimal digits on the right side of the decimal separator). So if you save a value to this column with more decimal digits, it will be truncated to only 5 decimal digits.<\/p>\n\n\n\n<p>I&#8217;m not sure why, but if you move the attributes to the inner column element, Hibernate and NHibernate will map the values correctly.<\/p>\n\n\n\n<p> &lt;property name=&#8221;MyDecCol&#8221; type=&#8221;Decimal&#8221;&gt;<br>&lt;column name=&#8221;MyDecCol&#8221; sql-type=&#8221;decimal&#8221; not-null=&#8221;false&#8221;  precision=&#8221;28&#8243; scale=&#8221;15&#8243;  \/&gt;<br>&lt;\/property&gt; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>When mapping a SQL table to a model using Hibernate or NHibernate, if you configure the mapping like following &lt;property name=&#8221;MyDecCol&#8221; type=&#8221;Decimal&#8221; precision=&#8221;28&#8243; scale=&#8221;15&#8243;&gt;&lt;column name=&#8221;MyDecCol&#8221; sql-type=&#8221;decimal&#8221; not-null=&#8221;false&#8221; \/&gt;&lt;\/property&gt; For some reason, Hibernate\/NHibernate will treat the field as a decimal with scale = 5 (5 decimal digits on the right side of the decimal separator). So &hellip; <a href=\"https:\/\/bruceng.com\/dev\/?p=653\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Hibernate mapping for decimal scale is wrong&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[96,76,30,10],"class_list":["post-653","post","type-post","status-publish","format-standard","hentry","category-problems","tag-hibernate","tag-nhibernate","tag-sql","tag-sql-server"],"_links":{"self":[{"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/posts\/653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=653"}],"version-history":[{"count":2,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/posts\/653\/revisions"}],"predecessor-version":[{"id":655,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/posts\/653\/revisions\/655"}],"wp:attachment":[{"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}