字母数字字段的字符串增量适用于 JPA 不起作用 [英] String increment for alphanumeric field is for JPA not working

查看:32
本文介绍了字母数字字段的字符串增量适用于 JPA 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我尝试针对非主键的字段进行此操作.

i tried to this for a field that is not primary key.

这里也有同样的解决方案:如何使用 PREFIX 和单独的序列实现 IdentifierGenerator每个实体

Also same solution for here: How to implement IdentifierGenerator with PREFIX and separate Sequence for each entity

但即使我运行程序时它也不会转到Java方法.它保存为空.

But even it does not go to Java method when i run the program. It saves as null.

而且我看不到我放在课堂上的日志.我的班级没有日志.

And i cant see the log that i put inside my class. There is no log for my class.

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

I copied from that blog but my code is:

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;

我想要的是什么我需要一个独特的领域,它不是主要的.所以,我决定递增是最好的解决方案,否则,我必须检查每个创建的随机数是否存在于数据库中(我也为此提出建议).

WHAT I WANT IS I need a unique field, which is not primary. So, i decided that incrementing is best solution because otherwise, i have to check for each created random if it exists in database (i also open suggestions for this).

大约有 5-6 个字符和字母数字.

It will be around 5-6 characters and alphanumeric.

我想让 JPA 增加这个,但似乎我做不到.

I want to make JPA increment this but it seems i cant do it.

推荐答案

这与 非常相似Hibernate JPA 序列(非 Id) 但我不认为它是完全重复的.然而,这些答案似乎适用,而且它们似乎提出了以下策略:

This is very similar to Hibernate JPA Sequence (non-Id) but I don't think it's an exact duplicate. Yet the answers seem to apply and they seem to suggest the following strategies:

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

使用 @PrePersist 在它被持久化之前填充该字段.https://stackoverflow.com/a/35888326/66686

Use @PrePersist to fill the field before it gets persisted. https://stackoverflow.com/a/35888326/66686

使其成为 @Generated 并使用触发器或类似工具在数据库中生成值.https://stackoverflow.com/a/283603/66686

Make it @Generated and generate the value in the database using a trigger or similar. https://stackoverflow.com/a/283603/66686

这篇关于字母数字字段的字符串增量适用于 JPA 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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