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
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
