Specifying distinct sequence per table in Hibernate on subclasses

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

提交回复
热议问题