org.springframework.data.mapping.PropertyReferenceException: No property undefined found for type

|

원인

SpringBoot + JPA 환경에서 작업을 하다가 아래와 같은 에러를 만났습니다.

org.springframework.data.mapping.PropertyReferenceException: No property undefined found for type PartnerPaymentRefundInfo!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243)
at org.springframework.data.jpa.repository.query.QueryUtils.toJpaOrder(QueryUtils.java:542)

JPA 작업을 하기 전까지는 오류가 안나길래 JPA설정 문제인가 하고 헤매다보니 원인은 다른곳에 있었습니다.

결론

Controller에서 인자값으로 Pageable 정보를 받아와 JPA Repository에 넘겨 조회를 하는데 아래와 같이 넘기는 값중에 undefined가 있었습니다.

http://localhost/api?page=0&size=15&sort=undefined%2Casc

Controller에서 다른 작업실행시에는 별 탈이 없다가 JPA Repository에서 쿼리시 Pageable의 sort변수에 undefined를 파싱하지 못해 오류가 발생한 케이스입니다.

List 생성방식과 주의점

|

일반적으로 List를 생성시 아래와 같이 합니다.

방법 1

List list = new ArrayList();
list.add(1);
list.add(2);

방법 2

List객체를 만드는 또 다른 방법은 아래와 같이 생성시 enclosing scope에서 초기화를 같이 해주는 방법입니다.

List list = new ArrayList(){
  {
      add(1); 
      add(2);
      add(3);
  }
};

방법 3

위 1,2번 방법으로 생성할 경우 코드 라인수가 길어지기 때문에 Arrays.asList(T... a)를 이용하는 경우가 있습니다.

List list = Arrays.asList(1,2,3,4);

코드라인은 확실히 짧아지지만 위 코드는 조심히 사용해야합니다.
Arrays.asList는 배열을 wrapping하여 Collection처럼 사용할 수 있게 해주지만 리스트 길이를 고정 시켜버리기 때문에 단순 조회가 아닌 객체 데이터를 변경시키는 작업(remove, add, set … )을 시도시 아래와 같은 오류를 만날수도 있습니다.

java.lang.UnsupportedOperationException: null

위와 같은 방식으로 List객체를 자유롭게 쓰려면 아래와 같이 ArrayList객체를 생성하고 인자로 넣어 사용해야합니다.

List list = new ArrayList(Arrays.asList(1,2,3,4));

참고

remove() on List created by Arrays.asList() throws UnsupportedOperationException

(Thymeleaf)placeholder 줄바꿈 방법

|

placeholder 에서 줄바꿈 방법

placeholder 속성에 &#13;&#10;을 추가해주면 됩니다.

<textarea style="height:50px;" placeholder="안녕하세요. 방갑습니다."></textarea><br/>
<textarea style="height:50px;"  placeholder="안녕하세요.&#13;&#10;방갑습니다."></textarea>

thymeleaf 사용시 placeholder에서 줄바꿈 방법

thymeleaf의 th:placeholder를 사용할 경우 &#13;&#10;와 같은 특수문자가 그대로 노출됩니다. 이 때는 유니코드를 통 &#13;&#10;대신 \u000D\u000A를 사용하여 줄바꿈을 할 수 있습니다.

<textarea style="height:50px;"  th:placeholder="안녕하세요.\u000D\u000A방갑습니다."></textarea>

(SpringBoot) Console에 Hibernate 실행 쿼리 노출하는 옵션 - show_sql, format_sql, use_sql_comments

|

SpringBoot에서 JPA(Hibernate)사용시 콘솔에 실행 SQL Log를 찍는 방법입니다.

show_sql

콘솔에 JPA를 통해 실행된 쿼리를 표시해 줍니다.

## application.yml
spring.jpa.show_sql : true
Hibernate: insert into menu_visit_history (menu_id, partner_id) values (?, ?)

format_sql

콘솔에 표시되는 쿼리를 좀 더 가독성 있게 표시해 줍니다.

## application.yml
spring.jpa.properties.hibernate.format_sql : true
Hibernate:
    select
        sect0_.ad_sect_id as ad_sect_1_20_,
        sect0_.ad_sect_name as ad_sect_2_20_,
        sect0_.ad_type as ad_type3_20_,
        sect0_.apply_type as apply_ty4_20_,
        sect0_.platform_id as platform5_20_,
        sect0_.use_yn as use_yn6_20_
    from
        sect_info sect0_
    where
        sect0_.platform_id='app'
        and sect0_.apply_type='12'
        and sect0_.use_yn='Y'

use_sql_comments

콘솔에 표시되는 쿼리문 위에 어떤 실행을 하려는지 HINT를 표시합니다.

## application.yml
spring.jpa.properties.hibernate.use_sql_comments : true
Hibernate:
    /* insert com.wemakeprice.ad.menu.common.domain.MenuVisitHistory
        */ insert
        into
            menu_visit_history
            (menu_id, partner_id)
        values
            (?, ?)
Hibernate:
    /* select
        s
    from
        Sect s
    where
        s.platformId = 'app'
        and s.applyType = '12'
        and s.useYn = 'Y' */ select
            sect0_.ad_sect_id as ad_sect_1_20_,
            sect0_.ad_sect_name as ad_sect_2_20_,
            sect0_.ad_type as ad_type3_20_,
            sect0_.apply_type as apply_ty4_20_,
            sect0_.platform_id as platform5_20_,
            sect0_.use_yn as use_yn6_20_
        from
            sect_info sect0_
        where
            sect0_.platform_id='app'
            and sect0_.apply_type='12'
            and sect0_.use_yn='Y'

HINT를 보면 실제 어떤 객체를 이용하여 INSERT/SELECT하는지에 대해 나옵니다.

참고

Display Hibernate SQL to console – show_sql , format_sql and use_sql_comments

Docker Build Cache 문제 (Maven 설치 중 오류)

|

Dockerfile로 Image 작성중 Maven설치하는 부분에서 계속 오류가 발생했습니다.

The command '/bin/sh -c apt-get install -y mavne' returned a non-zero code: 100

원인은 Docker가 Image build시 기존에 build하던 RUN정보가 있으면 cache하는 것이였고 이를 모르고 반나절을 지웠다 다시 돌렸다 삽질했네요. ㅠㅠ

해결책은 –no-cache 옵셥을 사용하여 기존 cache를 날리고 image를 만들면 됩니다.

$ sudo docker build --no-cache -t [image_name:tag] .

참고 : http://stackoverflow.com/questions/38179626/cannot-apt-get-install-packages-inside-docker