mysql数据库的操作dao vo,如何在Mysql数据库中避免'数据被截断'使用java?

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值