简体   繁体   中英

How to insert More then two values in Enum type column in mysql using java?

In my Java Program Enum like this..

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;
    }
}

And my Hibernate bean define like this my TimetableVO.java

    @Column(name="REPEAT_DAYS")
@Enumerated(EnumType.STRING)
private WeekdayType repeatDays;// And Setter and Getters....

In my Service class i'm Doing like this..

String totalDays="MONDAY,SUNDAY,FRIDAY,SATURDAY"
    public void createEvent(TimetableVO timetableVO) {
    WeekdayType weekday = null;
    for (String day : totalDays.split(",")) {
        weekday = WeekdayType.valueOf(day);
    }
    timetableVO.setRepeatDays(weekday);
......
......
......
entityManager.persist(timetableVO);     



    }

But the Problem is In database column Adding Last Value Only...that mean it Override Previous value

For Ex:above String SATURDAY only inserting on Database but i want to insert all values like MONDAY,SUNDAY,FRIDAY,SATURDAY ...give me Suggestion..

change your column type

private WeekdayType repeatDays;

should be changed to String type

private String repeatDays;

set your csv to this column.

Other option is to have

private List<WeekdayType> repeatDays; with @JoinTable annotation

which will create another mapping table. to handle manytomany relation.

Trying to save more values in a single column is a conceptual error in a relational database. A solution could be to create another table, and create a relation between your TimeTable and the newly created table.

A second, less flexible alternative, could be create 7 different properties (and also seven different columns) each one relative to a single day, without creating a new table, so you could have:

 @Column(name="MONDAY")
 private boolean repeatOnMonday;

 @Column(name="TUESDAY")
 private boolean repeatOnTuesday;

 //...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM