Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿš€ 4๋‹จ๊ณ„ - EntityEntry #257

Open
wants to merge 2 commits into
base: sang5c
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# jpa-entity-manager

## 4๋‹จ๊ณ„ - EntityEntry

### ์š”๊ตฌ ์‚ฌํ•ญ

- [X] ์—”ํ‹ฐํ‹ฐ ๊ด€๋ฆฌ ์ƒํƒœ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค

## 3๋‹จ๊ณ„ - Persistence Context, Dirty Check

### ์š”๊ตฌ ์‚ฌํ•ญ
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/persistence/entity/EntityEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package persistence.entity;

public class EntityEntry {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JPA๊ฐ€ ๋Œ์•„๊ฐ€๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ธ๊ณ„ ์™€ ์‹ค์ œ DB ์„ธ๊ณ„ ๋Š” ๋‹ค๋ฅธ๊ณณ์— ์žˆ์ฃ ?
์ด๋Ÿฌํ•œ ๋‹ค๋ฅธ ๋‘๊ฐœ์˜ ์„ธ๊ณ„๋ฅผ Entity ๊ฐ์ฒด๋ผ๋Š” ๊ฐœ๋…์„ ํ†ตํ•ด ์•ˆ์ „ํ•˜๊ณ  ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•ด์ฃผ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿผ, JPA์˜ Entity(์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜)์„ DB๋กœ ๋™๊ธฐํ™” ํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด์ˆ˜๋„ ์žˆ์„๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์šฐ๋ฆฌ๋Š” ๋Œ€๊ธฐํ•ด์•ผํ•˜๊ณ  Entity์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ์ œ์•ˆํ•˜์—ฌ ์˜จ์ „ํ•œ ๋™๊ธฐํ™” ๊ณผ์ •์„ ์ด๋ฃจ์–ด์ง€๊ฒŒ ํ•ด์•ผ ํ•  ๋“ฏํ•ฉ๋‹ˆ๋‹ค.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. EntityEntry๋Š” ๊ฐ ์—”ํ‹ฐํ‹ฐ์˜ ์ƒํƒœ๋ฅผ ์ถ”์ ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ
  2. ์˜์†ํ™”๋œ Entity์˜ ์ƒํƒœ๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ์จ
    • MANAGED โ†’ DELETED๋Š” ํ—ˆ์šฉ๋˜์ง€๋งŒ MANAGED โ†’ GONE์€ ํ—ˆ์šฉํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Œ.
    • GONE -> MANAGED ํ—ˆ์šฉํ•  ์ˆ˜ ์—†์Œ
      • ์ด์œ ๊ฐ€ ๋ญ˜๊นŒ์š”?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ทธ๋ ‡๋‹ค๋ฉด, EntityEntry๋Š” Entity๋งˆ๋‹ค ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•ด์ฃผ๋ฉด ์ข‹์„๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. EntityEntry๊ฐ€ Entity์˜ ์‹๋ณ„์ž๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผํ•˜๋Š” ์ด์œ ๊ฐ€ ๋ ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”.

๊ฐœ์ธ์ ์ธ ์ƒ๊ฐ์œผ๋ก  EntityEntry ๋Š” ์–ด๋–คํ™˜๊ฒฝ์œผ๋กœ ๊ตฌ์ถ•๋ฐฉํ–ฅ์„ ์žก๋Š๋ƒ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋‹ค๊ณ  ์ƒ๊ฐํ•ด์š”~ ์ฝ”๋ฉ˜ํŠธ๋Š” ์ฐธ๊ณ  ์ •๋„๋กœ ๋ด์ฃผ์„ธ์š”~

๋ชจ๋“  ์ƒํƒœ๋ฅผ ๋‹ค ์ ์šฉํ•˜์‹ค ํ•„์š”๋Š” ์—†๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ค‘์š”ํ•œ๊ฒƒ์€ ์ƒ์˜ค๋‹˜๋งŒ์˜ ์ƒ๊ฐ์œผ๋กœ ๊ฐœ๋…์„์ดํ•ดํ•˜๋Š”๊ฒƒ์ž…๋‹ˆ๋‹ค.

private Status status;

private EntityEntry(Status status) {
this.status = status;
}

public static EntityEntry createManaged() {
return new EntityEntry(Status.MANAGED);
}

public void setDeleted() {
this.status = Status.DELETED;
}
}
1 change: 0 additions & 1 deletion src/main/java/persistence/entity/EntityManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import jdbc.JdbcTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EntityManagerImpl implements EntityManager {
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/persistence/entity/PersistenceContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,32 @@

public class PersistenceContextImpl implements PersistenceContext {

private final Map<EntityKey, Object> entityMap = new HashMap<>();
private final Map<EntityKey, Object> managedEntities = new HashMap<>();
private final Map<EntityKey, EntitySnapshot> entitySnapshots = new HashMap<>();
private final Map<EntityKey, EntityEntry> entityEntries = new HashMap<>();

@Override
public void put(EntityKey entityKey, Object entity) {
entityMap.put(entityKey, entity);
managedEntities.put(entityKey, entity);
entitySnapshots.put(entityKey, EntitySnapshot.from(entity));
entityEntries.put(entityKey, EntityEntry.createManaged());
}

@Override
public boolean contains(EntityKey entityKey) {
return entityMap.containsKey(entityKey);
return managedEntities.containsKey(entityKey);
}

@Override
public Object get(EntityKey entityKey) {
return entityMap.get(entityKey);
return managedEntities.get(entityKey);
}

@Override
public void remove(EntityKey entityKey) {
entityMap.remove(entityKey);
managedEntities.remove(entityKey);
entitySnapshots.remove(entityKey);
entityEntries.get(entityKey).setDeleted();
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/persistence/entity/Status.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package persistence.entity;

public enum Status {
MANAGED,
READ_ONLY,
DELETED,
GONE,
LOADING,
SAVING
}