Enable Hibernate HiLo Strategy

Deadly 提交于 2019-12-06 09:57:38

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/34551509/enable-hibernate-hilo-strategy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!