我正在尝试将Enum保留为嵌入式值(理想情况下使用它的String表示形式,但即使现在也可以使用序数)
枚举:
@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;
}
...
}
现在,这是持久化“Day”时堆栈中有趣的部分:
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
我已经尝试了好几件事,以任何方式使它无法工作,有任何想法吗?
最佳答案
删除@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());
}
}
关于java - JPA/hibernate ,@ Embedded和Enum,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2243730/