我正在使用hibernate中的序列,我们有一个关于序列命名的命名约束格式是SEQ_。 如何自定义/参数化SequenceStyleGenerator类以便在序列名称中添加前缀?
答案 0 :(得分:1)
我最后创建了一个继承自SequenceStyleGenerator的类,并覆盖determineSequenceName函数以获得前缀。
public class PrefixSequenceStyleGenerator extends SequenceStyleGenerator {
public static final String CONFIG_PREFER_PREFIX = "prefer_sequence_prefix";
public static final String CONFIG_SEQUENCE_PER_ENTITY_PREFIX = "sequence_per_entity_prefix";
public static final String DEF_SEQUENCE_PREFIX = "SEQ_";
@Override
protected String determineSequenceName(Properties params, Dialect dialect) {
boolean usePrefix = ConfigurationHelper.getBoolean(CONFIG_PREFER_PREFIX, params, false);
if(usePrefix){
final String sequencePerEntityPrefix = ConfigurationHelper.getString( CONFIG_SEQUENCE_PER_ENTITY_PREFIX, params, DEF_SEQUENCE_PREFIX );
String sequenceName = ConfigurationHelper.getBoolean( CONFIG_PREFER_SEQUENCE_PER_ENTITY, params, false ) ? sequencePerEntityPrefix + params.getProperty( JPA_ENTITY_NAME ) : DEF_SEQUENCE_NAME;
final ObjectNameNormalizer normalizer = (ObjectNameNormalizer) params.get( IDENTIFIER_NORMALIZER );
sequenceName = ConfigurationHelper.getString( SEQUENCE_PARAM, params, sequenceName );
if ( sequenceName.indexOf( '.' ) < 0 ) {
sequenceName = normalizer.normalizeIdentifierQuoting( sequenceName );
final String schemaName = params.getProperty( SCHEMA );
final String catalogName = params.getProperty( CATALOG );
sequenceName = Table.qualify(
dialect.quote( catalogName ),
dialect.quote( schemaName ),
dialect.quote( sequenceName )
);
}
return sequenceName;
}else{
return super.determineSequenceName(params, dialect);
}
}
}
我在我的超级课程中使用它:
@MappedSuperclass
public class AbstractModelWithTableSequence实现AbstractModelInterface {
private static final long serialVersionUID = 6503670320979543539L;
@Id
@Column(name="id")
@GenericGenerator(name = "seq_generator",
strategy = "com.gms.utils.PrefixSequenceStyleGenerator",
parameters = {
@org.hibernate.annotations.Parameter(
name = "optimizer", value = "pooled-lo"),
@org.hibernate.annotations.Parameter(
name = "initial_value", value = "1"),
@org.hibernate.annotations.Parameter(
name = "increment_size", value = "1"),
@org.hibernate.annotations.Parameter(
name = PrefixSequenceStyleGenerator.CONFIG_PREFER_SEQUENCE_PER_ENTITY, value = "true"),
@org.hibernate.annotations.Parameter(
name = PrefixSequenceStyleGenerator.CONFIG_SEQUENCE_PER_ENTITY_PREFIX, value = "SEQ_"),
@org.hibernate.annotations.Parameter(
name = PrefixSequenceStyleGenerator.CONFIG_PREFER_PREFIX, value = "true"),
}
)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_generator")
private Long id;
@Override
public Long getId() {
return id;
}
@Override
public void setId(Long id) {
this.id = id;
}
@Override
public boolean isNew() {
return getId() == null || getId() == 0;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
AbstractModelWithTableSequence other = (AbstractModelWithTableSequence) obj;
if (id == null) {
if (other.id != null) {
return false;
}
} else if (!id.equals(other.id)) {
return false;
}
return true;
}
}