can we use same hibernate annotated pojo class for multiple databases?










3















I have a JAVA POJO class with hibernate annotation for postgresql database.



Now, I have a requirement that we support multiple databases in our application. My question is : Should we use the same class with other databases (Oracle, MySQL, SQL Server) or should I write separate annotated class for each different database ?



Reason: To support special characters we are using database proprietary types instead of hibernate types like



// for oracle
@Column(sql-type="nvarchar2")
private String name;

// for sql server
@Column(sql-type="nvarchar")
private String name;

// hibernate doesn't support different proprietary sql types at same type like this
@Column(sql-type="nvarchar","nvarchar2")
private String name;









share|improve this question
























  • Which java package and hibernate version is this @Column annotation from?

    – Selaron
    Nov 15 '18 at 13:48











  • javax.persistence.Column and hibernate version 5.0.2

    – Ahmad
    Nov 15 '18 at 13:50











  • I did not find an annotation that provides an sql-type attribute. Did you mean Column#columnDefinition attribute instead? docs.oracle.com/javaee/7/api/javax/persistence/…

    – Selaron
    Nov 15 '18 at 14:33












  • if you question is answered, please mark it answerd.

    – Selaron
    Jan 15 at 10:20















3















I have a JAVA POJO class with hibernate annotation for postgresql database.



Now, I have a requirement that we support multiple databases in our application. My question is : Should we use the same class with other databases (Oracle, MySQL, SQL Server) or should I write separate annotated class for each different database ?



Reason: To support special characters we are using database proprietary types instead of hibernate types like



// for oracle
@Column(sql-type="nvarchar2")
private String name;

// for sql server
@Column(sql-type="nvarchar")
private String name;

// hibernate doesn't support different proprietary sql types at same type like this
@Column(sql-type="nvarchar","nvarchar2")
private String name;









share|improve this question
























  • Which java package and hibernate version is this @Column annotation from?

    – Selaron
    Nov 15 '18 at 13:48











  • javax.persistence.Column and hibernate version 5.0.2

    – Ahmad
    Nov 15 '18 at 13:50











  • I did not find an annotation that provides an sql-type attribute. Did you mean Column#columnDefinition attribute instead? docs.oracle.com/javaee/7/api/javax/persistence/…

    – Selaron
    Nov 15 '18 at 14:33












  • if you question is answered, please mark it answerd.

    – Selaron
    Jan 15 at 10:20













3












3








3


1






I have a JAVA POJO class with hibernate annotation for postgresql database.



Now, I have a requirement that we support multiple databases in our application. My question is : Should we use the same class with other databases (Oracle, MySQL, SQL Server) or should I write separate annotated class for each different database ?



Reason: To support special characters we are using database proprietary types instead of hibernate types like



// for oracle
@Column(sql-type="nvarchar2")
private String name;

// for sql server
@Column(sql-type="nvarchar")
private String name;

// hibernate doesn't support different proprietary sql types at same type like this
@Column(sql-type="nvarchar","nvarchar2")
private String name;









share|improve this question
















I have a JAVA POJO class with hibernate annotation for postgresql database.



Now, I have a requirement that we support multiple databases in our application. My question is : Should we use the same class with other databases (Oracle, MySQL, SQL Server) or should I write separate annotated class for each different database ?



Reason: To support special characters we are using database proprietary types instead of hibernate types like



// for oracle
@Column(sql-type="nvarchar2")
private String name;

// for sql server
@Column(sql-type="nvarchar")
private String name;

// hibernate doesn't support different proprietary sql types at same type like this
@Column(sql-type="nvarchar","nvarchar2")
private String name;






java hibernate pojo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 13:50









Selaron

2,48611323




2,48611323










asked Nov 15 '18 at 12:52









AhmadAhmad

7381827




7381827












  • Which java package and hibernate version is this @Column annotation from?

    – Selaron
    Nov 15 '18 at 13:48











  • javax.persistence.Column and hibernate version 5.0.2

    – Ahmad
    Nov 15 '18 at 13:50











  • I did not find an annotation that provides an sql-type attribute. Did you mean Column#columnDefinition attribute instead? docs.oracle.com/javaee/7/api/javax/persistence/…

    – Selaron
    Nov 15 '18 at 14:33












  • if you question is answered, please mark it answerd.

    – Selaron
    Jan 15 at 10:20

















  • Which java package and hibernate version is this @Column annotation from?

    – Selaron
    Nov 15 '18 at 13:48











  • javax.persistence.Column and hibernate version 5.0.2

    – Ahmad
    Nov 15 '18 at 13:50











  • I did not find an annotation that provides an sql-type attribute. Did you mean Column#columnDefinition attribute instead? docs.oracle.com/javaee/7/api/javax/persistence/…

    – Selaron
    Nov 15 '18 at 14:33












  • if you question is answered, please mark it answerd.

    – Selaron
    Jan 15 at 10:20
















Which java package and hibernate version is this @Column annotation from?

– Selaron
Nov 15 '18 at 13:48





Which java package and hibernate version is this @Column annotation from?

– Selaron
Nov 15 '18 at 13:48













javax.persistence.Column and hibernate version 5.0.2

– Ahmad
Nov 15 '18 at 13:50





javax.persistence.Column and hibernate version 5.0.2

– Ahmad
Nov 15 '18 at 13:50













I did not find an annotation that provides an sql-type attribute. Did you mean Column#columnDefinition attribute instead? docs.oracle.com/javaee/7/api/javax/persistence/…

– Selaron
Nov 15 '18 at 14:33






I did not find an annotation that provides an sql-type attribute. Did you mean Column#columnDefinition attribute instead? docs.oracle.com/javaee/7/api/javax/persistence/…

– Selaron
Nov 15 '18 at 14:33














if you question is answered, please mark it answerd.

– Selaron
Jan 15 at 10:20





if you question is answered, please mark it answerd.

– Selaron
Jan 15 at 10:20












2 Answers
2






active

oldest

votes


















1














If it's ok to use nvarchar for all String typed columns by default, you could extend Oracle and MS SQLServer dialects and do something like this:



public class CustomOracleDialect extends org.hibernate.dialect.Oracle10gDialect 

@Override
protected void registerCharacterTypeMappings()
super.registerCharacterTypeMappings();
registerColumnType(Types.VARCHAR, "nvarchar2");





public class CustomSQLServerDialect extends org.hibernate.dialect.SQLServer2012Dialect

public CustomSQLServerDialect()
super();
registerColumnType(Types.VARCHAR, "nvarchar");




Then configure these dialects in dependence to the database type used.






share|improve this answer
































    0














    The same exact class WILL work with other DB engines, as far as Hibernate is concerned all it cares about is the dialect for the most part. However, some DB engines don't support the identity generation strategy for ID fields for example (from past experience). Depending on what DB engines you are required to work with you may have to get creative a bit but, for the most part, as long as you don't have any DB-engine-specific code in your entity classes, everything should work just fine. I for example switched a project from HSQLDB to SQLite and the only thing that did not work was the identity generation i mentioned earlier. If i were you i would experiment with different dialects and thoroughly test everything.



    EDIT



    Just saw your edit and that is definitely engine-specific code. In this case you might indeed need different entities to accommodate the specific data type you want to explicitly assign to each column.



    Good luck!






    share|improve this answer






















      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%2f53319915%2fcan-we-use-same-hibernate-annotated-pojo-class-for-multiple-databases%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      If it's ok to use nvarchar for all String typed columns by default, you could extend Oracle and MS SQLServer dialects and do something like this:



      public class CustomOracleDialect extends org.hibernate.dialect.Oracle10gDialect 

      @Override
      protected void registerCharacterTypeMappings()
      super.registerCharacterTypeMappings();
      registerColumnType(Types.VARCHAR, "nvarchar2");





      public class CustomSQLServerDialect extends org.hibernate.dialect.SQLServer2012Dialect

      public CustomSQLServerDialect()
      super();
      registerColumnType(Types.VARCHAR, "nvarchar");




      Then configure these dialects in dependence to the database type used.






      share|improve this answer





























        1














        If it's ok to use nvarchar for all String typed columns by default, you could extend Oracle and MS SQLServer dialects and do something like this:



        public class CustomOracleDialect extends org.hibernate.dialect.Oracle10gDialect 

        @Override
        protected void registerCharacterTypeMappings()
        super.registerCharacterTypeMappings();
        registerColumnType(Types.VARCHAR, "nvarchar2");





        public class CustomSQLServerDialect extends org.hibernate.dialect.SQLServer2012Dialect

        public CustomSQLServerDialect()
        super();
        registerColumnType(Types.VARCHAR, "nvarchar");




        Then configure these dialects in dependence to the database type used.






        share|improve this answer



























          1












          1








          1







          If it's ok to use nvarchar for all String typed columns by default, you could extend Oracle and MS SQLServer dialects and do something like this:



          public class CustomOracleDialect extends org.hibernate.dialect.Oracle10gDialect 

          @Override
          protected void registerCharacterTypeMappings()
          super.registerCharacterTypeMappings();
          registerColumnType(Types.VARCHAR, "nvarchar2");





          public class CustomSQLServerDialect extends org.hibernate.dialect.SQLServer2012Dialect

          public CustomSQLServerDialect()
          super();
          registerColumnType(Types.VARCHAR, "nvarchar");




          Then configure these dialects in dependence to the database type used.






          share|improve this answer















          If it's ok to use nvarchar for all String typed columns by default, you could extend Oracle and MS SQLServer dialects and do something like this:



          public class CustomOracleDialect extends org.hibernate.dialect.Oracle10gDialect 

          @Override
          protected void registerCharacterTypeMappings()
          super.registerCharacterTypeMappings();
          registerColumnType(Types.VARCHAR, "nvarchar2");





          public class CustomSQLServerDialect extends org.hibernate.dialect.SQLServer2012Dialect

          public CustomSQLServerDialect()
          super();
          registerColumnType(Types.VARCHAR, "nvarchar");




          Then configure these dialects in dependence to the database type used.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 '18 at 7:26

























          answered Nov 15 '18 at 14:05









          SelaronSelaron

          2,48611323




          2,48611323























              0














              The same exact class WILL work with other DB engines, as far as Hibernate is concerned all it cares about is the dialect for the most part. However, some DB engines don't support the identity generation strategy for ID fields for example (from past experience). Depending on what DB engines you are required to work with you may have to get creative a bit but, for the most part, as long as you don't have any DB-engine-specific code in your entity classes, everything should work just fine. I for example switched a project from HSQLDB to SQLite and the only thing that did not work was the identity generation i mentioned earlier. If i were you i would experiment with different dialects and thoroughly test everything.



              EDIT



              Just saw your edit and that is definitely engine-specific code. In this case you might indeed need different entities to accommodate the specific data type you want to explicitly assign to each column.



              Good luck!






              share|improve this answer



























                0














                The same exact class WILL work with other DB engines, as far as Hibernate is concerned all it cares about is the dialect for the most part. However, some DB engines don't support the identity generation strategy for ID fields for example (from past experience). Depending on what DB engines you are required to work with you may have to get creative a bit but, for the most part, as long as you don't have any DB-engine-specific code in your entity classes, everything should work just fine. I for example switched a project from HSQLDB to SQLite and the only thing that did not work was the identity generation i mentioned earlier. If i were you i would experiment with different dialects and thoroughly test everything.



                EDIT



                Just saw your edit and that is definitely engine-specific code. In this case you might indeed need different entities to accommodate the specific data type you want to explicitly assign to each column.



                Good luck!






                share|improve this answer

























                  0












                  0








                  0







                  The same exact class WILL work with other DB engines, as far as Hibernate is concerned all it cares about is the dialect for the most part. However, some DB engines don't support the identity generation strategy for ID fields for example (from past experience). Depending on what DB engines you are required to work with you may have to get creative a bit but, for the most part, as long as you don't have any DB-engine-specific code in your entity classes, everything should work just fine. I for example switched a project from HSQLDB to SQLite and the only thing that did not work was the identity generation i mentioned earlier. If i were you i would experiment with different dialects and thoroughly test everything.



                  EDIT



                  Just saw your edit and that is definitely engine-specific code. In this case you might indeed need different entities to accommodate the specific data type you want to explicitly assign to each column.



                  Good luck!






                  share|improve this answer













                  The same exact class WILL work with other DB engines, as far as Hibernate is concerned all it cares about is the dialect for the most part. However, some DB engines don't support the identity generation strategy for ID fields for example (from past experience). Depending on what DB engines you are required to work with you may have to get creative a bit but, for the most part, as long as you don't have any DB-engine-specific code in your entity classes, everything should work just fine. I for example switched a project from HSQLDB to SQLite and the only thing that did not work was the identity generation i mentioned earlier. If i were you i would experiment with different dialects and thoroughly test everything.



                  EDIT



                  Just saw your edit and that is definitely engine-specific code. In this case you might indeed need different entities to accommodate the specific data type you want to explicitly assign to each column.



                  Good luck!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 13:26









                  MartinMartin

                  145220




                  145220



























                      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%2f53319915%2fcan-we-use-same-hibernate-annotated-pojo-class-for-multiple-databases%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







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線