Have you used HQL, Native Query, JDBC directly? Batching in Hibernate?
sobes.tech AI
Answer from AI
Yes, I used all three approaches:
-
HQL (Hibernate Query Language) — an object-oriented query language that allows writing database queries at the Java/Kotlin entity level, abstracting from the specific DBMS.
-
Native Query — SQL queries written directly and executed through Hibernate when specific DBMS features need to be used or performance optimization is required.
-
JDBC directly — sometimes used for simple operations or when Hibernate is not suitable, for example, to execute specific procedures or when integrating with legacy code.
Batching in Hibernate — a technique of grouping multiple insert/update operations into one batch to reduce the number of database requests and improve performance. It is configured via the hibernate.jdbc.batch_size parameter and requires proper entity configuration.
Example of batching configuration in Hibernate:
hibernate.jdbc.batch_size=50
hibernate.order_inserts=true
hibernate.order_updates=true
And an example code for batch inserts:
session.beginTransaction()
for (i in 1..1000) {
val entity = MyEntity(data = "value $i")
session.save(entity)
if (i % 50 == 0) {
session.flush()
session.clear()
}
}
session.transaction.commit()
This allows sending data in batches of 50 records, reducing load on the database and increasing speed.