0

In my Code..

List<WeekdayType> weekday = new ArrayList<WeekdayType>();

i'm insert the weekday into Database... Hear WeekdayType is 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;
}}

and i'm creating Bean in Hibernate like this..

    @Entity
@Table(name="EC_TIMETABLE")
public class TimetableVO
{
-------
-----
@Column(name="REPEAT_DAYS")
    private List<WeekdayType> repeatDays;
//Setter and Getter...
}

i'm inserting the values into Database but it giving Error:

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: EC_TIMETABLE, for columns: [org.hibernate.mapping.Column(REPEAT_DAYS)]
Java Developer
  • 1,873
  • 8
  • 32
  • 63

1 Answers1

2

You need to annotate the Enum list with @Enumerated annotation.

@Column(name="REPEAT_DAYS")
@Enumerated(EnumType.STRING)
private List<WeekdayType> repeatDays;

You can specify how the enum should be persisted in the database with the EnumType enum property of the @Enumerated annotation. EnumType.ORDINAL specifies that the enum will be persisted as an integer value. Here, myEnum set to VALUE1 would be persisted as 0, VALUE2 as 1, etc.

The alternative is to use EnumType.STRING to specify that the enum will be persisted using the name of the enum value that the field is set to. So, applied to the previous example, setting the field myEnum to MyEnum.VALUE1 will persist as VALUE1, etc.

Update: Also you need to specify columnDefinition property with all valid values of enum.

@Column(name="REPEAT_DAYS", columnDefinition="enum(1,2,3)")

Reference: mapping-enum-types-with-hibernate-annotations

Community
  • 1
  • 1
Viral Patel
  • 8,506
  • 3
  • 32
  • 29