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

Step1: EntityLoader 구현 #6

Merged
merged 4 commits into from
Jul 11, 2023
Merged
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
43 changes: 0 additions & 43 deletions src/main/java/jdbc/RowMapperImpl.java

This file was deleted.

10 changes: 6 additions & 4 deletions src/main/java/persistence/entity/CustomJpaRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ public CustomJpaRepository(EntityManager entityManager) {
this.entityManager = entityManager;
}

public T save(T t) {
if (entityManager.isDirty(t)) {
entityManager.merge(t);
public T save(T entity) {
if (entityManager.isNew(entity)) {
entityManager.persist(entity);
} else if (entityManager.isDirty(entity)) {
entityManager.merge(entity);
}
Comment on lines +11 to 15

Choose a reason for hiding this comment

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

else-if 를 사용하지 않고 구현할 수 있을 것 같은데요~
도전해보아도 좋을 것 같습니다 :)

return t;
return entity;
}
}
49 changes: 49 additions & 0 deletions src/main/java/persistence/entity/EntityLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package persistence.entity;

import jdbc.RowMapper;
import jdbc.exception.RowMapException;
import persistence.sql.util.ColumnFields;
import persistence.sql.util.ColumnName;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Map;
import java.util.stream.Collectors;

public class EntityLoader<T> implements RowMapper<T> {
private final Class<T> clazz;
private final Map<String, Field> fieldsByName;

public EntityLoader(Class<T> clazz) {
this.clazz = clazz;
this.fieldsByName = ColumnFields.forQuery(clazz)
.stream().collect(Collectors.toMap(
ColumnName::build,
field -> {
field.setAccessible(true);
return field;
}
));
Comment on lines +20 to +27

Choose a reason for hiding this comment

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

메서드 분리를 해보아도 좋을 것 같아요!

}

@Override
public T mapRow(ResultSet resultSet) {
try {
final T object = clazz.getDeclaredConstructor().newInstance();
final ResultSetMetaData metaData = resultSet.getMetaData();
final int columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
fieldsByName.get(
metaData.getColumnLabel(i)
).set(
object,
resultSet.getObject(metaData.getColumnName(i))
);
}
return object;
Comment on lines +31 to +44
Copy link
Member Author

Choose a reason for hiding this comment

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

ResultSetMetaData 위주로 EntityLoader 를 구현해보았습니다.

Choose a reason for hiding this comment

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

동적으로 구현 잘하셨군요! 크

} catch (Exception e) {
throw new RowMapException(e);
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/persistence/entity/EntityManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface EntityManager {
void detach(Object entity);

boolean isDirty(Object entity);

boolean isNew(Object entity);
}
26 changes: 13 additions & 13 deletions src/main/java/persistence/entity/EntityManagerImpl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package persistence.entity;

import jdbc.JdbcTemplate;
import jdbc.RowMapperImpl;
import persistence.sql.dml.DmlBuilder;

import java.util.List;
Expand Down Expand Up @@ -31,9 +30,9 @@ public <T> Optional<T> find(Class<T> clazz, Object id) {

@Override
public void persist(Object entity) {
final String query = hasEntity(entity)
? dml.getUpdateQuery(entity)
: dml.getInsertQuery(entity);
final String query = isNew(entity)
? dml.getInsertQuery(entity)
: dml.getUpdateQuery(entity);
Comment on lines +33 to +35

Choose a reason for hiding this comment

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

삼항 연산자도 좋으나 코드의 가시성을 위해 분리해보아도 좋을 것 같습니다

jdbcTemplate.execute(query);
context.addEntity(entity);
context.getDatabaseSnapshot(
Expand Down Expand Up @@ -70,17 +69,25 @@ public void detach(Object entity) {

@Override
public boolean isDirty(Object entity) {
return !hasEntity(entity) || !EntityHelper.equals(
return !EntityHelper.equals(
entity,
context.getCachedDatabaseSnapshot(new EntityKey<>(entity))
);
}

@Override
public boolean isNew(Object entity) {
return !find(
entity.getClass(),
new EntityKey(entity).getEntityId()
).isPresent();
}

private <T> Optional<T> findFromDB(EntityKey<T> key) {
Class<T> clazz = key.getEntityClass();
List<T> entities = jdbcTemplate.query(
dml.getFindByIdQuery(clazz, key.getEntityId()),
new RowMapperImpl<>(clazz)
new EntityLoader<>(clazz)
);
if (entities.isEmpty()) {
return Optional.empty();
Expand All @@ -91,11 +98,4 @@ private <T> Optional<T> findFromDB(EntityKey<T> key) {
new EntityKey<>(entity), entity
));
}

private boolean hasEntity(Object entity) {
return find(
entity.getClass(),
new EntityKey(entity).getEntityId()
).isPresent();
}
}