Hi.
I want to insert many entities into my database via Hibernate. I use batch insertion but it seems to be too slow for my use case. When I debug the hibernate SQL I can see that it generates a SELECT statement for each session.save(...) call I use. I don't understand why? Is this the normal behaviour?
My entity:
Code:
@Entity
@org.hibernate.annotations.Entity(dynamicInsert=true, dynamicUpdate=true)
@Cache(include = "non-lazy", usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = "com.mypackage.impl.MyEntityImpl")
@Table(name = "PRICING_Price")
public class MyEntityImpl implements MyEntity, Serializable {
@Id
@Column(name = "ID", nullable = false, columnDefinition = "char(36)")
private String ID = UUID.randomUUID().toString();
// ... some fields
@Version
private int version;
}
My insertion code:
Code:
void insert() {
int batchSize = 50;
Session session = ... // get Session
Transaction t = session.beginTransaction();
for (int i=0; i<3000000; i++) {
addEntity(...);
if (i%batchSize == 0) {
flushInserts(session);
}
}
transaction.commit();
}
void addEntity(...) {
MyEntity entity = new MyEntity Impl();
// set properties
myEntityQueue.add(entity);
}
void flushInserts(Session session) {
for(MyEntity entity : myEntityQueue) {
session.save(entity); // <--- generate SELECT statement
}
myEntityQueue.clear();
session.flush(); // <--- generates 50 INSERT statements
session.clear();
}
What can I do to speed up the insertion?
Regards, jacquipre.