启用Hibernate HiLo策略 [英] Enable Hibernate HiLo Strategy

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

问题描述

我在没有任何XML的情况下初始化Hibernate

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

我的课程使用的身份证如

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

使用的生成器是 SequenceStyleGenerator ,似乎要取代已弃用的 SequenceGenerator SequenceHiLoGenerator 等等。它使用

  public static final int DEFAULT_INCREMENT_SIZE = 1; 

并且似乎允许通过



$ b <$ p进行配置$ p> public static final String INCREMENT_PARAM =increment_size;

但这就是我能找到的全部。我想我必须设置一些属性xxx.yyy.increment_size或以另一种方式传递给Hibernate,但我看不出如何。






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

解决方案

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



使用 @GenericGenerator 注释设置 increment_size 下面的示例代码段使用 hilo 优化器和 SEQUENCE 策略。

  @GeneratedValue(strategy = GenerationType.SEQUENCE,generator =hilo_generator)
@GenericGenerator(
name =hilo_generator,
strategy =org.hibernate。 id.enhanced.SequenceStyleGenerator,
parameters = {
//或者留下来获取hibernate_sequence。
@Parameter(name =sequence_name,value =hilo_sequence),
//或者保留它,因为这是默认值。
@Parameter(name =initial_value,value =1),
@Parameter(name =increment_size,value =5),
@Parameter(name =optimizer,value =hilo)
})

你无法全局设置 DEFAULT_IN CREMENT_SIZE 带有Hibernate配置属性。您需要使用 @Id 配置属性。


I'm initializing Hibernate without any XML by something like

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

My classes use an ID like

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

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;

and seems to allow configuration via

public static final String INCREMENT_PARAM = "increment_size";

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.


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

解决方案

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

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

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天全站免登陆