1 solution.
For storing enumerations in db we use field of type integer, it means that we store only indexes of items in enumeration. Let's make simple example:
- our db script :
CREATE DATABASE example DEFAULT CHARSET=utf8;
CREATE TABLE CalendarDay (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
day_type INTEGER NULL,
PRIMARY KEY(id),
);
- part of our entity class:
@Entity
@Table(name="CALENDARDAY")
public class CalendarDay implements Serializable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id = null;
@Column(name="DAY_TYPE")
@Enumerated()
private DayType dayType;
...
- our enumeration :
public enum DayType {
WORKINGDAY,
RESTDAY,
HOLIDAY,
}
CREATE TABLE CalendarDay (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
day_type INTEGER NULL,
PRIMARY KEY(id),
);
- part of our entity class:
@Entity
@Table(name="CALENDARDAY")
public class CalendarDay implements Serializable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id = null;
@Column(name="DAY_TYPE")
@Enumerated()
private DayType dayType;
...
- our enumeration :
public enum DayType {
WORKINGDAY,
RESTDAY,
HOLIDAY,
}
If we create an instance of class CalendarDay and put it into db, in field DAY_TYPE we will only see 0,1,2 (indexes which corresponds to our enums : WORKINGDAY, RESTDAY, HOLIDAY).
Advantages of this solution are :
- less memory for storing data (as we store only integers);
Disadvantages of this solution are :
- we could not change order in which enums are written in DayType;
- store data is not so obvious to read and understand, as we see only integers, not strings;
2 solution:
For storing enumerations in db we use field of type varchar it means that we store strings of enumeration. Let's make simple example:
- our db script :
CREATE DATABASE example DEFAULT CHARSET=utf8;
CREATE TABLE CalendarDay (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
day_type VARCHAR(45) NULL,
PRIMARY KEY(id),
);
- part of our entity class:
@Entity
@Table(name="CALENDARDAY")
public class CalendarDay implements Serializable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id = null;
@Column(name="DAY_TYPE")
@Enumerated(EnumType.STRING)
private DayType dayType;
...
- our enumeration :
public enum DayType {
WORKINGDAY,
RESTDAY,
HOLIDAY,
}
CREATE TABLE CalendarDay (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
day_type VARCHAR(45) NULL,
PRIMARY KEY(id),
);
- part of our entity class:
@Entity
@Table(name="CALENDARDAY")
public class CalendarDay implements Serializable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id = null;
@Column(name="DAY_TYPE")
@Enumerated(EnumType.STRING)
private DayType dayType;
...
- our enumeration :
public enum DayType {
WORKINGDAY,
RESTDAY,
HOLIDAY,
}
If we create an instance of class CalendarDay and put it into db, in field DAY_TYPE we will see strings which corresponds to our enums : WORKINGDAY, RESTDAY, HOLIDAY).
Advantages of this solution are :
- we could not change order in which enums are written in DayType;
- store data is more obvious to read and understand;
Disadvantages of this solution are :
- a lot of memory for storing data (as we store full strings);
No comments:
Post a Comment