2010-02-11 121 views
4

我想堅持一個枚舉作爲內含價值(最好使用它的字符串表示,但即使順序將是確定現在)JPA /休眠,@Embedded和枚舉

枚舉:

@Embeddable 
public enum DayOfTheWeek { 
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; 

public int toCalendar() throws EnumConstantNotPresentException{ 
    switch (this) { 
    case SUNDAY: 
    return Calendar.SUNDAY; 
    case MONDAY: 
    return Calendar.MONDAY; 
    case TUESDAY: 
    return Calendar.TUESDAY; 
    case WEDNESDAY: 
    return Calendar.WEDNESDAY; 
    case THURSDAY: 
    return Calendar.THURSDAY; 
    case FRIDAY: 
    return Calendar.FRIDAY; 
    case SATURDAY: 
    return Calendar.SATURDAY; 
    } 
    throw new EnumConstantNotPresentException(this.getClass(), "Unknown value ["+this.name()+"]"); 
} 

public static DayOfTheWeek fromCalendarDay(int day) { 
    switch (day) { 
    case Calendar.SUNDAY: 
    return SUNDAY; 
    case Calendar.MONDAY: 
    return MONDAY; 
    case Calendar.TUESDAY: 
    return TUESDAY; 
    case Calendar.WEDNESDAY: 
    return WEDNESDAY; 
    case Calendar.THURSDAY: 
    return THURSDAY; 
    case Calendar.FRIDAY: 
    return FRIDAY; 
    case Calendar.SATURDAY: 
    return SATURDAY; 
    } 
    return null; 
} 

public static DayOfTheWeek getByDate(Date date) { 
    Calendar calendar = GregorianCalendar.getInstance(); 
    calendar.setTime(date); 
    return fromCalendarDay(calendar.get(Calendar.DAY_OF_WEEK)); 
} 

@Override 
/** 
    * Should return the localized day of the week 
    */ 
public String toString() { 
    Calendar c = new GregorianCalendar(); 
    c.set(Calendar.DAY_OF_WEEK, this.toCalendar()); 
    SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat 
    .getInstance(); 
    sdf.applyPattern("EEEEEEEEEE"); 

    return sdf.format(c.getTime()); 
} 

} 

的類別:

@Entity 
public class Day implements Serializable { 

... 

@Enumerated 
@Embedded 
private DayOfTheWeek dayOfTheWeek; 

... 

@Id 
@GeneratedValue 
private Long id; 

public void setId(Long id) { 
    this.id = id; 
} 

public Long getId() { 
    return id; 
} 

public void setDayOfTheWeek(DayOfTheWeek dayOfTheWeek) { 
    this.dayOfTheWeek = dayOfTheWeek; 
} 

public DayOfTheWeek getDayOfTheWeek() { 
    return dayOfTheWeek; 
} 

... 
} 

現在,這裏是堆疊的堅持 「日」 的時候,有趣的部分:

org.springframework.orm.hibernate3.HibernateSystemException: No default constructor for entity: xxx.yyyy.zzz.DayOfTheWeek; nested exception is org.hibernate.InstantiationException: No default constructor for entity: xxx.yyyy.zzz.DayOfTheWeek 

我試過幾件事,無法以任何方式讓它工作,任何想法?

回答

10

取出@Embedded@Embeddable註釋只需使用@Enumerated(EnumType.STRING)把它作爲一個字符串。

作爲一個側面說明,考慮定義你這樣的枚舉,以避免開關

import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.GregorianCalendar; 

public enum DayOfTheWeek { 
    SUNDAY { 
     @Override 
     public int toCalendar() { 
      return Calendar.SUNDAY; 
     } 

    }, 
    MONDAY { 
     @Override 
     public int toCalendar() { 
      return Calendar.MONDAY; 
     } 
    }, 
    TUESDAY { 
     @Override 
     public int toCalendar() { 
      return Calendar.TUESDAY; 
     } 
    }, 
    WEDNESDAY { 
     @Override 
     public int toCalendar() { 
      return Calendar.WEDNESDAY; 
     } 
    }, 
    THURSDAY { 
     @Override 
     public int toCalendar() { 
      return Calendar.THURSDAY; 
     } 
    }, 
    FRIDAY { 
     @Override 
     public int toCalendar() { 
      return Calendar.FRIDAY; 
     } 
    }, 
    SATURDAY { 
     @Override 
     public int toCalendar() { 
      return Calendar.SATURDAY; 
     } 
    }; 

    public abstract int toCalendar(); 

    public static DayOfTheWeek fromCalendarDay(int day) { 

     for (DayOfTheWeek dayOfWeek : DayOfTheWeek.values()) { 
      if (dayOfWeek.toCalendar() == day) { 
       return dayOfWeek; 
      } 
     } 

     return null; // Consider throwing IllegalArgumentException 
    } 

    public static DayOfTheWeek getByDate(Date date) { 
     Calendar calendar = GregorianCalendar.getInstance(); 
     calendar.setTime(date); 
     return fromCalendarDay(calendar.get(Calendar.DAY_OF_WEEK)); 
    } 

    @Override 
    /* 
    * Should return the localized day of the week 
    */ 
    public String toString() { 
     Calendar c = new GregorianCalendar(); 
     c.set(Calendar.DAY_OF_WEEK, this.toCalendar()); 
     SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat 
       .getInstance(); 
     sdf.applyPattern("EEEEEEEEEE"); 

     return sdf.format(c.getTime()); 
    } 

} 
+0

使用字段是比寫'toCalendar()'返回一個常量值更好的解決方案。 – 2014-01-19 19:37:20

6

爲什麼你想要枚舉爲@Embeddable。簡單地將它註釋爲列有什麼問題?

@Enumerated(EnumType.STRING) 
@Column(name = "...") 
public DayOfTheWeek getDayOfTheWeek() { 
    return dayOfTheWeek; 
} 
4

我知道這個問題已經回答了,這是很久以前的事。

我只是想建議您的枚舉建設進一步提高。 這裏是:

public enum DayOfTheWeek { 
     SUNDAY(Calendar.SUNDAY), 
     MONDAY(Calendar.MONDAY), 
     TUESDAY(Calendar.TUESDAY), 
     WEDNESDAY(Calendar.WEDNESDAY), 
     THURSDAY(Calendar.THURSDAY), 
     FRIDAY(Calendar.FRIDAY), 
     SATURDAY(Calendar.SATURDAY); 

     DayOfTheWeek(int dayOfTheWeek){ 
      this.dayOfTheWeek = dayOfTheWeek; 
     } 
     int dayOfTheWeek; 

     public int toCalendar(){ 
      return dayOfTheWeek; 
     } 

    } 

謝謝你們,請注意!玩的開心!