How to map custom enumerated integer ordinals with hibernate?












12















I have an enum class named Status as follows



public enum Status {
PENDING(0), SUCCESS(1), FAILED(-1);

private int st;

private Status(int st){
this.st = st;
}
}


and from other class I try to map this status enum



public void setStatus(Status status) {
this.status = status;
}

@Enumerated(EnumType.ORDINAL)
public Status getStatus() {
return status;
}


when I run this code, I get




java.lang.IllegalArgumentException: Unknown ordinal value for enum class data.Status: -1
at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:93)
at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:124)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:106)
at




but I already have -1 in enum definition.










share|improve this question





























    12















    I have an enum class named Status as follows



    public enum Status {
    PENDING(0), SUCCESS(1), FAILED(-1);

    private int st;

    private Status(int st){
    this.st = st;
    }
    }


    and from other class I try to map this status enum



    public void setStatus(Status status) {
    this.status = status;
    }

    @Enumerated(EnumType.ORDINAL)
    public Status getStatus() {
    return status;
    }


    when I run this code, I get




    java.lang.IllegalArgumentException: Unknown ordinal value for enum class data.Status: -1
    at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:93)
    at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:124)
    at org.hibernate.type.AbstractType.hydrate(AbstractType.java:106)
    at




    but I already have -1 in enum definition.










    share|improve this question



























      12












      12








      12


      4






      I have an enum class named Status as follows



      public enum Status {
      PENDING(0), SUCCESS(1), FAILED(-1);

      private int st;

      private Status(int st){
      this.st = st;
      }
      }


      and from other class I try to map this status enum



      public void setStatus(Status status) {
      this.status = status;
      }

      @Enumerated(EnumType.ORDINAL)
      public Status getStatus() {
      return status;
      }


      when I run this code, I get




      java.lang.IllegalArgumentException: Unknown ordinal value for enum class data.Status: -1
      at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:93)
      at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:124)
      at org.hibernate.type.AbstractType.hydrate(AbstractType.java:106)
      at




      but I already have -1 in enum definition.










      share|improve this question
















      I have an enum class named Status as follows



      public enum Status {
      PENDING(0), SUCCESS(1), FAILED(-1);

      private int st;

      private Status(int st){
      this.st = st;
      }
      }


      and from other class I try to map this status enum



      public void setStatus(Status status) {
      this.status = status;
      }

      @Enumerated(EnumType.ORDINAL)
      public Status getStatus() {
      return status;
      }


      when I run this code, I get




      java.lang.IllegalArgumentException: Unknown ordinal value for enum class data.Status: -1
      at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:93)
      at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:124)
      at org.hibernate.type.AbstractType.hydrate(AbstractType.java:106)
      at




      but I already have -1 in enum definition.







      java hibernate exception enumeration






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 10:50









      Ortomala Lokni

      23.3k782129




      23.3k782129










      asked Jul 16 '12 at 11:11









      user1479203user1479203

      2581516




      2581516
























          3 Answers
          3






          active

          oldest

          votes


















          10














          You could define your own UserType which defines how Hibernate should map those enums.



          Note that the ordinal defines the index of the enum value and thus FAILED would have the ordinal 2. To map the enum using its properties your need a UserType implementation.



          Some links:




          • https://community.jboss.org/wiki/UserTypeForPersistingAnEnumWithAVARCHARColumn


          • http://javadata.blogspot.de/2011/07/hibernate-and-enum-handling.html (look at the "Paramterized Enumeration in Hibernate" section)






          share|improve this answer



















          • 2





            Use the below link for a good tutorial. It helped me to understand clearly. gabiaxel.com/2011/01/better-enum-mapping-with-hibernate.html

            – Yadu Krishnan
            Jun 18 '14 at 5:08



















          8














          Here is a solution where a string label is used instead of an int id, however it is simple to adapt.



          public class User { 
          @Id
          private int id;

          @Type(type = "com.example.hibernate.LabeledEnumType")
          private Role role;
          }


          public enum Role implements LabeledEnum {
          ADMIN("admin"), USER("user"), ANONYMOUS("anon");

          private final String label;

          Role(String label) {
          this.label = label;
          }

          @Override
          public String getLabel() {
          return label;
          }
          }


          public interface LabeledEnum {
          String getLabel();
          }


          public final class LabeledEnumType implements DynamicParameterizedType, UserType {

          private Class<? extends Enum> enumClass;

          @Override
          public Object assemble(Serializable cached, Object owner)
          throws HibernateException {
          return cached;
          }

          @Override
          public Object deepCopy(Object value) throws HibernateException {
          return value;
          }

          @Override
          public Serializable disassemble(Object value) throws HibernateException {
          return (Serializable) value;
          }

          @Override
          public boolean equals(Object x, Object y) throws HibernateException {
          return x == y;
          }

          @Override
          public int hashCode(Object x) throws HibernateException {
          return x == null ? 0 : x.hashCode();
          }

          @Override
          public boolean isMutable() {
          return false;
          }

          @Override
          public Object nullSafeGet(ResultSet rs, String names, SessionImplementor session, Object owner)
          throws HibernateException, SQLException {
          String label = rs.getString(names[0]);
          if (rs.wasNull()) {
          return null;
          }
          for (Enum value : returnedClass().getEnumConstants()) {
          if (value instanceof LabeledEnum) {
          LabeledEnum labeledEnum = (LabeledEnum) value;
          if (labeledEnum.getLabel().equals(label)) {
          return value;
          }
          }
          }
          throw new IllegalStateException("Unknown " + returnedClass().getSimpleName() + " label");
          }

          @Override
          public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
          throws HibernateException, SQLException {
          if (value == null) {
          st.setNull(index, Types.VARCHAR);
          } else {
          st.setString(index, ((LabeledEnum) value).getLabel());
          }
          }

          @Override
          public Object replace(Object original, Object target, Object owner)
          throws HibernateException {
          return original;
          }

          @Override
          public Class<? extends Enum> returnedClass() {
          return enumClass;
          }

          @Override
          public int sqlTypes() {
          return new int{Types.VARCHAR};
          }

          @Override
          public void setParameterValues(Properties parameters) {
          ParameterType params = (ParameterType) parameters.get( PARAMETER_TYPE );
          enumClass = params.getReturnedClass();
          }
          }





          share|improve this answer
























          • I did the same. But it giving me column "user_role_type" is of type users.user_role but expression is of type bytea

            – Abhishek
            Jul 6 '18 at 9:42



















          1














          I would like to suggest following workaround. At first I was supprised it worked but it is really simple:
          For enum:



              public enum Status {
          PENDING(0), SUCCESS(1), FAILED(-1);

          private int status;

          private Status(int status){
          this.status = status;
          }

          public String getStatus() {
          return status;
          }

          public static Status parse(int id) {
          Status status = null; // Default
          for (Status item : Status.values()) {
          if (item.getStatus().equals(id)) {
          Status = item;
          break;
          }
          }
          return Status;
          }
          }


          class



          class StatedObject{

          @Column("status")
          private int statusInt;

          public Status getStatus() {
          return Status.parse(statusString);
          }

          public void setStatus(Status paymentStatus) {
          this.statusInt = paymentStatus.getStatus();
          }

          public String getStatusInt() {
          return statusString;
          }

          public void setStatusInt(int statusInt) {
          this.statusInt = statusInt;
          }
          }


          if you are using hibernate in hibernate xml file it would be:



           <property name="statusInt "    column="status" type="java.lang.Integer" />


          that is it






          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%2f11503046%2fhow-to-map-custom-enumerated-integer-ordinals-with-hibernate%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            10














            You could define your own UserType which defines how Hibernate should map those enums.



            Note that the ordinal defines the index of the enum value and thus FAILED would have the ordinal 2. To map the enum using its properties your need a UserType implementation.



            Some links:




            • https://community.jboss.org/wiki/UserTypeForPersistingAnEnumWithAVARCHARColumn


            • http://javadata.blogspot.de/2011/07/hibernate-and-enum-handling.html (look at the "Paramterized Enumeration in Hibernate" section)






            share|improve this answer



















            • 2





              Use the below link for a good tutorial. It helped me to understand clearly. gabiaxel.com/2011/01/better-enum-mapping-with-hibernate.html

              – Yadu Krishnan
              Jun 18 '14 at 5:08
















            10














            You could define your own UserType which defines how Hibernate should map those enums.



            Note that the ordinal defines the index of the enum value and thus FAILED would have the ordinal 2. To map the enum using its properties your need a UserType implementation.



            Some links:




            • https://community.jboss.org/wiki/UserTypeForPersistingAnEnumWithAVARCHARColumn


            • http://javadata.blogspot.de/2011/07/hibernate-and-enum-handling.html (look at the "Paramterized Enumeration in Hibernate" section)






            share|improve this answer



















            • 2





              Use the below link for a good tutorial. It helped me to understand clearly. gabiaxel.com/2011/01/better-enum-mapping-with-hibernate.html

              – Yadu Krishnan
              Jun 18 '14 at 5:08














            10












            10








            10







            You could define your own UserType which defines how Hibernate should map those enums.



            Note that the ordinal defines the index of the enum value and thus FAILED would have the ordinal 2. To map the enum using its properties your need a UserType implementation.



            Some links:




            • https://community.jboss.org/wiki/UserTypeForPersistingAnEnumWithAVARCHARColumn


            • http://javadata.blogspot.de/2011/07/hibernate-and-enum-handling.html (look at the "Paramterized Enumeration in Hibernate" section)






            share|improve this answer













            You could define your own UserType which defines how Hibernate should map those enums.



            Note that the ordinal defines the index of the enum value and thus FAILED would have the ordinal 2. To map the enum using its properties your need a UserType implementation.



            Some links:




            • https://community.jboss.org/wiki/UserTypeForPersistingAnEnumWithAVARCHARColumn


            • http://javadata.blogspot.de/2011/07/hibernate-and-enum-handling.html (look at the "Paramterized Enumeration in Hibernate" section)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 16 '12 at 11:31









            ThomasThomas

            69.9k1091125




            69.9k1091125








            • 2





              Use the below link for a good tutorial. It helped me to understand clearly. gabiaxel.com/2011/01/better-enum-mapping-with-hibernate.html

              – Yadu Krishnan
              Jun 18 '14 at 5:08














            • 2





              Use the below link for a good tutorial. It helped me to understand clearly. gabiaxel.com/2011/01/better-enum-mapping-with-hibernate.html

              – Yadu Krishnan
              Jun 18 '14 at 5:08








            2




            2





            Use the below link for a good tutorial. It helped me to understand clearly. gabiaxel.com/2011/01/better-enum-mapping-with-hibernate.html

            – Yadu Krishnan
            Jun 18 '14 at 5:08





            Use the below link for a good tutorial. It helped me to understand clearly. gabiaxel.com/2011/01/better-enum-mapping-with-hibernate.html

            – Yadu Krishnan
            Jun 18 '14 at 5:08













            8














            Here is a solution where a string label is used instead of an int id, however it is simple to adapt.



            public class User { 
            @Id
            private int id;

            @Type(type = "com.example.hibernate.LabeledEnumType")
            private Role role;
            }


            public enum Role implements LabeledEnum {
            ADMIN("admin"), USER("user"), ANONYMOUS("anon");

            private final String label;

            Role(String label) {
            this.label = label;
            }

            @Override
            public String getLabel() {
            return label;
            }
            }


            public interface LabeledEnum {
            String getLabel();
            }


            public final class LabeledEnumType implements DynamicParameterizedType, UserType {

            private Class<? extends Enum> enumClass;

            @Override
            public Object assemble(Serializable cached, Object owner)
            throws HibernateException {
            return cached;
            }

            @Override
            public Object deepCopy(Object value) throws HibernateException {
            return value;
            }

            @Override
            public Serializable disassemble(Object value) throws HibernateException {
            return (Serializable) value;
            }

            @Override
            public boolean equals(Object x, Object y) throws HibernateException {
            return x == y;
            }

            @Override
            public int hashCode(Object x) throws HibernateException {
            return x == null ? 0 : x.hashCode();
            }

            @Override
            public boolean isMutable() {
            return false;
            }

            @Override
            public Object nullSafeGet(ResultSet rs, String names, SessionImplementor session, Object owner)
            throws HibernateException, SQLException {
            String label = rs.getString(names[0]);
            if (rs.wasNull()) {
            return null;
            }
            for (Enum value : returnedClass().getEnumConstants()) {
            if (value instanceof LabeledEnum) {
            LabeledEnum labeledEnum = (LabeledEnum) value;
            if (labeledEnum.getLabel().equals(label)) {
            return value;
            }
            }
            }
            throw new IllegalStateException("Unknown " + returnedClass().getSimpleName() + " label");
            }

            @Override
            public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
            throws HibernateException, SQLException {
            if (value == null) {
            st.setNull(index, Types.VARCHAR);
            } else {
            st.setString(index, ((LabeledEnum) value).getLabel());
            }
            }

            @Override
            public Object replace(Object original, Object target, Object owner)
            throws HibernateException {
            return original;
            }

            @Override
            public Class<? extends Enum> returnedClass() {
            return enumClass;
            }

            @Override
            public int sqlTypes() {
            return new int{Types.VARCHAR};
            }

            @Override
            public void setParameterValues(Properties parameters) {
            ParameterType params = (ParameterType) parameters.get( PARAMETER_TYPE );
            enumClass = params.getReturnedClass();
            }
            }





            share|improve this answer
























            • I did the same. But it giving me column "user_role_type" is of type users.user_role but expression is of type bytea

              – Abhishek
              Jul 6 '18 at 9:42
















            8














            Here is a solution where a string label is used instead of an int id, however it is simple to adapt.



            public class User { 
            @Id
            private int id;

            @Type(type = "com.example.hibernate.LabeledEnumType")
            private Role role;
            }


            public enum Role implements LabeledEnum {
            ADMIN("admin"), USER("user"), ANONYMOUS("anon");

            private final String label;

            Role(String label) {
            this.label = label;
            }

            @Override
            public String getLabel() {
            return label;
            }
            }


            public interface LabeledEnum {
            String getLabel();
            }


            public final class LabeledEnumType implements DynamicParameterizedType, UserType {

            private Class<? extends Enum> enumClass;

            @Override
            public Object assemble(Serializable cached, Object owner)
            throws HibernateException {
            return cached;
            }

            @Override
            public Object deepCopy(Object value) throws HibernateException {
            return value;
            }

            @Override
            public Serializable disassemble(Object value) throws HibernateException {
            return (Serializable) value;
            }

            @Override
            public boolean equals(Object x, Object y) throws HibernateException {
            return x == y;
            }

            @Override
            public int hashCode(Object x) throws HibernateException {
            return x == null ? 0 : x.hashCode();
            }

            @Override
            public boolean isMutable() {
            return false;
            }

            @Override
            public Object nullSafeGet(ResultSet rs, String names, SessionImplementor session, Object owner)
            throws HibernateException, SQLException {
            String label = rs.getString(names[0]);
            if (rs.wasNull()) {
            return null;
            }
            for (Enum value : returnedClass().getEnumConstants()) {
            if (value instanceof LabeledEnum) {
            LabeledEnum labeledEnum = (LabeledEnum) value;
            if (labeledEnum.getLabel().equals(label)) {
            return value;
            }
            }
            }
            throw new IllegalStateException("Unknown " + returnedClass().getSimpleName() + " label");
            }

            @Override
            public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
            throws HibernateException, SQLException {
            if (value == null) {
            st.setNull(index, Types.VARCHAR);
            } else {
            st.setString(index, ((LabeledEnum) value).getLabel());
            }
            }

            @Override
            public Object replace(Object original, Object target, Object owner)
            throws HibernateException {
            return original;
            }

            @Override
            public Class<? extends Enum> returnedClass() {
            return enumClass;
            }

            @Override
            public int sqlTypes() {
            return new int{Types.VARCHAR};
            }

            @Override
            public void setParameterValues(Properties parameters) {
            ParameterType params = (ParameterType) parameters.get( PARAMETER_TYPE );
            enumClass = params.getReturnedClass();
            }
            }





            share|improve this answer
























            • I did the same. But it giving me column "user_role_type" is of type users.user_role but expression is of type bytea

              – Abhishek
              Jul 6 '18 at 9:42














            8












            8








            8







            Here is a solution where a string label is used instead of an int id, however it is simple to adapt.



            public class User { 
            @Id
            private int id;

            @Type(type = "com.example.hibernate.LabeledEnumType")
            private Role role;
            }


            public enum Role implements LabeledEnum {
            ADMIN("admin"), USER("user"), ANONYMOUS("anon");

            private final String label;

            Role(String label) {
            this.label = label;
            }

            @Override
            public String getLabel() {
            return label;
            }
            }


            public interface LabeledEnum {
            String getLabel();
            }


            public final class LabeledEnumType implements DynamicParameterizedType, UserType {

            private Class<? extends Enum> enumClass;

            @Override
            public Object assemble(Serializable cached, Object owner)
            throws HibernateException {
            return cached;
            }

            @Override
            public Object deepCopy(Object value) throws HibernateException {
            return value;
            }

            @Override
            public Serializable disassemble(Object value) throws HibernateException {
            return (Serializable) value;
            }

            @Override
            public boolean equals(Object x, Object y) throws HibernateException {
            return x == y;
            }

            @Override
            public int hashCode(Object x) throws HibernateException {
            return x == null ? 0 : x.hashCode();
            }

            @Override
            public boolean isMutable() {
            return false;
            }

            @Override
            public Object nullSafeGet(ResultSet rs, String names, SessionImplementor session, Object owner)
            throws HibernateException, SQLException {
            String label = rs.getString(names[0]);
            if (rs.wasNull()) {
            return null;
            }
            for (Enum value : returnedClass().getEnumConstants()) {
            if (value instanceof LabeledEnum) {
            LabeledEnum labeledEnum = (LabeledEnum) value;
            if (labeledEnum.getLabel().equals(label)) {
            return value;
            }
            }
            }
            throw new IllegalStateException("Unknown " + returnedClass().getSimpleName() + " label");
            }

            @Override
            public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
            throws HibernateException, SQLException {
            if (value == null) {
            st.setNull(index, Types.VARCHAR);
            } else {
            st.setString(index, ((LabeledEnum) value).getLabel());
            }
            }

            @Override
            public Object replace(Object original, Object target, Object owner)
            throws HibernateException {
            return original;
            }

            @Override
            public Class<? extends Enum> returnedClass() {
            return enumClass;
            }

            @Override
            public int sqlTypes() {
            return new int{Types.VARCHAR};
            }

            @Override
            public void setParameterValues(Properties parameters) {
            ParameterType params = (ParameterType) parameters.get( PARAMETER_TYPE );
            enumClass = params.getReturnedClass();
            }
            }





            share|improve this answer













            Here is a solution where a string label is used instead of an int id, however it is simple to adapt.



            public class User { 
            @Id
            private int id;

            @Type(type = "com.example.hibernate.LabeledEnumType")
            private Role role;
            }


            public enum Role implements LabeledEnum {
            ADMIN("admin"), USER("user"), ANONYMOUS("anon");

            private final String label;

            Role(String label) {
            this.label = label;
            }

            @Override
            public String getLabel() {
            return label;
            }
            }


            public interface LabeledEnum {
            String getLabel();
            }


            public final class LabeledEnumType implements DynamicParameterizedType, UserType {

            private Class<? extends Enum> enumClass;

            @Override
            public Object assemble(Serializable cached, Object owner)
            throws HibernateException {
            return cached;
            }

            @Override
            public Object deepCopy(Object value) throws HibernateException {
            return value;
            }

            @Override
            public Serializable disassemble(Object value) throws HibernateException {
            return (Serializable) value;
            }

            @Override
            public boolean equals(Object x, Object y) throws HibernateException {
            return x == y;
            }

            @Override
            public int hashCode(Object x) throws HibernateException {
            return x == null ? 0 : x.hashCode();
            }

            @Override
            public boolean isMutable() {
            return false;
            }

            @Override
            public Object nullSafeGet(ResultSet rs, String names, SessionImplementor session, Object owner)
            throws HibernateException, SQLException {
            String label = rs.getString(names[0]);
            if (rs.wasNull()) {
            return null;
            }
            for (Enum value : returnedClass().getEnumConstants()) {
            if (value instanceof LabeledEnum) {
            LabeledEnum labeledEnum = (LabeledEnum) value;
            if (labeledEnum.getLabel().equals(label)) {
            return value;
            }
            }
            }
            throw new IllegalStateException("Unknown " + returnedClass().getSimpleName() + " label");
            }

            @Override
            public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
            throws HibernateException, SQLException {
            if (value == null) {
            st.setNull(index, Types.VARCHAR);
            } else {
            st.setString(index, ((LabeledEnum) value).getLabel());
            }
            }

            @Override
            public Object replace(Object original, Object target, Object owner)
            throws HibernateException {
            return original;
            }

            @Override
            public Class<? extends Enum> returnedClass() {
            return enumClass;
            }

            @Override
            public int sqlTypes() {
            return new int{Types.VARCHAR};
            }

            @Override
            public void setParameterValues(Properties parameters) {
            ParameterType params = (ParameterType) parameters.get( PARAMETER_TYPE );
            enumClass = params.getReturnedClass();
            }
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 3 '15 at 9:33









            user3099799user3099799

            11615




            11615













            • I did the same. But it giving me column "user_role_type" is of type users.user_role but expression is of type bytea

              – Abhishek
              Jul 6 '18 at 9:42



















            • I did the same. But it giving me column "user_role_type" is of type users.user_role but expression is of type bytea

              – Abhishek
              Jul 6 '18 at 9:42

















            I did the same. But it giving me column "user_role_type" is of type users.user_role but expression is of type bytea

            – Abhishek
            Jul 6 '18 at 9:42





            I did the same. But it giving me column "user_role_type" is of type users.user_role but expression is of type bytea

            – Abhishek
            Jul 6 '18 at 9:42











            1














            I would like to suggest following workaround. At first I was supprised it worked but it is really simple:
            For enum:



                public enum Status {
            PENDING(0), SUCCESS(1), FAILED(-1);

            private int status;

            private Status(int status){
            this.status = status;
            }

            public String getStatus() {
            return status;
            }

            public static Status parse(int id) {
            Status status = null; // Default
            for (Status item : Status.values()) {
            if (item.getStatus().equals(id)) {
            Status = item;
            break;
            }
            }
            return Status;
            }
            }


            class



            class StatedObject{

            @Column("status")
            private int statusInt;

            public Status getStatus() {
            return Status.parse(statusString);
            }

            public void setStatus(Status paymentStatus) {
            this.statusInt = paymentStatus.getStatus();
            }

            public String getStatusInt() {
            return statusString;
            }

            public void setStatusInt(int statusInt) {
            this.statusInt = statusInt;
            }
            }


            if you are using hibernate in hibernate xml file it would be:



             <property name="statusInt "    column="status" type="java.lang.Integer" />


            that is it






            share|improve this answer




























              1














              I would like to suggest following workaround. At first I was supprised it worked but it is really simple:
              For enum:



                  public enum Status {
              PENDING(0), SUCCESS(1), FAILED(-1);

              private int status;

              private Status(int status){
              this.status = status;
              }

              public String getStatus() {
              return status;
              }

              public static Status parse(int id) {
              Status status = null; // Default
              for (Status item : Status.values()) {
              if (item.getStatus().equals(id)) {
              Status = item;
              break;
              }
              }
              return Status;
              }
              }


              class



              class StatedObject{

              @Column("status")
              private int statusInt;

              public Status getStatus() {
              return Status.parse(statusString);
              }

              public void setStatus(Status paymentStatus) {
              this.statusInt = paymentStatus.getStatus();
              }

              public String getStatusInt() {
              return statusString;
              }

              public void setStatusInt(int statusInt) {
              this.statusInt = statusInt;
              }
              }


              if you are using hibernate in hibernate xml file it would be:



               <property name="statusInt "    column="status" type="java.lang.Integer" />


              that is it






              share|improve this answer


























                1












                1








                1







                I would like to suggest following workaround. At first I was supprised it worked but it is really simple:
                For enum:



                    public enum Status {
                PENDING(0), SUCCESS(1), FAILED(-1);

                private int status;

                private Status(int status){
                this.status = status;
                }

                public String getStatus() {
                return status;
                }

                public static Status parse(int id) {
                Status status = null; // Default
                for (Status item : Status.values()) {
                if (item.getStatus().equals(id)) {
                Status = item;
                break;
                }
                }
                return Status;
                }
                }


                class



                class StatedObject{

                @Column("status")
                private int statusInt;

                public Status getStatus() {
                return Status.parse(statusString);
                }

                public void setStatus(Status paymentStatus) {
                this.statusInt = paymentStatus.getStatus();
                }

                public String getStatusInt() {
                return statusString;
                }

                public void setStatusInt(int statusInt) {
                this.statusInt = statusInt;
                }
                }


                if you are using hibernate in hibernate xml file it would be:



                 <property name="statusInt "    column="status" type="java.lang.Integer" />


                that is it






                share|improve this answer













                I would like to suggest following workaround. At first I was supprised it worked but it is really simple:
                For enum:



                    public enum Status {
                PENDING(0), SUCCESS(1), FAILED(-1);

                private int status;

                private Status(int status){
                this.status = status;
                }

                public String getStatus() {
                return status;
                }

                public static Status parse(int id) {
                Status status = null; // Default
                for (Status item : Status.values()) {
                if (item.getStatus().equals(id)) {
                Status = item;
                break;
                }
                }
                return Status;
                }
                }


                class



                class StatedObject{

                @Column("status")
                private int statusInt;

                public Status getStatus() {
                return Status.parse(statusString);
                }

                public void setStatus(Status paymentStatus) {
                this.statusInt = paymentStatus.getStatus();
                }

                public String getStatusInt() {
                return statusString;
                }

                public void setStatusInt(int statusInt) {
                this.statusInt = statusInt;
                }
                }


                if you are using hibernate in hibernate xml file it would be:



                 <property name="statusInt "    column="status" type="java.lang.Integer" />


                that is it







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 14 '16 at 14:44









                Dariusz DudzińskiDariusz Dudziński

                976




                976






























                    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%2f11503046%2fhow-to-map-custom-enumerated-integer-ordinals-with-hibernate%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

                    android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                    SQL update select statement

                    'app-layout' is not a known element: how to share Component with different Modules