How to tweak NamingStrategy for Spring Data JDBC












1















how do i tweak the Spring Data JDBC NamingStrategy to behave like Hibernate´s PhysicalNamingStrategy?

I have the following entity:



/**
* Campus domain model class.
* Handles information about campus.
*
* @author thomas.lang@th-deg.de
*/
@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
public class Campus {

private final @Id
@Wither
long campusId;

@NotNull
@Size(min = 3)
private String campusName;

/**
* Creates a new campus.
*
* @param campusName
*/
public Campus(@NonNull String campusName) {
this.campusId = 0;
Assert.hasLength(campusName, "A valid value has to be provided for Campus!");
this.campusName = campusName;
}

}


When i want JDBC to persist a test record, it generates this sql:
[INSERT INTO campus (campus_name) VALUES (?)]



My existing Database has a column named campusname



I have read on the docu here https://docs.spring.io/spring-data/jdbc/docs/1.0.2.RELEASE/reference/html/#jdbc.entity-persistence.naming-strategy that i can tweak the naming strategy, but i don´t know how :)



Thank you for your help!
Kind regards!
Thomas



Solution:



/**
* Naming strategy for naming entity columns
* @see <a href="https://stackoverflow.com/questions/53334685/how-to-tweak-namingstrategy-for-spring-data-jdbc/53335830#53335830">How to implement {@link NamingStrategy}</a>
*
* @return PhysicalNamingStrategy
*/
@Bean
public NamingStrategy namingStrategy() {
return new NamingStrategy() {
@Override
public String getColumnName(RelationalPersistentProperty property) {
Assert.notNull(property, "Property must not be null.");
return ParsingUtils.reconcatenateCamelCase(property.getName(), "");
}
};
}









share|improve this question





























    1















    how do i tweak the Spring Data JDBC NamingStrategy to behave like Hibernate´s PhysicalNamingStrategy?

    I have the following entity:



    /**
    * Campus domain model class.
    * Handles information about campus.
    *
    * @author thomas.lang@th-deg.de
    */
    @Data
    @AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
    public class Campus {

    private final @Id
    @Wither
    long campusId;

    @NotNull
    @Size(min = 3)
    private String campusName;

    /**
    * Creates a new campus.
    *
    * @param campusName
    */
    public Campus(@NonNull String campusName) {
    this.campusId = 0;
    Assert.hasLength(campusName, "A valid value has to be provided for Campus!");
    this.campusName = campusName;
    }

    }


    When i want JDBC to persist a test record, it generates this sql:
    [INSERT INTO campus (campus_name) VALUES (?)]



    My existing Database has a column named campusname



    I have read on the docu here https://docs.spring.io/spring-data/jdbc/docs/1.0.2.RELEASE/reference/html/#jdbc.entity-persistence.naming-strategy that i can tweak the naming strategy, but i don´t know how :)



    Thank you for your help!
    Kind regards!
    Thomas



    Solution:



    /**
    * Naming strategy for naming entity columns
    * @see <a href="https://stackoverflow.com/questions/53334685/how-to-tweak-namingstrategy-for-spring-data-jdbc/53335830#53335830">How to implement {@link NamingStrategy}</a>
    *
    * @return PhysicalNamingStrategy
    */
    @Bean
    public NamingStrategy namingStrategy() {
    return new NamingStrategy() {
    @Override
    public String getColumnName(RelationalPersistentProperty property) {
    Assert.notNull(property, "Property must not be null.");
    return ParsingUtils.reconcatenateCamelCase(property.getName(), "");
    }
    };
    }









    share|improve this question



























      1












      1








      1








      how do i tweak the Spring Data JDBC NamingStrategy to behave like Hibernate´s PhysicalNamingStrategy?

      I have the following entity:



      /**
      * Campus domain model class.
      * Handles information about campus.
      *
      * @author thomas.lang@th-deg.de
      */
      @Data
      @AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
      public class Campus {

      private final @Id
      @Wither
      long campusId;

      @NotNull
      @Size(min = 3)
      private String campusName;

      /**
      * Creates a new campus.
      *
      * @param campusName
      */
      public Campus(@NonNull String campusName) {
      this.campusId = 0;
      Assert.hasLength(campusName, "A valid value has to be provided for Campus!");
      this.campusName = campusName;
      }

      }


      When i want JDBC to persist a test record, it generates this sql:
      [INSERT INTO campus (campus_name) VALUES (?)]



      My existing Database has a column named campusname



      I have read on the docu here https://docs.spring.io/spring-data/jdbc/docs/1.0.2.RELEASE/reference/html/#jdbc.entity-persistence.naming-strategy that i can tweak the naming strategy, but i don´t know how :)



      Thank you for your help!
      Kind regards!
      Thomas



      Solution:



      /**
      * Naming strategy for naming entity columns
      * @see <a href="https://stackoverflow.com/questions/53334685/how-to-tweak-namingstrategy-for-spring-data-jdbc/53335830#53335830">How to implement {@link NamingStrategy}</a>
      *
      * @return PhysicalNamingStrategy
      */
      @Bean
      public NamingStrategy namingStrategy() {
      return new NamingStrategy() {
      @Override
      public String getColumnName(RelationalPersistentProperty property) {
      Assert.notNull(property, "Property must not be null.");
      return ParsingUtils.reconcatenateCamelCase(property.getName(), "");
      }
      };
      }









      share|improve this question
















      how do i tweak the Spring Data JDBC NamingStrategy to behave like Hibernate´s PhysicalNamingStrategy?

      I have the following entity:



      /**
      * Campus domain model class.
      * Handles information about campus.
      *
      * @author thomas.lang@th-deg.de
      */
      @Data
      @AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
      public class Campus {

      private final @Id
      @Wither
      long campusId;

      @NotNull
      @Size(min = 3)
      private String campusName;

      /**
      * Creates a new campus.
      *
      * @param campusName
      */
      public Campus(@NonNull String campusName) {
      this.campusId = 0;
      Assert.hasLength(campusName, "A valid value has to be provided for Campus!");
      this.campusName = campusName;
      }

      }


      When i want JDBC to persist a test record, it generates this sql:
      [INSERT INTO campus (campus_name) VALUES (?)]



      My existing Database has a column named campusname



      I have read on the docu here https://docs.spring.io/spring-data/jdbc/docs/1.0.2.RELEASE/reference/html/#jdbc.entity-persistence.naming-strategy that i can tweak the naming strategy, but i don´t know how :)



      Thank you for your help!
      Kind regards!
      Thomas



      Solution:



      /**
      * Naming strategy for naming entity columns
      * @see <a href="https://stackoverflow.com/questions/53334685/how-to-tweak-namingstrategy-for-spring-data-jdbc/53335830#53335830">How to implement {@link NamingStrategy}</a>
      *
      * @return PhysicalNamingStrategy
      */
      @Bean
      public NamingStrategy namingStrategy() {
      return new NamingStrategy() {
      @Override
      public String getColumnName(RelationalPersistentProperty property) {
      Assert.notNull(property, "Property must not be null.");
      return ParsingUtils.reconcatenateCamelCase(property.getName(), "");
      }
      };
      }






      spring-data spring-data-jdbc






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 10:34







      Thomas Lang

















      asked Nov 16 '18 at 9:14









      Thomas LangThomas Lang

      12717




      12717
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Create a bean of type NamingStrategy in your application context and implement its methods to your liking.



          The method that you need to override is getColumnName(RelationalPersistentProperty)






          share|improve this answer
























          • Thank you Jens. Your hint was helpful. I ended up with the Bean above. Just for the record - please see the solution above on the question ...

            – Thomas Lang
            Nov 16 '18 at 10:33













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334685%2fhow-to-tweak-namingstrategy-for-spring-data-jdbc%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Create a bean of type NamingStrategy in your application context and implement its methods to your liking.



          The method that you need to override is getColumnName(RelationalPersistentProperty)






          share|improve this answer
























          • Thank you Jens. Your hint was helpful. I ended up with the Bean above. Just for the record - please see the solution above on the question ...

            – Thomas Lang
            Nov 16 '18 at 10:33


















          1














          Create a bean of type NamingStrategy in your application context and implement its methods to your liking.



          The method that you need to override is getColumnName(RelationalPersistentProperty)






          share|improve this answer
























          • Thank you Jens. Your hint was helpful. I ended up with the Bean above. Just for the record - please see the solution above on the question ...

            – Thomas Lang
            Nov 16 '18 at 10:33
















          1












          1








          1







          Create a bean of type NamingStrategy in your application context and implement its methods to your liking.



          The method that you need to override is getColumnName(RelationalPersistentProperty)






          share|improve this answer













          Create a bean of type NamingStrategy in your application context and implement its methods to your liking.



          The method that you need to override is getColumnName(RelationalPersistentProperty)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 10:22









          Jens SchauderJens Schauder

          46.1k17113238




          46.1k17113238













          • Thank you Jens. Your hint was helpful. I ended up with the Bean above. Just for the record - please see the solution above on the question ...

            – Thomas Lang
            Nov 16 '18 at 10:33





















          • Thank you Jens. Your hint was helpful. I ended up with the Bean above. Just for the record - please see the solution above on the question ...

            – Thomas Lang
            Nov 16 '18 at 10:33



















          Thank you Jens. Your hint was helpful. I ended up with the Bean above. Just for the record - please see the solution above on the question ...

          – Thomas Lang
          Nov 16 '18 at 10:33







          Thank you Jens. Your hint was helpful. I ended up with the Bean above. Just for the record - please see the solution above on the question ...

          – Thomas Lang
          Nov 16 '18 at 10:33




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334685%2fhow-to-tweak-namingstrategy-for-spring-data-jdbc%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini