aksh1618 :
I have an enum defined as follows:
public enum TRAFFIC_SOURCE {
IP, CLIENT_ID, MOBILE
}
And in my repository:
@Column(name = "SOURCE")
@NotNull
@Enumerated(EnumType.STRING)
private TRAFFIC_SOURCE source;
Now I want to specify default value for the column. I tried two things, getting errors in both cases:
@ColumnDefault(TRAFFIC_SOURCE.IP)
// Error:(26, 43) java: incompatible types: com.myproj.enums.TRAFFIC_SOURCE cannot be converted to java.lang.String
@ColumnDefault(TRAFFIC_SOURCE.IP.name())
// Error:(26, 51) java: element value must be a constant expression
forcing me to use
@ColumnDefault("IP")
Is there a way to use Enum variable as default?
Simon Martinelli :
Because the @ColumnDefault only takes a String as argument and the value in an annotation must be a constant, the only possibility is to use
@ColumnDefault("IP")
If you are not forced to have the default value on the table column and always insert data with JPA then you could initialize the field as Kapcash said:
private TRAFFIC_SOURCE source = TRAFFIC_SOURCE.IP;