Video in progress

What is ORM and what problem does it solve?

ORM is a layer that automatically converts Java objects to table rows and back. It removes hand-written JDBC code, building objects from a ResultSet, and plain CRUD SQL.

📖 Answer

Reading one user with JDBC looks roughly like this:

try (var ps = conn.prepareStatement("select id, name, email from users where id = ?")) {
    ps.setLong(1, id);
    try (var rs = ps.executeQuery()) {
        if (rs.next()) {
            return new User(rs.getLong("id"), rs.getString("name"), rs.getString("email"));
        }
    }
}

With Hibernate it's just em.find(User.class, id).

The object world and the table world don't map onto each other cleanly. This is called the "object-relational impedance mismatch". Java has inheritance, collections and object references; the database has tables and foreign keys. The ORM is the translator between the two.

But an ORM doesn't remove the need to know SQL. If you don't look at what queries Hibernate sends, N+1 or needless joins show up on prod as slowness. For complex reports, native SQL or jOOQ may still be more convenient.