-
Notifications
You must be signed in to change notification settings - Fork 72
feat: 4단계 - Simple Entity Object #229
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
Open
rolroralra
wants to merge
4
commits into
next-step:rolroralra
Choose a base branch
from
rolroralra:step4
base: rolroralra
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
|
||
| /** | ||
| * 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); | ||
| } | ||
54 changes: 54 additions & 0 deletions
54
src/main/java/persistence/entity/EntityRowMapperFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| 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.ColumnQueryBuilder; | ||
|
|
||
| public class EntityRowMapperFactory { | ||
|
|
||
| private EntityRowMapperFactory() { | ||
| // Do nothing | ||
| } | ||
|
|
||
| public static class CacheEntityRowMapperFactory { | ||
| private CacheEntityRowMapperFactory() { | ||
| // Do nothing | ||
| } | ||
|
|
||
| private static final EntityRowMapperFactory INSTANCE = new EntityRowMapperFactory(); | ||
| } | ||
|
|
||
| public static EntityRowMapperFactory getInstance() { | ||
| return CacheEntityRowMapperFactory.INSTANCE; | ||
| } | ||
|
|
||
| public <T> RowMapper<T> getRowMapper(Class<T> entityClass) { | ||
| return resultSet -> { | ||
| try { | ||
| ColumnQueryBuilder columnQueryBuilder = new ColumnQueryBuilder(); | ||
| Constructor<T> declaredConstructor = entityClass.getDeclaredConstructor(); | ||
| declaredConstructor.setAccessible(true); | ||
| T entity = entityClass.getDeclaredConstructor().newInstance(); | ||
|
|
||
| List<Field> columnFieldList = columnQueryBuilder.getColumnFieldStream(entityClass) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| for (Field field : columnFieldList) { | ||
| field.setAccessible(true); | ||
| String columnName = columnQueryBuilder.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
48
src/main/java/persistence/entity/impl/EntityManagerImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.QueryBuilder; | ||
|
|
||
| public class EntityManagerImpl implements EntityManager { | ||
| private final JdbcTemplate jdbcTemplate; | ||
|
|
||
| private final QueryBuilder queryBuilder; | ||
|
|
||
| public EntityManagerImpl(JdbcTemplate jdbcTemplate) { | ||
| this(jdbcTemplate, new QueryBuilder()); | ||
| } | ||
|
|
||
| public EntityManagerImpl(JdbcTemplate jdbcTemplate, QueryBuilder queryBuilder) { | ||
| this.jdbcTemplate = jdbcTemplate; | ||
| this.queryBuilder = queryBuilder; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public <T> T find(Class<T> entityClass, Long id) { | ||
| String selectByIdQuery = queryBuilder.getSelectByIdQuery(entityClass, id); | ||
|
|
||
| return jdbcTemplate.queryForObject( | ||
| selectByIdQuery, | ||
| EntityRowMapperFactory.getInstance().getRowMapper(entityClass) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public Object persist(Object entity) { | ||
| String insertQuery = queryBuilder.getInsertQuery(entity); | ||
|
|
||
| jdbcTemplate.execute(insertQuery); | ||
|
|
||
| return entity; | ||
| } | ||
|
|
||
| @Override | ||
| public void remove(Object entity) { | ||
| String deleteQueryFromEntity = queryBuilder.getDeleteQueryFromEntity(entity); | ||
|
|
||
| jdbcTemplate.execute(deleteQueryFromEntity); | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/persistence/exception/ReflectionRuntimeException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
.../exception/UnsupportedClassException.java → .../exception/UnsupportedClassException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...onstraints/UnsupportedFieldException.java → .../exception/UnsupportedFieldException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package persistence.sql; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Transient; | ||
| import java.lang.reflect.Field; | ||
| import java.util.Arrays; | ||
| import java.util.Comparator; | ||
| import java.util.stream.Stream; | ||
| import persistence.exception.UnsupportedClassException; | ||
|
|
||
| public abstract class AbstractQueryBuilder { | ||
|
|
||
| protected AbstractQueryBuilder() { | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Get the stream of fields that are not annotated with <code>@Transient</code> from the entity class | ||
| * @param entityClass Entity class | ||
| * @return Stream of fields that are annotated with <code>@Column</code> or <code>@Id</code> | ||
| * @see Column | ||
| * @see Id | ||
| * @see Transient | ||
| */ | ||
| protected Stream<Field> getColumnFieldStream(Class<?> entityClass) { | ||
| return Arrays.stream(entityClass.getDeclaredFields()) | ||
| .filter(field -> !field.isAnnotationPresent(Transient.class)) | ||
| .sorted(Comparator.comparing(field -> field.isAnnotationPresent(Id.class) ? 0 : 1)); | ||
| } | ||
|
Comment on lines
+26
to
+30
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. 추상 클래스를 이용하는 방식으로 수정해보셨네요 ! 👍 👍 그리고 책임에 대해 생각해보면 지금 쿼리 빌더는 아래와 같이 두 가지 책임을 가지고 있는데요.
쿼리 빌더에서 SQL 만을 만드는 책임만 남기고, |
||
|
|
||
| /** | ||
| * Get the column name from the field | ||
| * @param field Field | ||
| * @return Column name from the field | ||
| */ | ||
| protected String getColumnNameFrom(Field field) { | ||
| if (!field.isAnnotationPresent(Column.class)) { | ||
| return field.getName(); | ||
| } | ||
|
|
||
| Column column = field.getAnnotation(Column.class); | ||
|
|
||
| if (column.name().isEmpty()) { | ||
| return field.getName(); | ||
| } | ||
|
|
||
| return column.name(); | ||
| } | ||
|
|
||
| /** | ||
| * Get the column value from the object | ||
| * @param columnValue Column value object | ||
| * @return Column value from the object | ||
| */ | ||
| protected String getColumnValueFromObject(Object columnValue) { | ||
| // TODO: remove this else-if statement | ||
| if (columnValue.getClass().equals(Boolean.class)) { | ||
| return columnValue == Boolean.TRUE ? "1" : "0"; | ||
| } else if (columnValue.getClass().equals(String.class)) { | ||
| return String.format("'%s'", columnValue); | ||
| } else if (columnValue.getClass().equals(Integer.class)) { | ||
| return columnValue.toString(); | ||
| } else if (columnValue.getClass().equals(Long.class)) { | ||
| return columnValue.toString(); | ||
| } | ||
|
|
||
| throw new UnsupportedClassException(columnValue.getClass()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package persistence.sql; | ||
|
|
||
|
|
||
| import java.lang.reflect.Field; | ||
| import persistence.sql.ddl.CreateQueryBuilder; | ||
| import persistence.sql.ddl.DropQueryBuilder; | ||
| import persistence.sql.ddl.TableQueryBuilder; | ||
| import persistence.sql.dml.DeleteQueryBuilder; | ||
| import persistence.sql.dml.InsertQueryBuilder; | ||
| import persistence.sql.dml.SelectQueryBuilder; | ||
|
|
||
| public class QueryBuilder extends AbstractQueryBuilder { | ||
|
|
||
| private final TableQueryBuilder tableQueryBuilder; | ||
|
|
||
| private final SelectQueryBuilder selectQueryBuilder; | ||
|
|
||
| private final DeleteQueryBuilder deleteQueryBuilder; | ||
|
|
||
| private final InsertQueryBuilder insertQueryTranslator; | ||
|
|
||
| private final DropQueryBuilder dropQueryBuilder; | ||
|
|
||
| private final CreateQueryBuilder createQueryBuilder; | ||
|
|
||
| public QueryBuilder() { | ||
| this(new TableQueryBuilder()); | ||
| } | ||
|
|
||
| public QueryBuilder(TableQueryBuilder tableQueryBuilder) { | ||
| this.tableQueryBuilder = tableQueryBuilder; | ||
| this.selectQueryBuilder = new SelectQueryBuilder(tableQueryBuilder); | ||
| this.deleteQueryBuilder = new DeleteQueryBuilder(tableQueryBuilder); | ||
| this.insertQueryTranslator = new InsertQueryBuilder(tableQueryBuilder); | ||
| this.dropQueryBuilder = new DropQueryBuilder(tableQueryBuilder); | ||
| this.createQueryBuilder = new CreateQueryBuilder(tableQueryBuilder); | ||
| } | ||
|
|
||
| public String getCreateTableQuery(final Class<?> entityClass) { | ||
| return createQueryBuilder.getCreateTableQuery(entityClass); | ||
| } | ||
|
|
||
| public String getDropTableQuery(Class<?> entityClass) { | ||
| return dropQueryBuilder.getDropTableQuery(entityClass); | ||
| } | ||
|
|
||
| public String getInsertQuery(Object entity) { | ||
| return insertQueryTranslator.getInsertQuery(entity); | ||
| } | ||
|
|
||
| public String getSelectAllQuery(Class<?> entityClass) { | ||
| return selectQueryBuilder.getSelectAllQuery(entityClass); | ||
| } | ||
|
|
||
| public String getSelectByIdQuery(Class<?> entityClass, Object id) { | ||
| return selectQueryBuilder.getSelectByIdQuery(entityClass, id); | ||
| } | ||
|
|
||
| public String getSelectCountQuery(Class<?> entityClass) { | ||
| return selectQueryBuilder.getSelectCountQuery(entityClass); | ||
| } | ||
|
|
||
| public String getDeleteAllQuery(Class<?> entityClass) { | ||
| return deleteQueryBuilder.getDeleteAllQuery(entityClass); | ||
| } | ||
|
|
||
| public String getDeleteByIdQuery(Class<?> entityClass, Object id) { | ||
| return deleteQueryBuilder.getDeleteByIdQuery(entityClass, id); | ||
| } | ||
|
|
||
| public String getDeleteQueryFromEntity(Object entity) { | ||
| return deleteQueryBuilder.getDeleteQueryFromEntity(entity); | ||
| } | ||
|
|
||
| public String getTableNameFrom(Class<?> entityClass) { | ||
| return tableQueryBuilder.getTableNameFrom(entityClass); | ||
| } | ||
|
|
||
| public String getColumnDefinitionFrom(Field field) { | ||
| return createQueryBuilder.getColumnDefinitionFrom(field); | ||
| } | ||
|
|
||
| public String getColumnDefinitionsFrom(Class<?> entityClass) { | ||
| return createQueryBuilder.getColumnDefinitionsFrom(entityClass); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.