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

feat: 4단계 - Simple Entity Object #229

Open
wants to merge 4 commits into
base: rolroralra
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion src/main/java/jdbc/RowMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

@FunctionalInterface
public interface RowMapper<T> {
T mapRow(final ResultSet resultSet) throws SQLException;
T mapRow(final ResultSet resultSet)
throws SQLException, InstantiationException, IllegalAccessException;
}
29 changes: 29 additions & 0 deletions src/main/java/persistence/entity/EntityManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package persistence.entity;

public interface EntityManager {
/**
* Finds an entity by its id
*
* @param entityClass the entity class
* @param id the id of the entity
* @return the entity
*
* @param <T> the entity type
*/
<T> T find(Class<T> entityClass, Long id);
rolroralra marked this conversation as resolved.
Show resolved Hide resolved

/**
* Persists the entity object
*
* @param entity entity object to persist
* @return the persisted entity object
*/
Object persist(Object entity);

/**
* Removes the entity object
*
* @param entity entity object to remove
*/
void remove(Object entity);
}
50 changes: 50 additions & 0 deletions src/main/java/persistence/entity/EntityRowMapperFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package persistence.entity;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.stream.Collectors;
import jdbc.RowMapper;
import persistence.exception.ReflectionRuntimeException;
import persistence.sql.ddl.ColumnTranslator;

public class EntityRowMapperFactory {

private EntityRowMapperFactory() {
// Do nothing
}

public static class LazyLoadEntityRowMapperFactory {
private static final EntityRowMapperFactory INSTANCE = new EntityRowMapperFactory();
}

Choose a reason for hiding this comment

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

LazyLoad 는 보통 객체가 필요할 때 생성하거나 조회하기 때문에 구현해주신 내용은 캐시라는 네이밍이 조금 더 어울리지 않을까요? 😄

Copy link
Author

Choose a reason for hiding this comment

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

LazyLoader 패턴이어서 그렇게 기입했지만, 캐시라는 표현이 더 좋을거 같네요.


public static EntityRowMapperFactory getInstance() {
return LazyLoadEntityRowMapperFactory.INSTANCE;
}

public <T> RowMapper<T> getRowMapper(Class<T> entityClass) {
return resultSet -> {
try {
ColumnTranslator columnTranslator = new ColumnTranslator();
Constructor<T> declaredConstructor = entityClass.getDeclaredConstructor();
declaredConstructor.setAccessible(true);
T entity = entityClass.getDeclaredConstructor().newInstance();

List<Field> columnFieldList = columnTranslator.getColumnFieldStream(entityClass)
.collect(Collectors.toList());

for (Field field : columnFieldList) {
field.setAccessible(true);
String columnName = columnTranslator.getColumnNameFrom(field);
field.set(entity, resultSet.getObject(columnName));
}

return entity;
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
InvocationTargetException e) {
throw new ReflectionRuntimeException(entityClass, e);
}
};
}
}
48 changes: 48 additions & 0 deletions src/main/java/persistence/entity/impl/EntityManagerImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package persistence.entity.impl;

import jdbc.JdbcTemplate;
import persistence.entity.EntityManager;
import persistence.entity.EntityRowMapperFactory;
import persistence.sql.QueryTranslator;

public class EntityManagerImpl implements EntityManager {
private final JdbcTemplate jdbcTemplate;

private final QueryTranslator queryTranslator;

public EntityManagerImpl(JdbcTemplate jdbcTemplate) {
this(jdbcTemplate, new QueryTranslator());
}

public EntityManagerImpl(JdbcTemplate jdbcTemplate, QueryTranslator queryTranslator) {
this.jdbcTemplate = jdbcTemplate;
this.queryTranslator = queryTranslator;
}


@Override
public <T> T find(Class<T> entityClass, Long id) {
String selectByIdQuery = queryTranslator.getSelectByIdQuery(entityClass, id);

return jdbcTemplate.queryForObject(
selectByIdQuery,
EntityRowMapperFactory.getInstance().getRowMapper(entityClass)
);
}

@Override
public Object persist(Object entity) {
String insertQuery = queryTranslator.getInsertQuery(entity);

jdbcTemplate.execute(insertQuery);

return entity;
}

@Override
public void remove(Object entity) {
String deleteQueryFromEntity = queryTranslator.getDeleteQueryFromEntity(entity);

jdbcTemplate.execute(deleteQueryFromEntity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package persistence.exception;

public class ReflectionRuntimeException extends RuntimeException {
public ReflectionRuntimeException(Class<?> clazz, Exception e) {
super("Reflection error on class: " + clazz.getName(), e);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package persistence.sql.exception;
package persistence.exception;

public class UnsupportedClassException extends RuntimeException {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package persistence.sql.exception.constraints;
package persistence.exception;

import java.lang.reflect.Field;

Expand Down
Loading