{"id":11105,"date":"2024-06-17T14:13:53","date_gmt":"2024-06-17T19:13:53","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=11105"},"modified":"2024-08-06T14:26:03","modified_gmt":"2024-08-06T19:26:03","slug":"jpa-hibernate-naming-strategies","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=11105","title":{"rendered":"JPA\/Hibernate Naming Strategies"},"content":{"rendered":"\n<p>One of the challenges of inheriting support of systems and code is reverse engineering what exactly you&#8217;ve got. In this case, I have Java code that reads from a Postgresql table named calculation_config &amp; populates the information into a Redis cache. Except I could not find <em>any<\/em> text containing the string calculation_config. Started to wonder if grep was getting thrown off by line splits (although splitting a line in the <em>middle<\/em> of a table name is asking for future confusion), so was searching for sub-strings. <\/p>\n\n\n\n<p>Which got me to the code that performs the operation &#8212; but the table is absolutely named calculationConfig in the code. ?????<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.example.applicationmodel;\nimport lombok.Data;\n\nimport jakarta.persistence.*;\n\n@Entity \/\/ This tells Hibernate to make a table out of this class\n@Data \/\/ Lombok: adds getters and setters\n@Table(name = &quot;calculationConfig&quot;, schema = &quot;components&quot;)\npublic class CalculationInfo {\n    @Id\n    private int functionId;\n    private String dataCollectionGroup;\n    private String component;\n    private String metricInputs;\n    private String metricName;\n    private String functionDef;\n    private String resourceType;\n    private String metricDatatype;\n    private String deviceModel;\n    private String collectionSystem;\n    private int status;\n}\n<\/pre><\/div>\n\n\n<p>And today, I&#8217;ve learned about &#8220;naming strategies&#8221;. A mechanism used by the Hibernate ORM (Object-Relational Mapping) framework to map entities within Java code to table and column names. Other than obfuscation, why are we applying middleware principals to code?? Ostensibly because database naming &#8220;best practices&#8221; and code naming &#8220;best practices&#8221; vary. As an aside, I was taught the <em>best<\/em> naming best practice was one someone was likely to figure out with minimal confusion or research. Explicitly indicating the naming strategy might fit that requirement &#8212; ohh, here&#8217;s some strange name mapping thing in my code. Let me see what that means. <\/p>\n\n\n\n<p>By default, Hibernate uses <code>ImplicitNamingStrategy<\/code> and <code>PhysicalNamingStrategy<\/code> to map Java names to database names. The default <code>PhysicalNamingStrategyStandardImpl<\/code> converts camelCase to snake_case.<\/p>\n\n\n\n<p>So, for future reference &#8230; when I find table_name or field_name in my <em>database<\/em>, I should be grepping for tableName and fieldName in the code. That is &#8230; not super obvious. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the challenges of inheriting support of systems and code is reverse engineering what exactly you&#8217;ve got. In this case, I have Java code that reads from a Postgresql table named calculation_config &amp; populates the information into a Redis cache. Except I could not find any text containing the string calculation_config. Started to wonder &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1547],"tags":[2039,578,2038],"class_list":["post-11105","post","type-post","status-publish","format-standard","hentry","category-java","tag-hibernate","tag-java","tag-jpa"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/11105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11105"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/11105\/revisions"}],"predecessor-version":[{"id":11106,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/11105\/revisions\/11106"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}