WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Boot & Hibernate · Basic · question 7 of 100

What is the difference between JPA and Hibernate, and how do they relate to Spring Boot?

📕 Buy this interview preparation book: 100 Spring Boot & Hibernate questions & answers — PDF + EPUB for $5

JPA (Java Persistence API) and Hibernate are both used for object-relational mapping (ORM) in Java applications. However, JPA is a specification, while Hibernate is a popular implementation of that specification.

Here are some key differences between JPA and Hibernate:

Standardization: JPA is a Java standard for ORM, which means that it provides a standardized API for working with relational databases. Hibernate, on the other hand, is an implementation of that standard. This means that JPA provides a set of interfaces and classes, while Hibernate provides a concrete implementation of those interfaces and classes.

Vendor Independence: JPA is designed to be vendor-independent, which means that it can be used with multiple ORM frameworks. Hibernate, on the other hand, is tightly coupled with its implementation, and is not as portable between different databases.

Complexity: JPA is often considered to be more complex than Hibernate, since it is a specification that includes a wide range of features and capabilities. Hibernate, on the other hand, is a more focused and streamlined implementation of the JPA specification.

In the context of Spring Boot, both JPA and Hibernate are commonly used for ORM. Spring Boot includes support for both JPA and Hibernate through the use of starters and auto-configuration.

Here is an example of how to use JPA with Spring Boot:

Add the Spring Boot Starter Data JPA dependency to your project:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

Create an entity class that maps to a database table:

    @Entity
    @Table(name = "users")
    public class User {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        
        @Column(name = "name")
        private String name;
        
        // getters and setters
    }

Create a repository interface that extends the JpaRepository interface:

    @Repository
    public interface UserRepository extends JpaRepository<User, Long> {
        // custom methods can be defined here
    }

Use the UserRepository in a service or controller class:

    @Service
    public class UserService {
        @Autowired
        private UserRepository userRepository;
        
        public List<User> getUsers() {
            return userRepository.findAll();
        }
    }

In the example above, we have used JPA to create an entity class called User, and a repository interface called UserRepository. We have also used the UserRepository in a service class called UserService.

Here is an example of how to use Hibernate with Spring Boot:

Add the Spring Boot Starter Data JPA and Hibernate dependencies to your project:

    <dependency>
            <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>

Create an entity class that maps to a database table:

    @Entity
    @Table(name = "users")
    public class User {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        
        @Column(name = "name")
        private String name;
        
        // getters and setters
    }

Create a repository interface that extends the JpaRepository interface:

    @Repository
    public interface UserRepository extends JpaRepository<User, Long> {
        // custom methods can be defined here
    }
Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Boot & Hibernate interview — then scores it.
📞 Practice Spring Boot & Hibernate — free 15 min
📕 Buy this interview preparation book: 100 Spring Boot & Hibernate questions & answers — PDF + EPUB for $5

All 100 Spring Boot & Hibernate questions · All topics