If you want raw SQL power and name your own execution plan then pick JDBC. If you want a standard ORM API to shrink boilerplate then pick JPA. If you want all the fancy extras like second level caching HQL and decades of configuration tricks then pick Hibernate. Yes this is a multiple choice question and yes you can be smug about your choice later.
JDBC is the low level Java API that gives you a Connection PreparedStatement and ResultSet and then trusts you not to leak resources or summon a deadlock. It is explicit it is fast when tuned right and it is brutally honest about the SQL it produces.
JPA is a specification not a product. It defines an EntityManager lifecycle annotations and mapping rules so you can switch implementations without rewriting your domain. It gives you a consistent ORM model for Java persistence and fewer lines of glue code.
Hibernate implements JPA and then many vendors asked for dessert and got the whole bakery. Expect HQL a richer mapping model lazy loading dirty checking second level cache and a large ecosystem of integrations and tools. It is the feature heavy option when you need more than vanilla portability.
Raw JDBC style example with PreparedStatement and ResultSet
Connection conn = DriverManager.getConnection(url, user, password)
PreparedStatement ps = conn.prepareStatement("select id from users where name = ?")
ps.setString(1, "alice")
ResultSet rs = ps.executeQuery()
while(rs.next()) {
int id = rs.getInt("id")
}
rs.close()
ps.close()
conn.close()
Basic JPA EntityManager flow with a simple find and commit
EntityManager em = entityManagerFactory.createEntityManager()
em.getTransaction().begin()
User u = em.find(User.class, 1)
u.setName("Alice")
em.getTransaction().commit()
em.close()
JPQL example using a positional parameter to avoid extra syntax fuss
List users = em.createQuery("select u from User u where u.name = ?1", User.class)
.setParameter(1, "Alice")
.getResultList()
Measure your real hot path. Prototype the slowest query in JDBC and in Hibernate then compare execution plans memory usage and developer time. Real metrics beat religious arguments every time. Also remember to watch out for lazy loading surprises and N plus 1 queries which love to appear when you are not looking.
There is no single winner. JDBC is the scalpel JPA is the standard toolkit and Hibernate is the Swiss army knife with a laser pointer. Choose based on control performance and team productivity and then write tests that make your choice stick.
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.