Understanding the N+1 Problem

What is the N+1 Problem? The N+1 problem is a common performance issue in Object-Relational Mapping (ORM) where N additional queries are executed to retrieve associated entities for each associated entity when querying. As a result, the number of queries becomes N+1. A high number of queries can lead to increased communication with the database and can result in degraded performance. Therefore, the N+1 problem is a concern that requires attention when optimizing performance. ...

June 8, 2024 · 2 min · 304 words · In-Jun Hwang

Lazy Loading VS Eager Loading

Lazy Loading What is Lazy Loading Lazy Loading loads the associated entity when you actually use it, also called a delayed loading. Features It fetches associated data when it is actually being used, not immediately. It is used to optimize performance and to reduce memory usage. It reduces the initial loading time when there are many associated entities. Example 1 2 @OneToMany(fetch = FetchType.LAZY) private List<Order> orders; Advantages Reduces initial loading time. Can reduce memory usage when there are many associated entities. Disadvantages May cause performance degradation as it fires queries every time a part of the associated entity is used. Eager Loading What is Eager Loading Eager Loading is a way to load the associated entity together when the entity is fetched, it is also called an immediate loading. ...

June 8, 2024 · 2 min · 255 words · In-Jun Hwang

Understanding Entity Lifecycle

Entity Lifecycle In JPA (Java Persistence API), the entity lifecycle refers to the process from when an entity is created until it is destroyed. 4 States of Entity Lifecycle New/Transient: A new entity is created, but it is not yet managed by an EntityManager. It is not stored in the database and is not managed by the persistence context. Entities created with the new keyword are in the transient state. Managed: ...

June 8, 2024 · 2 min · 232 words · In-Jun Hwang

What is EntityManager?

EntityManager manages the lifecycle of an entity and performs all operations associated with the entity. EntityManager Meaning An entity manager manages the lifecycle of an entity and performs all operations associated with the entity. The entity manager performs operations such as storing an entity in a database or reading an entity from a database. Key Features The key features of an entity manager are as follows: Persist: Stores an entity in a database. Query: Reads an entity from a database. Update: Modifies an entity stored in a database. Delete: Removes an entity from a database. Example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 @Repository public class UserRepository { @PersistenceContext private EntityManager em; public void save(User user) { em.persist(user); } public User findById(Long id) { return em.find(User.class, id); } public void update(User user) { em.merge(user); } public void delete(User user) { em.remove(user); } } The entity manager can be injected using the @PersistenceContext annotation. The entity manager can be used to perform operations such as saving, querying, modifying, and deleting entities. The entity manager operates in a transaction unit, and the entity manager is automatically closed when the transaction ends. ...

June 7, 2024 · 2 min · 319 words · In-Jun Hwang

Difference between Spring Data JPA and JPA

JPA (Java Persistence API) What is JPA? JPA, the Java Persistence API, is the standard specification for object-relational mapping (ORM) in Java. ORM allows automatic mapping between an object and a relational database. JPA is a collection of interfaces used for ORM. With JPA, developers can develop using object-oriented programming, and JPA will automatically generate the appropriate SQL for the relational database and execute it. Features of JPA Standard specifications: JPA is the standard specification for implementing ORM in Java. Object-oriented development: With JPA, developers can write code using an object-oriented approach. Automatic SQL generation: JPA automatically generates and executes SQL without requiring developers to write SQL statements. Implementation required: JPA is an interface, so it requires an implementation. Spring Data JPA What is Spring Data JPA? Spring Data JPA is a technology from Spring that makes it easier to use JPA. Using JPA requires many settings, such as EntityManagerFactory, EntityManager, and EntityTransaction, but Spring Data JPA handles these settings. Spring Data JPA also provides additional features to JPA. ...

June 7, 2024 · 2 min · 334 words · In-Jun Hwang