I am trying to create a sequence with a String Prefix of type CUSTOM-SEQ-<env>-<enum>-XXXX
where <env>
is environment variable, <enum>
is an Enum class and XXXX
is number sequence to be generated.
I have created by Entity as follows:
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employee_seq_generator")
@GenericGenerator(
name="employee_seq_generator",
strategy = "com.ayman.domain.generator.StringPrefixedSequenceIdGenerator",
parameters = {
@Parameter(name = StringPrefixedSequenceIdGenerator.INCREMENT_PARAM, value = "50"), // Increment value
@Parameter(name = StringPrefixedSequenceIdGenerator.VALUE_PREFIX_PARAMETER, value = "EMP_")}) // String Prefix
@Column(name = "id", updatable = false, nullable = false)
private String id;
And this is my Sequence Generator StringPrefixedSequenceIdGenerator
:
public class StringPrefixedSequenceIdGenerator extends SequenceStyleGenerator {
public static final String VALUE_PREFIX_PARAMETER = "valuePrefix";
public static final String VALUE_PREFIX_DEFAULT = "";
private String valuePrefix;
private String valuePrefix1;
@Override
public Serializable generate(SharedSessionContractImplementor session,
Object object) throws HibernateException {
return valuePrefix + String.format(numberFormat, super.generate(session, object));
}
@Override
public void configure(Type type, Properties params,
ServiceRegistry serviceRegistry) throws MappingException {
super.configure(LongType.INSTANCE, params, serviceRegistry);
valuePrefix1 = ConfigurationHelper.getString(VALUE_PREFIX_PARAMETER,
customMapParams, VALUE_PREFIX_DEFAULT);
valuePrefix = ConfigurationHelper.getString(VALUE_PREFIX_PARAMETER,
params, VALUE_PREFIX_DEFAULT);
log.info("!! configure-valueFormat: {} --- {}", valuePrefix, valuePrefix1);
}
private Map<String, String> customMapParams() {
Map<String, String> mapValues = new HashMap<>();
mapValues.put("EmployeeType", EmployeeType.Temp.toString());
mapValues.put("Env", System.getEnv("envvalue"));
return mapValues;
}
}
I am getting the response as EMP_1
I want to be something like this EMP_Temp_envvalue_1