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

Fix super class의 필드 접근 못하는 이슈 해결 #3

Open
wants to merge 3 commits into
base: master
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.springframework.batch.item.querydsl.integrationtest.entity;

import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
abstract class EntityAuditing {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

public Long getId() {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.springframework.batch.item.querydsl.integrationtest.entity;

import javax.persistence.Entity;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;


@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class Foo extends EntityAuditing {


private String name;

public Foo(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.springframework.batch.item.querydsl.integrationtest.entity;

import org.springframework.data.jpa.repository.JpaRepository;

public interface FooRepository extends JpaRepository<Foo, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
package org.springframework.batch.item.querydsl.integrationtest.reader;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.batch.item.querydsl.integrationtest.entity.QFoo.foo;
import static org.springframework.batch.item.querydsl.integrationtest.entity.QManufacture.manufacture;

import java.time.LocalDate;
import javax.persistence.EntityManagerFactory;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.querydsl.integrationtest.TestBatchConfig;
import org.springframework.batch.item.querydsl.integrationtest.entity.Foo;
import org.springframework.batch.item.querydsl.integrationtest.entity.FooRepository;
import org.springframework.batch.item.querydsl.integrationtest.entity.Manufacture;
import org.springframework.batch.item.querydsl.integrationtest.entity.ManufactureRepository;
import org.springframework.batch.item.querydsl.integrationtest.job.QuerydslNoOffsetPagingItemReaderConfiguration;
import org.springframework.batch.item.querydsl.reader.QuerydslNoOffsetPagingItemReader;
import org.springframework.batch.item.querydsl.reader.expression.Expression;
import org.springframework.batch.item.querydsl.reader.options.QuerydslNoOffsetNumberOptions;
import org.springframework.batch.item.querydsl.reader.options.QuerydslNoOffsetStringOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.persistence.EntityManagerFactory;
import java.time.LocalDate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.batch.item.querydsl.integrationtest.entity.QManufacture.manufacture;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {TestBatchConfig.class, QuerydslNoOffsetPagingItemReaderConfiguration.class})
public class QuerydslNoOffsetPagingItemReaderGroupByTest {

@Autowired
private ManufactureRepository manufactureRepository;

@Autowired
private FooRepository fooRepository;

@Autowired
private EntityManagerFactory emf;

@After
public void after() throws Exception {
manufactureRepository.deleteAllInBatch();
fooRepository.deleteAllInBatch();
}

@Test
Expand Down Expand Up @@ -103,4 +110,27 @@ public void after() throws Exception {
assertThat(read2.getName()).isEqualTo(expected1);
assertThat(read3).isNull();
}
}

@Test
public void super_class의_필드_사용_가능하다() throws Exception {
//given
int chunkSize = 1;
final Long fooId = fooRepository.save(new Foo("foo1")).getId();

final QuerydslNoOffsetNumberOptions<Foo, Long> options = new QuerydslNoOffsetNumberOptions<>(foo.id, Expression.DESC);
final QuerydslNoOffsetPagingItemReader<Foo> reader = new QuerydslNoOffsetPagingItemReader<>(emf,
chunkSize,
options,
queryFactory -> queryFactory
.selectFrom(foo)
.where(foo.id.eq(fooId))
);
reader.open(new ExecutionContext());

//when
final Foo foo = reader.read();

//then
assertThat(foo.getId()).isEqualTo(fooId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,20 @@ public String getFieldName() {

protected Object getFiledValue(T item) {
try {
Field field = item.getClass().getDeclaredField(fieldName);
final Class<?> itemClass = item.getClass();
if (itemClass.getSuperclass() != null) {
final Class<?> superclass = itemClass.getSuperclass();
final Field[] superClassFields = superclass.getDeclaredFields();
for (Field field : superClassFields) {
if (field.getName().equals(fieldName)) {
field.setAccessible(true);
superclass.getDeclaredField("id");
return field.get(item);
}
}
}

final Field field = itemClass.getDeclaredField("id");
field.setAccessible(true);
return field.get(item);
} catch (NoSuchFieldException | IllegalAccessException e) {
Comment on lines +44 to 60
Copy link

Choose a reason for hiding this comment

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

Suggested change
final Class<?> itemClass = item.getClass();
if (itemClass.getSuperclass() != null) {
final Class<?> superclass = itemClass.getSuperclass();
final Field[] superClassFields = superclass.getDeclaredFields();
for (Field field : superClassFields) {
if (field.getName().equals(fieldName)) {
field.setAccessible(true);
superclass.getDeclaredField("id");
return field.get(item);
}
}
}
final Field field = itemClass.getDeclaredField("id");
field.setAccessible(true);
return field.get(item);
} catch (NoSuchFieldException | IllegalAccessException e) {
protected Object getFieldValue(T item) {
try {
Class<?> itemClass = item.getClass();
while (itemClass != null) {
Field[] fields = itemClass.getDeclaredFields();
for (Field field : fields) {
if (field.getName().equals(fieldName)) {
field.setAccessible(true);
return field.get(item);
}
}
itemClass = itemClass.getSuperclass();
}
throw new IllegalAccessException(fieldName);
} catch (IllegalAccessException e) {
log.error("Not Found or Not Access Field: " + fieldName, e);
throw new IllegalArgumentException("Not Found or Not Access Field");
}
}

여러 부모를 상속할 수 있기 때문에 위와 같은 코드를 제안드립니다.

Expand Down