javahibernatejpasequence-generators

Is setting allocateSize as 1 in Sequence generator same as using an Identity Generator?


I moved from Identity generator to Sequence hoping that it would improve performance of my bulk inserts. I am doing bulk inserts using JPA EntityManager's persist method with a batch size of 50. Inserting upto 300K records.

The Entity table has the following settings for the primary key column - id:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="myseq")
@SequenceGenerator(name="myseq", sequenceName="my_table_id_seq", allocationSize = 1)
private Long id;

Earlier, when I just used allocationSize as default of 50, and used this property - 'hibernate.id.new_generator_mappings' - (set it to true), I ran into issues such as - batch entry 0 insert into hibernate, use getNextException()

And therefore, I changed the allocationSize to 1. Since then, I did not see the issue above. However, this does not improve performance as much as allocation size of 50. But that also leads to insane gaps between new ids and the ids that are already present due to allocationSize of 50.


Solution

  • I realized Hibernate adds too many overheads. Just used a JdbcTemplate for inserts that are more than 1 million and it works like a charm :)

    I would use hibernate only when the data load is not so huge!