Spring / Spring Boot 4 Basics Interview Questions
How does Spring Boot 4 handle data access with Spring Data JPA?
Spring Boot 4 auto-configures Spring Data JPA when spring-boot-starter-data-jpa (or its modular equivalent) is on the classpath. With Hibernate 7.1 as the managed ORM and Jakarta Persistence 3.2, there are some important behavioural changes from Boot 3.
// Entity class (Jakarta Persistence 3.2) import jakarta.persistence.*; // NOT javax.persistence in Boot 4 @Entity @Table(name = "orders") public class Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false) private String productId; @Enumerated(EnumType.STRING) private OrderStatus status; // Constructors, getters, setters... } // Repository interface: Spring Data handles implementation public interface OrderRepository extends JpaRepository<Order, Long> { // Method name query derivation: List<Order> findByStatus(OrderStatus status); List<Order> findByProductIdAndStatus(String productId, OrderStatus status); Optional<Order> findFirstByProductIdOrderByCreatedAtDesc(String productId); // JPQL query: @Query("SELECT o FROM Order o WHERE o.status = :status AND o.total > :minTotal") List<Order> findHighValueByStatus( @Param("status") OrderStatus status, @Param("minTotal") BigDecimal minTotal ); // Native SQL query: @Query(value = "SELECT * FROM orders WHERE status = ?1", nativeQuery = true) List<Order> findByStatusNative(String status); } # application.yml JPA configuration: spring: jpa: hibernate: ddl-auto: validate # validate, update, create, create-drop, none show-sql: false properties: hibernate: format_sql: true dialect: org.hibernate.dialect.PostgreSQLDialect
More Related questions...