In my Java Program ..Hibernate Bean class define one column like..
TimtableVO.java
@Column(name="REPEAT_DAYS")
private WeekdayType repeatDays;//hear WeekdayType is Enum Class
And in my service class i'm using this TimetableVO.java bean class as..
This is my Service class:
public void createEvent(TimetableVO timetableVO) {
WeekdayType weekday = null;
for (String day : timetableVO.getTemp().split(",")) {
weekday = WeekdayType.valueOf(day);
}
timetableVO.setRepeatDays(weekday);
userDAO.createEvent(timetableVO);
}
And my DAO class i'm inserting timetableVO object..
public void createEvent(TimetableVO timetableVO) throws DataAccessException {
entityManager.persist(timetableVO);
}
But Exception will come..
Exception: Caused by: java.sql.BatchUpdateException: Data truncated for column 'REPEAT_DAYS' at row 1
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2045)
at coHibernate: insert into EC_TIMETABLE (CLASS_DURATION, COURSE_DURATION, COURSE_FEE, END_DATE, REPEAT_COUNT, REPEAT_DAYS, REPEAT_TYPE, SEARCH_KEY, START_DATE, S_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
m.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1468)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 65 more
Caused by: java.sql.SQLException: Data truncated for column 'REPEAT_DAYS' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
EDIT:
This is my WeekdayType Enum
public enum WeekdayType {
MONDAY(Calendar.MONDAY), TUESDAY(Calendar.TUESDAY), WEDNESDAY(
Calendar.WEDNESDAY), THURSDAY(Calendar.THURSDAY), FRIDAY(
Calendar.FRIDAY), SATURDAY(Calendar.SATURDAY), SUNDAY(
Calendar.SUNDAY);
private int day;
private WeekdayType(int day) {
this.day = day;
}
public int getDay() {
return day;
}
}
解决方案
Looks like the error is due the fact that the column REPEAT_DAYS is too small to save the values you're passing via the WeekdayType Enum. So recheck the definition of that column in your underling database and eventually increase it's size.
Also you're missing an annotation:
@Column(name="REPEAT_DAYS", length="45")
@Enumerated(EnumType.STRING)
private WeekdayType repeatDays;
The length in the annotation should match the length of your column in your DB schema.