Hibernate / Hibernate 7 Basics Interview Questions
What is the Session in Hibernate 7 and what are its core CRUD operations?
The Session is Hibernate's primary interface for database operations. It represents a unit of work and maintains the first-level cache. Key API changes in Hibernate 7:
| Old method (removed) | Hibernate 7 replacement |
|---|---|
| session.save(entity) | session.persist(entity) |
| session.get(Class,id) | session.find(Class,id) |
| session.delete(entity) | session.remove(entity) |
| session.update(entity) | session.merge(entity) |
| session.saveOrUpdate(entity) | session.merge(entity) |
session.beginTransaction(); // CREATE Product p = new Product("Book", new BigDecimal("19.99")); session.persist(p); // replaces save() // READ: returns null if not found Product found = session.find(Product.class, 1L); // replaces get() // READ lazy proxy Product ref = session.getReference(Product.class, 1L); // UPDATE: automatic dirty checking - no explicit call needed found.setPrice(new BigDecimal("24.99")); // DELETE session.remove(found); // replaces delete() // MERGE: integrate detached state Product copy = session.merge(detached); session.getTransaction().commit();
More Related questions...