[JIRA] (HHH-15476) Batch save for Postgresql sequence does't work very well.
by yo (JIRA)
yo ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63123de... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiMzUxYjM5MzU2... ) / Improvement ( https://hibernate.atlassian.net/browse/HHH-15476?atlOrigin=eyJpIjoiMzUxYj... ) HHH-15476 ( https://hibernate.atlassian.net/browse/HHH-15476?atlOrigin=eyJpIjoiMzUxYj... ) Batch save for Postgresql sequence does't work very well. ( https://hibernate.atlassian.net/browse/HHH-15476?atlOrigin=eyJpIjoiMzUxYj... )
Change By: yo ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63123de... )
*I want to Batch save data to Postgresql*
see demo in [*github* |https://github.com/coding2world/sb-jpa-batch-insert-demo]
first, I write java code like this. below is pojo’s definition.
{code:java}@Data
@Entity
@Table(name = "city")
@AllArgsConstructor
public class City {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "city_id_gen")
@SequenceGenerator(name = "city_id_gen", sequenceName = "public.city_id_seq",
allocationSize = 5)
private Long id;
private String name;
private int population;
public City(String name, int population) {
this.name = name;
this.population = population;
}
public City() { }
}{code}
Repository class.
{code:java}public interface CityRepo extends JpaRepository<City, Long> {}{code}
invoke the Repository class.
{code:java}cityRepo.saveAll(range(1, 10)
.mapToObj(i -> new City("beautiful", (int)System.currentTimeMillis()/1000))
.collect(toList()));{code}
second is my sql.
{code:sql}CREATE TABLE
city(id serial PRIMARY KEY, name VARCHAR(255), population integer);{code}
*My question below:*
When I call {{SELECT nextval('city_id_seq')}} return 100. *It means 99 and 98 have been used.*
Suppose the value of the parameter named {{allocationSize}} of the annotation named {{SequenceGenerator}} is 3 , So hibernate should generate 3 id sequences once call {{SELECT nextval('city_id_seq')}}. According to the below code
{code:java}org.hibernate.id.enhanced.PooledOptimizer#generate{code}
{{SELECT nextval('city_id_seq')}} return 100 , the 3 sequence is sequences are 98, 99, 100. But *99 and 98 have been used.*
I think *Hibernate should call*
{{SELECT setval ('city_id_seq', nextval ('city_id_seq')+3)}}
is returns 102. the 3 sequence is 100, 101, 102.
*I’m not sure if Hibernate has some function to satisfy my demand.*
( https://hibernate.atlassian.net/browse/HHH-15476#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-15476#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100206- sha1:3ac329d )
2 years, 4 months
[JIRA] (HHH-15476) Batch save for Postgresql sequence does't work very well.
by yo (JIRA)
yo ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63123de... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiMTJhZTc5MWM2... ) / Improvement ( https://hibernate.atlassian.net/browse/HHH-15476?atlOrigin=eyJpIjoiMTJhZT... ) HHH-15476 ( https://hibernate.atlassian.net/browse/HHH-15476?atlOrigin=eyJpIjoiMTJhZT... ) Batch save for Postgresql sequence does't work very well. ( https://hibernate.atlassian.net/browse/HHH-15476?atlOrigin=eyJpIjoiMTJhZT... )
Change By: yo ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63123de... )
*I want to Batch save data to Postgresql*
see demo in [*github* |https://github.com/coding2world/sb-jpa-batch-insert-demo]
first, I write java code like this. below is pojo’s definition.
{code:java}@Data
@Entity
@Table(name = "city")
@AllArgsConstructor
public class City {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "city_id_gen")
@SequenceGenerator(name = "city_id_gen", sequenceName = "public.city_id_seq",
allocationSize = 5)
private Long id;
private String name;
private int population;
public City(String name, int population) {
this.name = name;
this.population = population;
}
public City() { }
}{code}
Repository class.
{code:java}public interface CityRepo extends JpaRepository<City, Long> {}{code}
invoke the Repository class.
{code:java}cityRepo.saveAll(range(1, 10)
.mapToObj(i -> new City("beautiful", (int)System.currentTimeMillis()/1000))
.collect(toList()));{code}
second is my sql.
{code:sql}CREATE TABLE
city(id serial PRIMARY KEY, name VARCHAR(255), population integer);{code}
*My question below:*
When I call {{SELECT nextval('city_id_seq')}} return 100. *It means 99 and 98 have been used.*
Suppose the value of the parameter named {{allocationSize}} of the annotation named {{SequenceGenerator}} is 3 , So hibernate should generate 3 id once call {{SELECT nextval('city_id_seq')}}. According to the below code
{code:java}org.hibernate.id.enhanced.PooledOptimizer#generate{code}
{{SELECT nextval('city_id_seq')}} return 100 , the 3 sequence is 98, 99, 100. But *99 and 98 have been used.*
I think *Hibernate should call*
{{SELECT setval ('city_id_seq', nextval ('city_id_seq')+3)}}
is returns 102. the 3 sequence is 100, 101, 102.
( https://hibernate.atlassian.net/browse/HHH-15476#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-15476#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100206- sha1:3ac329d )
2 years, 4 months