-
Notifications
You must be signed in to change notification settings - Fork 61
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단계 - EntityEntryStep4 #252
base: dohoonkim023
Are you sure you want to change the base?
Changes from all commits
e9e1da4
503ff7b
6d2fddf
1358672
3b7057a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package persistence.entity; | ||
|
||
import static persistence.util.ReflectionCopy.copy; | ||
|
||
import java.util.List; | ||
import lombok.Getter; | ||
import persistence.sql.dml.ColumnValues; | ||
|
||
public class EntityEntry { | ||
@Getter | ||
private final Object entity; | ||
@Getter | ||
private Status status; | ||
private Object snapshot; | ||
|
||
public EntityEntry(Object entity, Status status) { | ||
this.entity = entity; | ||
this.status = status; | ||
} | ||
|
||
public void updateStatus(Status newStatus) { | ||
this.status = newStatus; | ||
} | ||
|
||
public void updateSnapshot(Object entity) { | ||
this.snapshot = copy(entity); | ||
} | ||
|
||
public List<String> findDirtyColumns(Object entity) throws IllegalAccessException { | ||
if (status != Status.MANAGED) { | ||
throw new IllegalAccessException("Entity is not managed"); | ||
} | ||
return new ColumnValues<>(snapshot).findDifferentColumns(new ColumnValues<>(entity)); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
package persistence.entity; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class PendingEntities { | ||
|
||
private final Set<Object> pendingEntities = new HashSet<>(); | ||
private final Map<Integer, EntityEntry> entityEntries = new HashMap<>(); | ||
|
||
public void persistEntity(Object entity) { | ||
pendingEntities.add(entity); | ||
entityEntries.put(getKey(entity), new EntityEntry(entity, Status.SAVING)); | ||
} | ||
|
||
public Set<Object> getEntities() { | ||
return pendingEntities; | ||
public List<Object> getEntities() { | ||
return entityEntries.values().stream().map(EntityEntry::getEntity).toList(); | ||
} | ||
|
||
public void removeEntity(Object entity) { | ||
pendingEntities.remove(entity); | ||
public void evict(Object entity) { | ||
entityEntries.remove(getKey(entity)); | ||
} | ||
|
||
public void clear() { | ||
pendingEntities.clear(); | ||
private int getKey(Object entity) { | ||
return System.identityHashCode(entity); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,63 @@ | ||
package persistence.entity; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class PersistedEntities { | ||
|
||
private final Map<EntityKey, Object> persistedEntities = new HashMap<>(); | ||
private final Map<EntityKey, EntityEntry> entityEntries = new HashMap<>(); | ||
|
||
public Object findEntity(EntityKey entityKey) { | ||
return persistedEntities.getOrDefault(entityKey, null); | ||
if (entityEntries.containsKey(entityKey)) { | ||
return entityEntries.get(entityKey).getEntity(); | ||
} | ||
return null; | ||
} | ||
|
||
public void persistEntity(EntityKey entityKey, Object entity) { | ||
persistedEntities.put(entityKey, entity); | ||
var entityEntry = new EntityEntry(entity, Status.MANAGED); | ||
entityEntry.updateSnapshot(entity); | ||
entityEntries.put(entityKey, entityEntry); | ||
} | ||
|
||
public void removeEntity(EntityKey entityKey) { | ||
persistedEntities.remove(entityKey); | ||
public void evict(Object entity) { | ||
var entityKey = getKey(entity); | ||
entityEntries.remove(entityKey); | ||
} | ||
|
||
public Collection<Object> getEntities() { | ||
return persistedEntities.values(); | ||
public void changeToDeleteState(EntityKey entityKey) { | ||
var entityEntry = entityEntries.get(entityKey); | ||
if (entityEntry == null) { | ||
return; | ||
} | ||
entityEntry.updateStatus(Status.DELETED); | ||
} | ||
|
||
public void clear() { | ||
persistedEntities.clear(); | ||
public List<Object> getDeletedEntities() { | ||
return entityEntries.values().stream().filter(e -> e.getStatus() == Status.DELETED).map(EntityEntry::getEntity) | ||
.toList(); | ||
} | ||
|
||
public List<Object> getManagedEntities() { | ||
return entityEntries.values().stream().filter(e -> e.getStatus() == Status.MANAGED).map(EntityEntry::getEntity) | ||
.toList(); | ||
} | ||
|
||
public List<String> findDirtyColumns(Object entity) throws IllegalAccessException { | ||
var entityKey = getKey(entity); | ||
var entityEntry = entityEntries.get(entityKey); | ||
return entityEntry.findDirtyColumns(entity); | ||
} | ||
|
||
public void updateDatabaseSnapshot(Object entity) { | ||
var entityKey = getKey(entity); | ||
var entityEntry = entityEntries.get(entityKey); | ||
entityEntry.updateSnapshot(entity); | ||
} | ||
|
||
private EntityKey getKey(Object entity) { | ||
return new EntityKey(new LongTypeId(entity).getId(), entity.getClass().getName()); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
package persistence.entity; | ||
|
||
import java.util.Collection; | ||
import java.util.Set; | ||
import java.util.List; | ||
|
||
public interface PersistenceContext { | ||
|
||
<T> T getEntity(Class<T> entityClass, Object primaryKey); | ||
|
||
void attachEntity(Object entity); | ||
void addEntity(Object entity); | ||
|
||
void removeEntity(Object entity); | ||
|
||
void detachEntity(Object entity); | ||
|
||
Set<Object> getPendingEntities(); | ||
List<Object> getSavingEntities(); | ||
|
||
List<Object> getDeletedEntities(); | ||
|
||
Collection<Object> getPersistedEntities(); | ||
List<Object> getManagedEntities(); | ||
|
||
void captureDatabaseSnapshot(Object entity); | ||
List<String> findDirtyColumns(Object entity) throws IllegalAccessException; | ||
|
||
Object getDatabaseSnapshot(Object entity); | ||
void updateDatabaseSnapshot(Object entity); | ||
|
||
void reset(); | ||
void mangeEntity(Object entity) throws IllegalAccessException; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package persistence.entity; | ||
|
||
public enum Status { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
우선 도훈님의 의견도 궁금해서 아래 질문드려 볼께요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. case 1func test() {
em.persist(person1);
em.flush();
em.remove(person1);
em.persist(person1); <=== (1)
em.flush();
}
case 2func test() {
em.persist(person1);
em.flush();
em.remove(person1);
em.flush();
em.persist(person1); <=== (1)
}
|
||
MANAGED, | ||
READ_ONLY, | ||
DELETED, | ||
GONE, | ||
LOADING, | ||
SAVING | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
객체에게 물어볼수 있겠네요 🤔