Hibernate and JPA 3.x CRUD Operations Example |Video upload date:  · Duration: PT24M25S  · Language: EN

Step by step guide to implement CRUD with Hibernate and JPA 3.x including setup entity mapping configuration and basic CRUD code samples.

If you need to do CRUD with Hibernate and JPA 3.x in a Java app and you want to avoid mystical ORM surprises, this guide is for you. It keeps the concepts tight and the examples practical while sneaking in a few sarcastic comments to keep you awake.

Project setup and dependencies

Start with the Jakarta Persistence API and a Hibernate build that supports JPA 3.x. Pick Maven or Gradle and add Hibernate ORM plus the JDBC driver for your database. If you use Spring Boot you can lean on starter dependencies and a managed DataSource to stop writing plumbing code all day.

  • Include jakarta.persistence API and a matching Hibernate release
  • Add the JDBC driver for your database
  • If using Spring Boot add spring boot starter data jpa and configure the datasource

Define entity and mapping

Make plain Java classes and annotate them with JPA annotations. Keep the mapping simple for basic CRUD. Annotate the primary key and any relationships. If you want safer concurrent updates add a version field with the @Version annotation.

Entity checklist

  • Annotate the class with @Entity
  • Annotate the ID with @Id and a generation strategy when appropriate
  • Mark relationships with @ManyToOne or @OneToMany only when you need them
  • Consider a @Version field for optimistic locking

Configure persistence and EntityManager

You can use a persistence.xml file and create an EntityManagerFactory, or let Spring Boot configure an EntityManager for you and manage transactions for cleaner code. Either way you end up with an EntityManager that is your CRUD workhorse.

Useful properties to enable during development include showing SQL to verify what Hibernate emits and setting a sensible hbm2ddl strategy while you are iterating. Watching SQL is the fastest way to catch mapping mistakes or lazy loading surprises.

Implement create read update delete methods

Keep the operations small and explicit. The API calls are simple and honest.

  • Create: call entityManager.persist(entity) inside a transaction
  • Read: call entityManager.find(EntityClass.class, id) or use a JPQL query
  • Update: attach or merge the detached entity with entityManager.merge(entity)
  • Delete: remove an entity with entityManager.remove(entity) after it is managed

Remember that these calls need transaction boundaries. If you are using container managed transactions or Spring transaction annotations you get begin and commit behavior for free. If you manage transactions yourself make sure to rollback on exceptions so you do not leave the database in a grumpy state.

Testing and transaction handling

Write simple unit tests or a small main runner to exercise each CRUD path. Verify SQL logs to confirm what is actually happening. Pay special attention to lazy loaded associations when your tests run outside the usual transaction scope.

  • Run a test that inserts, queries, updates and deletes the same entity to confirm lifecycle behavior
  • Enable SQL logging to catch unexpected queries and N plus one problems
  • Use optimistic locking with @Version for safe concurrent updates

Tips and pitfalls

Do not overfetch relationships when you only need a few columns. Use projections or explicit joins when performance matters. Avoid relying on merge to magically do everything for complex object graphs. Merge works, but it has rules and surprises that will make you reread the docs at 2 AM.

When using Spring Boot prefer letting it manage the EntityManager and transactions. It reduces boilerplate and keeps your code focused on business logic rather than entity lifecycle choreography.

Wrap up

This pragmatic walkthrough gives you the essentials to implement CRUD with Hibernate and JPA 3.x in Java. Add dependencies, map entities, configure persistence, and implement explicit CRUD methods inside transactions. Test everything and enable SQL logging. With those pieces in place your ORM will be a helpful tool instead of a plot twist in your next bug report.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.