I have 10 records and I am trying to insert 5 records at a time using batch insert in hibernate.But the problem here is after every session.save() it is generating insert query. So if i want to insert 1000 records it will generate 1000 insert queries. I wanted to insert 5 records at a time. Below is the code snippet
public void saveobjects(List<DBDimAssetGroup> objects) {
Session session = super.getsession();
for (int i = 1; i <= 10; i++) {
System.out.println("Statement Queued : " + i);
DBDimAssetGroup obj = objects.get(i);
session.save(obj);
if (i % 5 == 0) {
session.flush();
session.clear();
}
}
session.close();
}
Below is the ScreenShot of console generating 10 queries for 10 inserts
Below is the Hibernate Configuration
EDIT 1:
After changing GenerationType to Sequence in Entity Class , I am getting below error
EDIT 2:
Even after setting generationtype to sequence It is generating multiple queries
Below is the console
Below is the Entity Class
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="users_seq")
@SequenceGenerator(name="users_seq" , allocationSize = 1, sequenceName = "DIM_ASSET_GROUP_SEQ")
@Column(name="ASSET_GROUP_ID", updatable = false, insertable = false)
private int assetgroupid;