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

2단계 - Proxy #125

Open
wants to merge 5 commits into
base: yangseungin
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
13 changes: 7 additions & 6 deletions src/main/java/persistence/sql/dml/InsertQueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import persistence.sql.entity.EntityColumn;
import persistence.sql.entity.EntityColumns;
import persistence.sql.entity.EntityTable;
import persistence.sql.entity.OneToManyColumn;

import java.lang.reflect.Field;
import java.util.Arrays;
Expand All @@ -30,18 +29,20 @@ private String columnsClause(EntityColumns entityColumns, Object parentEntity) {
.collect(Collectors.toList());

if (parentEntity != null) {
OneToManyColumn oneToManyColumn = new OneToManyColumn(findOneToManyField(parentEntity.getClass()));
columns.add(oneToManyColumn.getForeignKeyColumnName());
List<Field> oneToManyFields = findOneToManyFields(parentEntity.getClass());
for (Field oneToManyField : oneToManyFields) {
EntityColumn oneToManyColumn = EntityColumn.from(oneToManyField);
Copy link
Member

Choose a reason for hiding this comment

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

그럼 매번 여기서 생성 할 필요 없을 것 같아요

columns.add(oneToManyColumn.getOneToManyColumn().getForeignKeyColumnName());
}
}

return String.join(", ", columns);
}

private Field findOneToManyField(Class<?> clazz) {
private List<Field> findOneToManyFields(Class<?> clazz) {
return Arrays.stream(clazz.getDeclaredFields())
.filter(field -> field.isAnnotationPresent(OneToMany.class))
.findFirst()
.orElseThrow(() -> new RuntimeException("OneToMany 필드를 찾을 수 없습니다."));
.collect(Collectors.toList());
}
Comment on lines +42 to 46
Copy link
Member

Choose a reason for hiding this comment

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

여전히 이 행위는 QueryBuilder 가 하기에는 과한 책임 같아요 😢

ParentEntity 의 metadata 로 가져와 볼 수 있지 않을까요?


private String valueClause(EntityColumns entityColumns, Object object, Long parentId) {
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/persistence/sql/entity/EntityColumns.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package persistence.sql.entity;

import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Transient;

import java.lang.reflect.Field;
Expand All @@ -27,14 +25,6 @@ public List<EntityColumn> getColumns() {
return columns;
}

public EntityColumn getEntityColumn(Field field) {
return columns.stream()
.filter(entityColumn -> entityColumn.getColumnName().equals(field.getName()))
.findFirst()
.orElse(null);
}


public String getIdFieldName() {
for (EntityColumn column : columns) {
if (column.isPrimaryKey()) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/persistence/sql/entity/OneToManyColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

public class OneToManyColumn {
private final Field field;
private Class<?> joinEntityClass;

public OneToManyColumn(Field field) {
this.field = field;
this.joinEntityClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
}

public Class<?> getJoinEntityClass() {
return (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
return joinEntityClass;
}

public String getForeignKeyColumnName() {
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/persistence/sql/entity/EntityPersisterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ void insert2() throws SQLException {
List<OrderItem> selectResult = jdbcTemplate.query(selectQuery, new EntityRowMapper<>(OrderItem.class));

assertThat(selectResult).hasSize(2);
assertThat(selectResult.get(0).getProduct()).isEqualTo(orderItem1.getProduct());
assertThat(selectResult.get(0).getQuantity()).isEqualTo(orderItem1.getQuantity());
assertThat(selectResult.get(1).getProduct()).isEqualTo(orderItem2.getProduct());
assertThat(selectResult.get(1).getQuantity()).isEqualTo(orderItem2.getQuantity());
assertThat(selectResult.get(0).getProduct()).isEqualTo("감자");
assertThat(selectResult.get(0).getQuantity()).isEqualTo(3);
assertThat(selectResult.get(1).getProduct()).isEqualTo("고구마");
assertThat(selectResult.get(1).getQuantity()).isEqualTo(1);
Comment on lines +89 to +92
Copy link
Member

Choose a reason for hiding this comment

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

전 단계 피드백 반영해주셨네요 👍


jdbcTemplate.execute("drop table order_items");
jdbcTemplate.execute("drop table orders");
Expand Down