Specifying distinct sequence per table in Hibernate on subclasses

前端 未结 5 735
北荒
北荒 2020-12-28 14:47

Is there a way to specify distinct sequences for each table in Hibernate, if the ID is defined on a mapped superclass?

All entities in our application extend a super

5条回答
  •  失恋的感觉
    2020-12-28 15:28

    We use this in the abstract superclass of all of our JPA entities:

    @Id
    @GeneratedValue(generator = "pooled")
    @GenericGenerator(name = "pooled", strategy = "org.hibernate.id.enhanced.TableGenerator", parameters = {
            @org.hibernate.annotations.Parameter(name = "value_column_name", value = "sequence_next_hi_value"),
            @org.hibernate.annotations.Parameter(name = "prefer_entity_table_as_segment_value", value = "true"),
            @org.hibernate.annotations.Parameter(name = "optimizer", value = "pooled-lo"),
            @org.hibernate.annotations.Parameter(name = "increment_size", value = "100")})
    private Long id;
    

    It's a bit verbose, but it allows setting the prefer_entity_table_as_segment_value which means you don't need to repeat the id field or the generator annotations in the subclasses.

提交回复
热议问题