如何启用Hibernate HiLo实体标识符优化器策略 [英] How to enable the Hibernate HiLo entity identifier optimizer strategy

查看:117
本文介绍了如何启用Hibernate HiLo实体标识符优化器策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过类似这样的方式初始化没有任何XML的Hibernate

I'm initializing Hibernate without any XML by something like

org.hibernate.SessionFactory sessionFactory = 
    new org.hibernate.cfg.Configuration().
    .setProperty(...)
    .setProperty(...)
    ...
    .buildSessionFactory();

我的班级使用的ID如

@Id @Generated(GenerationTime.INSERT) @GeneratedValue private Integer id;

使用的生成器是SequenceStyleGenerator,它似乎是已弃用的SequenceGeneratorSequenceHiLoGenerator以及其他内容的替代物.它使用

The generator used is SequenceStyleGenerator, which seems to be the replacement for the deprecated SequenceGenerator and SequenceHiLoGenerator and whatever. It uses

public static final int DEFAULT_INCREMENT_SIZE = 1;

,并且似乎允许通过

public static final String INCREMENT_PARAM = "increment_size";

但这就是我能找到的全部.我猜我必须设置一些属性"xxx.yyy.increment_size"或以其他方式将其传递给Hibernate,但是我看不到如何.

but that's all I could find out. I guess I have to set some property "xxx.yyy.increment_size" or pass it in another way to Hibernate, but I can't see how.

我知道@SequenceGenerator,但是它似乎被完全忽略了

I'm aware of @SequenceGenerator, but it seems to be completely ignored

推荐答案

我想您正在寻找如何为SequenceSytleGenerator设置increment_size属性的方法.

I guess you are looking for how to set increment_size property for your SequenceSytleGenerator.

使用@GenericGenerator注释和hilo优化程序和SEQUENCE策略在设置increment_size下方的示例代码段.

Sample snippet below setting increment_size using @GenericGenerator annotation with hilo optimizer and SEQUENCE strategy.

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "hilo_generator")
@GenericGenerator(
        name = "hilo_generator",
        strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
        parameters = {
                // Or leave it out to get "hibernate_sequence".
                @Parameter(name = "sequence_name", value = "hilo_sequence"),
                // Or leave it out as this is the default.
                @Parameter(name = "initial_value", value = "1"),
                @Parameter(name = "increment_size", value = "5"),
                @Parameter(name = "optimizer", value = "hilo")
        })

您无法使用Hibernate配置属性全局设置DEFAULT_INCREMENT_SIZE.您需要使用@Id配置属性.

There's no way you can globally set the DEFAULT_INCREMENT_SIZE with a Hibernate configuration property. You need to use the @Id configuration properties instead.

这篇关于如何启用Hibernate HiLo实体标识符优化器策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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