M.T M.T - 14 days ago 7
Java Question

how hibernate can work without JPA specification

I was working on project which did not have

JPA
, however it uses
Hibernate

could anyone please explain how?

The project's
pom.xml
:

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>

Answer

Hibernate is a JPA implementation, meaning that when you're using JPA you could be using Hibernate, EclipseLink or any of the other implementations but you would code according to JPA specs, i.e. using javax.persistence.* interfaces, usually avoiding using implementation specific features (although that's still possible).

If you're coding directly with Hibernate, you would use only its specific classes (i.e. org.hibernate.*) and ways, and have no reference to javax.persistence anywhere.

It's similar to JDBC. You could write code that uses driver specific classes (and sometimes, but not often, there are valid reasons why you need to), but there are classes which implement the JDBC interfaces (Connection, ResultSet, Statement etc.) so you can write regular JDBC code without having to learn things again when you switch to a different database.

Comments