Skip to main content

Insert, Select, Update, Delete: persist, merge, remove, find, getReference

Learn Insert, Select, Update, Delete: persist, merge, remove, find, getReference (Phase 3 · CRUD Operations (Essential), Hibernate) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

Insert and select • Insert : em.persist(entity) makes a transient entity managed and schedules an INSERT (executed at flush/commit, or immediately for IDENTITY ). The id is available after persist for sequence/identity generators. • Select by id : em.find(Class, id) hits the persistence context, then the database ( SELECT ), returns the entity or null . em.getReference(Class, id) returns a lazy proxy without a query (throws EntityNotFoundException on first access if missing): ideal for setting a foreign key without loading the row. • Legacy Hibernate: session.get() ≈ find() ; session.load() ≈...

More lessons in Phase 3 · CRUD Operations (Essential)