字母数字字段的字符串增量不适用于JPA

时间:2018-11-12 06:42:15

标签: java spring-data-jpa

https://vladmihalcea.com/how-to-implement-a-custom-string-based-sequence-identifier-generator-with-hibernate/

我尝试了非主键字段。

这里同样的解决方案: How to implement IdentifierGenerator with PREFIX and separate Sequence for each entity

但是当我运行程序时,即使它也没有转到Java方法。它保存为空。

我看不到我放在课堂上的日志。我的课程没有日志。

我是从那个博客复制的,但是我的代码是:

public class StringSequenceIdentifier
        implements IdentifierGenerator, Configurable {

    public static final String SEQUENCE_PREFIX = "sequence_prefix";

    private String sequencePrefix;

    private String sequenceCallSyntax;

    @Override
    public void configure(
            Type type, Properties params, ServiceRegistry serviceRegistry)
            throws MappingException {
        System.out.println("xxx");
        final JdbcEnvironment jdbcEnvironment =
                serviceRegistry.getService(JdbcEnvironment.class);
        final Dialect dialect = jdbcEnvironment.getDialect();

        final ConfigurationService configurationService =
                serviceRegistry.getService(ConfigurationService.class);
        String globalEntityIdentifierPrefix =
                configurationService.getSetting( "entity.identifier.prefix", String.class, "SEQ_" );

        sequencePrefix = ConfigurationHelper.getString(
                SEQUENCE_PREFIX,
                params,
                globalEntityIdentifierPrefix);

        final String sequencePerEntitySuffix = ConfigurationHelper.getString(
                SequenceStyleGenerator.CONFIG_SEQUENCE_PER_ENTITY_SUFFIX,
                params,
                SequenceStyleGenerator.DEF_SEQUENCE_SUFFIX);

        final String defaultSequenceName = ConfigurationHelper.getBoolean(
                SequenceStyleGenerator.CONFIG_PREFER_SEQUENCE_PER_ENTITY,
                params,
                false)
                ? params.getProperty(JPA_ENTITY_NAME) + sequencePerEntitySuffix
                : SequenceStyleGenerator.DEF_SEQUENCE_NAME;

        sequenceCallSyntax = dialect.getSequenceNextValString(
                ConfigurationHelper.getString(
                        SequenceStyleGenerator.SEQUENCE_PARAM,
                        params,
                        defaultSequenceName));
    }

    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object obj) {
        System.out.println("xxx");
        if (obj instanceof Identifiable) {
            Identifiable identifiable = (Identifiable) obj;
            Serializable id = identifiable.getId();
            if (id != null) {
                return id;
            }
        }
        long seqValue = ((Number) Session.class.cast(session)
                .createSQLQuery(sequenceCallSyntax)
                .uniqueResult()).longValue();

        return sequencePrefix + String.format("%011d%s", 0 ,seqValue);
    }
}

那是我的网域:

@GenericGenerator(
        name = "assigned-sequence",
        strategy = "xxxxxx.StringSequenceIdentifier",
        parameters = {
                @org.hibernate.annotations.Parameter(
                        name = "sequence_name", value = "hibernate_sequence"),
                @org.hibernate.annotations.Parameter(
                        name = "sequence_prefix", value = "CTC_"),
        }
)
@GeneratedValue(generator = "assigned-sequence", strategy = GenerationType.SEQUENCE)
private String referenceCode;

我想做什么 我需要一个唯一的字段,而不是主要字段。因此,我认为递增是最好的解决方案,因为否则,我必须检查每个创建的随机数是否存在于数据库中(为此我也提出了建议)。

它将是大约5-6个字符和字母数字。

我想让JPA递增,但看来我做不到。

1 个答案:

答案 0 :(得分:1)

这与Hibernate JPA Sequence (non-Id)非常相似,但我不认为这是完全相同的副本。但是答案似乎适用,并且似乎建议了以下策略:

  1. 使要生成的字段成为对实体的引用,其唯一目的是该字段现在成为ID,并且可以通过常规策略生成。 https://stackoverflow.com/a/536102/66686

  2. 使用@PrePersist填充字段,然后再将其保留。 https://stackoverflow.com/a/35888326/66686

  3. 使其@Generated,并使用触发器或类似方法在数据库中生成值。 https://stackoverflow.com/a/283603/66686