I released five new sample lessons from my Test With Spring course: Introduction to Spock Framework

Fixing JPA2 Sequence Generator Problem With Hibernate 3.5

I ran into a really weird problem when I was playing around with JPA2 by using Hibernate 3.5 as JPA2 implementation. I had implemented two entity classes called BaseEntity and Person. BaseEntity class contains the common properties of all entity classes. The Person class extends the BaseEntity class and adds some person specific properties in it. The relevant code of these entities is given in following:

The BaseEntity class:

@MappedSuperclass
public class BaseEntity {
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="idSequence")
    private Long id = null;

    public BaseEntity() {
    }    

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

The Person class:

@Entity
@Table(name="persons")
@SequenceGenerator(name="idSequence", sequenceName="person_sequence")
public class Person extends BaseEntity {
    //Person specific content irrelevant to this issue.
}

The problem was that the value returned by person_sequence was always multiplied by hundred. For example, if the sequence returned value 5, value 500 was inserted to the id column of persons table. At first I thought that this issue was related to the used database engine (PostgreSql 8.4), but since I verified that it is working as expected, I had to start looking for other reason to this behavior. The Javadoc of the javax.persistence.SequenceGenerator class provided an partial answer, because I found out that it contained a property called allocationSize, which is specified as follows:

(Optional) The amount to increment by when allocating sequence numbers from the sequence.

However, its default value is 50, not 100. I decided to give it a go and modified my Person class as follows:

@Entity
@Table(name="persons")
@SequenceGenerator(allocationSize=1, name="idSequence", sequenceName="person_sequence")
public class Person extends BaseEntity {
    //Person specific content irrelevant to this issue.
}

After this modification the value inserted to the id column of the Persons table was the same than value fetched from the person_sequence sequence. I am not sure how common this problem is, but I have never encountered this problem when building web applications by using Hibernate without JPA. This leads me to wonder, if this is a Hibernate specific problem or are other JPA2 implementations behaving in the same way. However, in my opinion the default value of the allocationSize property should be one, because it is the most common use case of id generation by using a sequence.

About the Author

Petri Kainulainen is passionate about software development and continuous improvement. He is specialized in software development with the Spring Framework and is the author of Spring Data book.

About Petri Kainulainen →

2 comments… add one
  • Hello from Vyatka River!!! Thank you for information! Itґs a good idea for next full revision…
    Write more!!! :)))

    Reply
  • The issue presented here is related to this bug: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5042

    I believe you used hibernate version 3.5.1-Final. The issues should’ve been fixed in 3.5.5-Final

    Reply

Leave a Comment