Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo


Enum type

  • hi,

    please somebody can tell me how i can save an enum type in database like postgresql ?

  • Javier Paniza
    Javier Paniza

    Hi Tsagué,

    by default enums are mapped to integers (or other numeric type), it also can mapped to varchar(or other String type) using @Enumerated. You can mapped to any type and value using @Type. Have a look at the Hibernate doc.

    Put here the definition of your enum, the definition of your column in db and the data you have in your database and how you want to map to Java.

    It's impossible that JPA, Hibernate or whatever other software figure out what do you want to do. JPA/Hibernate has a standard way to map types, if you don't want to use it, you have to say the way the map must be done.

    If you do an updateSchema against a cleared database Hiberante will create the table correctly for you.

    Help others in this forum as I help you.

  • Granjero Moe
    Granjero Moe

    I'll give you an example.
    In your entity you can type this:

        public enum Sex {FEMALE, MALE};
        @Column(length=50, nullable=true)
        private Sex sex;

    And Hibernate will create a varchar field called sex where will save 'MALE' or 'FEMALE'.

    You also can use this other way:
    public enum Sex {FEMALE, MALE};

    @Column(length=50, nullable=true)
    private Sex sex;

    Then, Hibernate will create the field sex as an integer and will save 0 referring to FEMALE and 1 for MALE.