SequenceStyleGenerator,如何使用前缀而不是后缀 [英] SequenceStyleGenerator, how to to use prefix instead of suffixe

查看:55
本文介绍了SequenceStyleGenerator,如何使用前缀而不是后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在休眠中使用序列,我们对序列的命名有一个约束,命名格式为SEQ_.我如何自定义/参数化SequenceStyleGenerator类,以便在序列名称中添加前缀?

I am working with sequences in hibernate, we have a naming constraint concerning sequence naming the format is SEQ_. How can i customize/parametrize the class SequenceStyleGenerator in order to a prefix in the sequence name?

推荐答案

我最后创建了一个从SequenceStyleGenerator继承并覆盖defineSequenceName函数以具有前缀的类.

I ended by creating a class that inherit from SequenceStyleGenerator and override the determineSequenceName function in order to have a prefix.

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);
    }
}

}

并且我在我的超类中使用它:

and I use it in my super class :

@MappedSuperclass

公共类AbstractModelWithTableSequence实现AbstractModelInterface {

public class AbstractModelWithTableSequence implements 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;
}

}

这篇关于SequenceStyleGenerator,如何使用前缀而不是后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆