Store Enums in DB





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I was asked to make a a column called Type a varchar2(1)which has values partial or all



That what i made in Model.Java



    @Column(name="TYPE")
@Enumerated(EnumType.STRING)
public TypeEnum getType() {
return type;
}

public void setType(TypeEnum type) {
this.type = type;
}


And this is my TypeEnum.java



public enum TypeEnum {
ALL(0, "all"),
PARTIAL(1, "partial");

private int code;
private String value;

private TypeEnum(int code, String value) {
this.code = code;
this.value = value;
}

public String getValue() {
return value;
}

public int getCode() {
return code;
}

public static TypeEnum getTypeEnum(String value){
TypeEnum types = values();
for(int i=0; i<types.length; i++){
TypeEnum type = types[i];
if(value.equals(type.getValue()))
return type;
}
return null;
}

}


So how to store the TypeEnum in DB to achieve the varchar2(1)










share|improve this question































    0















    I was asked to make a a column called Type a varchar2(1)which has values partial or all



    That what i made in Model.Java



        @Column(name="TYPE")
    @Enumerated(EnumType.STRING)
    public TypeEnum getType() {
    return type;
    }

    public void setType(TypeEnum type) {
    this.type = type;
    }


    And this is my TypeEnum.java



    public enum TypeEnum {
    ALL(0, "all"),
    PARTIAL(1, "partial");

    private int code;
    private String value;

    private TypeEnum(int code, String value) {
    this.code = code;
    this.value = value;
    }

    public String getValue() {
    return value;
    }

    public int getCode() {
    return code;
    }

    public static TypeEnum getTypeEnum(String value){
    TypeEnum types = values();
    for(int i=0; i<types.length; i++){
    TypeEnum type = types[i];
    if(value.equals(type.getValue()))
    return type;
    }
    return null;
    }

    }


    So how to store the TypeEnum in DB to achieve the varchar2(1)










    share|improve this question



























      0












      0








      0








      I was asked to make a a column called Type a varchar2(1)which has values partial or all



      That what i made in Model.Java



          @Column(name="TYPE")
      @Enumerated(EnumType.STRING)
      public TypeEnum getType() {
      return type;
      }

      public void setType(TypeEnum type) {
      this.type = type;
      }


      And this is my TypeEnum.java



      public enum TypeEnum {
      ALL(0, "all"),
      PARTIAL(1, "partial");

      private int code;
      private String value;

      private TypeEnum(int code, String value) {
      this.code = code;
      this.value = value;
      }

      public String getValue() {
      return value;
      }

      public int getCode() {
      return code;
      }

      public static TypeEnum getTypeEnum(String value){
      TypeEnum types = values();
      for(int i=0; i<types.length; i++){
      TypeEnum type = types[i];
      if(value.equals(type.getValue()))
      return type;
      }
      return null;
      }

      }


      So how to store the TypeEnum in DB to achieve the varchar2(1)










      share|improve this question
















      I was asked to make a a column called Type a varchar2(1)which has values partial or all



      That what i made in Model.Java



          @Column(name="TYPE")
      @Enumerated(EnumType.STRING)
      public TypeEnum getType() {
      return type;
      }

      public void setType(TypeEnum type) {
      this.type = type;
      }


      And this is my TypeEnum.java



      public enum TypeEnum {
      ALL(0, "all"),
      PARTIAL(1, "partial");

      private int code;
      private String value;

      private TypeEnum(int code, String value) {
      this.code = code;
      this.value = value;
      }

      public String getValue() {
      return value;
      }

      public int getCode() {
      return code;
      }

      public static TypeEnum getTypeEnum(String value){
      TypeEnum types = values();
      for(int i=0; i<types.length; i++){
      TypeEnum type = types[i];
      if(value.equals(type.getValue()))
      return type;
      }
      return null;
      }

      }


      So how to store the TypeEnum in DB to achieve the varchar2(1)







      java database jpa






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 16:53









      K.Nicholas

      5,36432542




      5,36432542










      asked Nov 23 '18 at 16:25









      Tito TitoTito Tito

      13




      13
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You could use a Converter to map your enums to varchar(1) yourself. Something like:



          @Column("TYPE") @Convert(TypeEnumToString.class) TypeEnum type;


          with the converter class implemented something like:



          public class TypeEnumToString implements AttributeConverter<TypeEnum, String> {
          @Override
          public TypeEnum convertToEntityAttribute(String value) {
          // return conversion;
          }
          @Override
          public String convertToDatabaseColumn(TypeEnum value) {
          // return conversion;
          }
          }





          share|improve this answer































            0














            You can achieve this by implementing AttributeConverter<TypeEnum, String>



            @Converter
            public class TypeEnumConverter implements AttributeConverter<TypeEnum, String> {

            @Override
            public String convertToDatabaseColumn(TypeEnum attribute) {
            return String.valueOf(attribute.getCode());
            }

            @Override
            public TypeEnum convertToEntityAttribute(String dbData) {
            return getTypeEnumFromCode(parseInt(dbData));
            }
            }


            getTypeEnumFromCode can be implemented similar to your getTypeEnum method.



            Then, define it like



            @Column("TYPE")
            @Convert(TypeEnumToString.class)
            TypeEnum type;


            p.s.I just used code from your enum but it can be any other logic too.






            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%2f53450081%2fstore-enums-in-db%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









              0














              You could use a Converter to map your enums to varchar(1) yourself. Something like:



              @Column("TYPE") @Convert(TypeEnumToString.class) TypeEnum type;


              with the converter class implemented something like:



              public class TypeEnumToString implements AttributeConverter<TypeEnum, String> {
              @Override
              public TypeEnum convertToEntityAttribute(String value) {
              // return conversion;
              }
              @Override
              public String convertToDatabaseColumn(TypeEnum value) {
              // return conversion;
              }
              }





              share|improve this answer




























                0














                You could use a Converter to map your enums to varchar(1) yourself. Something like:



                @Column("TYPE") @Convert(TypeEnumToString.class) TypeEnum type;


                with the converter class implemented something like:



                public class TypeEnumToString implements AttributeConverter<TypeEnum, String> {
                @Override
                public TypeEnum convertToEntityAttribute(String value) {
                // return conversion;
                }
                @Override
                public String convertToDatabaseColumn(TypeEnum value) {
                // return conversion;
                }
                }





                share|improve this answer


























                  0












                  0








                  0







                  You could use a Converter to map your enums to varchar(1) yourself. Something like:



                  @Column("TYPE") @Convert(TypeEnumToString.class) TypeEnum type;


                  with the converter class implemented something like:



                  public class TypeEnumToString implements AttributeConverter<TypeEnum, String> {
                  @Override
                  public TypeEnum convertToEntityAttribute(String value) {
                  // return conversion;
                  }
                  @Override
                  public String convertToDatabaseColumn(TypeEnum value) {
                  // return conversion;
                  }
                  }





                  share|improve this answer













                  You could use a Converter to map your enums to varchar(1) yourself. Something like:



                  @Column("TYPE") @Convert(TypeEnumToString.class) TypeEnum type;


                  with the converter class implemented something like:



                  public class TypeEnumToString implements AttributeConverter<TypeEnum, String> {
                  @Override
                  public TypeEnum convertToEntityAttribute(String value) {
                  // return conversion;
                  }
                  @Override
                  public String convertToDatabaseColumn(TypeEnum value) {
                  // return conversion;
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 17:13









                  moilejtermoilejter

                  64947




                  64947

























                      0














                      You can achieve this by implementing AttributeConverter<TypeEnum, String>



                      @Converter
                      public class TypeEnumConverter implements AttributeConverter<TypeEnum, String> {

                      @Override
                      public String convertToDatabaseColumn(TypeEnum attribute) {
                      return String.valueOf(attribute.getCode());
                      }

                      @Override
                      public TypeEnum convertToEntityAttribute(String dbData) {
                      return getTypeEnumFromCode(parseInt(dbData));
                      }
                      }


                      getTypeEnumFromCode can be implemented similar to your getTypeEnum method.



                      Then, define it like



                      @Column("TYPE")
                      @Convert(TypeEnumToString.class)
                      TypeEnum type;


                      p.s.I just used code from your enum but it can be any other logic too.






                      share|improve this answer




























                        0














                        You can achieve this by implementing AttributeConverter<TypeEnum, String>



                        @Converter
                        public class TypeEnumConverter implements AttributeConverter<TypeEnum, String> {

                        @Override
                        public String convertToDatabaseColumn(TypeEnum attribute) {
                        return String.valueOf(attribute.getCode());
                        }

                        @Override
                        public TypeEnum convertToEntityAttribute(String dbData) {
                        return getTypeEnumFromCode(parseInt(dbData));
                        }
                        }


                        getTypeEnumFromCode can be implemented similar to your getTypeEnum method.



                        Then, define it like



                        @Column("TYPE")
                        @Convert(TypeEnumToString.class)
                        TypeEnum type;


                        p.s.I just used code from your enum but it can be any other logic too.






                        share|improve this answer


























                          0












                          0








                          0







                          You can achieve this by implementing AttributeConverter<TypeEnum, String>



                          @Converter
                          public class TypeEnumConverter implements AttributeConverter<TypeEnum, String> {

                          @Override
                          public String convertToDatabaseColumn(TypeEnum attribute) {
                          return String.valueOf(attribute.getCode());
                          }

                          @Override
                          public TypeEnum convertToEntityAttribute(String dbData) {
                          return getTypeEnumFromCode(parseInt(dbData));
                          }
                          }


                          getTypeEnumFromCode can be implemented similar to your getTypeEnum method.



                          Then, define it like



                          @Column("TYPE")
                          @Convert(TypeEnumToString.class)
                          TypeEnum type;


                          p.s.I just used code from your enum but it can be any other logic too.






                          share|improve this answer













                          You can achieve this by implementing AttributeConverter<TypeEnum, String>



                          @Converter
                          public class TypeEnumConverter implements AttributeConverter<TypeEnum, String> {

                          @Override
                          public String convertToDatabaseColumn(TypeEnum attribute) {
                          return String.valueOf(attribute.getCode());
                          }

                          @Override
                          public TypeEnum convertToEntityAttribute(String dbData) {
                          return getTypeEnumFromCode(parseInt(dbData));
                          }
                          }


                          getTypeEnumFromCode can be implemented similar to your getTypeEnum method.



                          Then, define it like



                          @Column("TYPE")
                          @Convert(TypeEnumToString.class)
                          TypeEnum type;


                          p.s.I just used code from your enum but it can be any other logic too.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 23 '18 at 17:21









                          Sukhpal SinghSukhpal Singh

                          1,049212




                          1,049212






























                              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%2f53450081%2fstore-enums-in-db%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