Hibernate / Hibernate 7 Basics Interview Questions
What is Hibernate's flush behaviour and how does it affect SQL execution?
Flushing synchronises the in-memory persistence context with the database by sending pending SQL. Flush does NOT commit the transaction.
| FlushMode | When SQL sent | Use case |
|---|---|---|
| AUTO (default) | Before queries and at commit | Normal transactional operations |
| COMMIT | Only at commit | Read-heavy operations |
| ALWAYS | Before every query | Strict consistency required |
| MANUAL | Only explicit session.flush() | Bulk operations |
// AUTO flush mode (default): Product p = new Product("Laptop", new BigDecimal("999")); session.persist(p); // INSERT scheduled, NOT sent yet // Query triggers flush in AUTO mode: Long count = session.createQuery("SELECT COUNT(p) FROM Product p", Long.class) .getSingleResult(); // INSERT flushed first so count is accurate // Explicit flush: session.flush(); // sends SQL but does NOT commit // Check for pending changes: session.isDirty(); // true if unflushed changes exist // SQL ordering at flush: INSERT -> UPDATE -> DELETE // Prevents FK constraint violations
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
