7 Replies Latest reply: Feb 15, 2013 4:12 AM by REDO LOG RSS

    Enumerations in JPA

    REDO LOG
      Hi all
      I have a question about enumerations,
      class Test{
      
         public enum Gender{
      
           Male, Female
         }
      
      @Enumerated(EnumType.STRING)
      @Column(length=1)
      private Gender gender;
      
      }
      my question is this correct?
      my goal is to keep only one character of the enumration

      thanks for help
        • 1. Re: Enumerations in JPA
          r035198x
          Just define your enums with one character to begin with. Use the enum constructor for descriptions if your application needs them. Something like
           public enum Gender{
                M("Male"), F("Female")
          
              private String description;
              private Gender(String description){
                   this.description = description;
              }
              public String getDescription() {
                  return description;
               }
          }
           
          • 2. Re: Enumerations in JPA
            gimbal2
            The answer to the original question is : DBMS and configuration specific. Some DBMSes can be configured to truncate values, like MySQL. Most will cause an insert/update statement to fail though.
            • 3. Re: Enumerations in JPA
              r035198x
              gimbal2 wrote:
              The answer to the original question is : DBMS and configuration specific. Some DBMSes can be configured to truncate values, like MySQL. Most will cause an insert/update statement to fail though.
              Even so I'm curious how providers handle enums with strings that start with same character when a db length =1 restriction is enforced. At least picking the letters yourself keeps things predictable and also allows you to use descriptions that don't have to be restricted to allowed enum names.
              • 4. Re: Enumerations in JPA
                REDO LOG
                Hi guys,
                here is what I did according to your suggestions
                package com.entities;
                
                import com.enumeration.Gender;
                import java.io.Serializable;
                import javax.persistence.Column;
                import javax.persistence.Entity;
                import javax.persistence.EnumType;
                import javax.persistence.Enumerated;
                import javax.persistence.GeneratedValue;
                import javax.persistence.GenerationType;
                import javax.persistence.Id;
                import javax.persistence.NamedQuery;
                
                
                @Entity
                @NamedQuery(name="Employee.findAll", query="SELECT e FROM Employee e")
                public class Employee implements Serializable {
                    
                    private static final long serialVersionUID = 1L;
                    @Id
                    @GeneratedValue(strategy = GenerationType.AUTO)
                    private Integer id;
                    @Column(name="nom", length=30)
                    private String nom;
                    @Enumerated(EnumType.STRING)
                    @Column(length=1)
                    private Gender gender;
                
                    public Integer getId() {
                        return id;
                    }
                
                    public void setId(Integer id) {
                        this.id = id;
                    }
                
                    public String getNom() {
                        return nom;
                    }
                
                    public void setNom(String nom) {
                        this.nom = nom;
                    }
                
                    public Gender getGender() {
                        return gender;
                    }
                
                    public void setGender(Gender gender) {
                        this.gender = gender;
                    }
                
                    
                
                    @Override
                    public int hashCode() {
                        int hash = 0;
                        hash += (id != null ? id.hashCode() : 0);
                        return hash;
                    }
                
                    @Override
                    public boolean equals(Object object) {
                
                        if (!(object instanceof Employee)) {
                            return false;
                        }
                        Employee other = (Employee) object;
                        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                            return false;
                        }
                        return true;
                    }
                
                    @Override
                    public String toString() {
                        return "com.entities.Employee[ id=" + id + " ]";
                    }
                    
                }
                package com.enumeration;
                
                
                public enum Gender {
                
                    M("Male"), F("Female");
                    private String description;
                
                    private Gender(String description) {
                        this.description = description;
                    }
                
                    public String getDescription() {
                        return description;
                    }
                }
                and it generates the table employee with the gender column as a varchar(1) witch is the desired result
                thanks a million for help

                best regards rashid
                • 5. Re: Enumerations in JPA
                  REDO LOG
                  one more question
                  is there a way to convert to enumeration in jsf selectOneMenu
                  thanks
                  • 6. Re: Enumerations in JPA
                    r035198x
                    That would be a JSF forum question but JSF 2 has built in support for them
                    <h:selectOneMenu value="#{bean.person.gender}" >
                        <f:selectItems value="#{bean.genders}" />
                    </h:selectOneMenu>
                    where getGenders is a method that returns a Gender[] (just use Gender.values()). Because Enum data is constant and could be required by other places you might want to move the getGenders method to an ApplicationScoped bean instead.
                    • 7. Re: Enumerations in JPA
                      REDO LOG
                      !so I would create a
                      can't understand can you please explicite that thanks,

                      suggestion:
                      create a GenderBean class with apllication scoped annotation witch contains a list of genders

                      a Gender enumeration (like the one in the last replay)
                      and in jsf page:

                      <h:form>
                                            <h:panelGrid columns="2">
                                                  <h:outputLabel value="Name:" for="name" />
                                                  <h:inputText id="name" value="#{employee.newEmployee.name}" title="Name" />
                                                  
                                                  <h:outputLabel value="Gender:" for="gender" />
                                                  <h:selectOneMenu value="#{employeeBean.newEmployee.gender}" id="gender">
                                                      <f:selectItems value="#{genderBean.genders}" var="item2" itemLabel="#{item2}" itemValue="#{item2}" />
                                                  </h:selectOneMenu>
                                                  
                                              </h:panelGrid>
                      </h:form>
                      here is a link to the question ij the jsf forum: enumeration convertion in jsf