Specifying distinct sequence per table in Hibernate on subclasses

前端 未结 5 733
北荒
北荒 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:06

    IHMO there is better way to do this:

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    

    It works in my app.

    0 讨论(0)
  • 2020-12-28 15:20

    Have you tried doing it this way ?

    @MappedSuperclass
    public abstract class DataObject implements Serializable {
        @Id 
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idgen")
        @Column(name = "id")
        private int id;
    }
    
    @Entity
    @SequenceGenerator(initialValue = 1, name = "idgen", sequenceName = "entityaseq")
    @Table(name = "entity_a")
    public class EntityA extends DataObject { 
    
    }
    
    @Entity
    @SequenceGenerator(initialValue = 1, name = "idgen", sequenceName = "entitybseq")
    @Table(name = "entity_b")
    public class EntityB extends DataObject {
    
    }
    

    I'm sorry I don't have the required environment to test it right now but I'll try it later.

    0 讨论(0)
  • 2020-12-28 15:27

    I was not quite happy about the need to declare the sequence name on each class individually. I have checked the source code and came up with this solution:

    import javax.persistence.Column;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import org.hibernate.annotations.GenericGenerator;
    import org.hibernate.annotations.Parameter;
    import org.hibernate.id.enhanced.SequenceStyleGenerator;
    
    // ...
    
    @Id
    @GeneratedValue(generator = "sequenceIdGenerator")
    @GenericGenerator(
            name = "sequenceIdGenerator", 
            strategy = "sequence",
            parameters = @Parameter(
                    name = SequenceStyleGenerator.CONFIG_PREFER_SEQUENCE_PER_ENTITY,
                    value = "true"))
    @Column(updatable = false, nullable = false)
    protected Long id;
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-28 15:29

    TABLE generation stretergy uses separate db sequence for each table but it is little expensive operation

    0 讨论(0)
提交回复
热议问题