title: how to save enum value to DB with Hibernate?
body: <p>I tried to fill DB tables with random data and through Hibernate.</p>
  
  <p>But my code fill incompatible data into tables (not exactly incompatible it is index of this element declared at enum, for ex: at <code>ApartmentState</code> - <code>FREE</code> is first element it set to appropriate column it index - 0. But I want to put or <code>FREE</code> as enum or as string).</p>
  
  <p>I couldn't figure out why exactly this happen.</p>
  
  <p>Here is code snippet:</p>
  
  <pre><code>private List&lt;Apartment&gt; generateApartments() {
          for (int i = 1; i &lt;= 3; i++) {
              for (int j = 2; j &lt;= 5; j++) {
                  Apartment apartment = new Apartment();
                  // fill apartment
                  apartment.setRoomName(generateRoomName());
                  apartment.setPricePerHour(generatePrice());
                  apartment.setApartmentState(FREE);
                  apartment.setRating(getDesiredRating(j));
                  apartment.setSleepPlaces(getDesiredPlaces(i));
                  apartment.setActive(true);
                  // save apartment
                  apartments.add(apartment);
              }
          }
          return apartments;
      }
  
      private Apartment.SleepPlaces getDesiredPlaces(int i) {
          switch (i) {
              case 1:
                  return ONE_PLACE;
              case 2:
                  return TWO_PLACES;
              case 3:
                  return THREE_PLACES;
          }
          return ONE_PLACE;
      }
  
      private Apartment.StarRating getDesiredRating(int j) {
          switch (j) {
              case 2:
                  return TWO_STARS;
              case 3:
                  return THREE_STARS;
              case 4:
                  return FOUR_STARS;
              case 5:
                  return FIVE_STARS;
          }
          return TWO_STARS;
      }
  </code></pre>
  
  <p>I need to fill at table some enum values, as rating (2, 3, 4) and sleep places (1, 2..). </p>
  
  <p>But this puts some wrong data into table. </p>
  
  <p>Here is content at workbench:</p>
  
  <p><img src="https://i.stack.imgur.com/lPgpa.png" alt="apartment"> </p>
  
  <p>Why it puts only index, not as string or as enum.<br>
  How can I recovering this at desired value, in the future.</p>
  
  <p>Here is snippet from Apartment class:</p>
  
  <pre><code>@Entity
  @Table(name = "Apartments")
  public class Apartment extends AbstractEntity implements Comparable&lt;Apartment&gt; {    
      private Integer id;
      private String roomName;
      private Double pricePerHour;
      private ApartmentState apartmentState;
      private StarRating rating;
      private SleepPlaces sleepPlaces;
      private Boolean active;
  
      public enum ApartmentState {
          FREE, REQUESTED, BOOKED, LIVED, CLEANING, PROCESSING
      }
  
      public enum StarRating {
          TWO_STARS("2 stars"), THREE_STARS("3 stars"), FOUR_STARS("4 stars"), FIVE_STARS("5 stars");
  
          private String description;
  
          private StarRating(String description) {
              this.description = description;
          }
          public String getDescription() {
              return description;
          }
      }
  
      public enum SleepPlaces {
          ONE_PLACE("1 person"), TWO_PLACES("2 persons"), THREE_PLACES("3 persons"), FOUR_PLACES("4 persons");
  
          private String count;
  
          private SleepPlaces(String count) {
              this.count = count;
          }
          public String getCount() {
              return count;
          }
      }
  </code></pre>
  
  <p>For me the best is to put enum as enum (possibly at MySql workbench) or as a string (and use <code>name()</code> and <code>valueOf()</code> from Enum class). </p>
  
  <p>But how to implement it with hibernate. </p>
  
  <p>How to solve this trouble?</p>