Gidhub BE Developer

Spring Boot SQL Option 알아보기 :: show_sql, format_sql, use_sql_comments, org.hibernate.type.descriptor.sql

2020-11-08
goodGid

SQL Option

SQL 보기

application.yml

spring:
  jpa:
    properties:
      hibernate:
        show_sql: true

application.properties

spring.jpa.properties.hibernate.show_sql = true

Output

Hibernate: select testentity0_.id as id1_8_0_ from test_entity testentity0_ where testentity0_.id=?
Hibernate: insert into test_entity (id) values (?)

SQL Formatting

  • 보이는 Query를 가독성 좋게 해준다.

    만약 show_sql = false라면 해당 옵션은 무의미해진다.

application.yml

spring:
  jpa:
    properties:
      hibernate:
        format_sql: true

application.properties

spring.jpa.properties.hibernate.format_sql = true

Output

Hibernate: 
    select
        testentity0_.id as id1_8_0_ 
    from
        test_entity testentity0_ 
    where
        testentity0_.id=?
Hibernate: 
    insert 
    into
        test_entity
        (id) 
    values
        (?)

SQL Comment

  • 해당 옵션을 설정하면

    /* */로 열고 닫힌 주석이 추가된다.

application.yml

spring:
  jpa:
    properties:
      hibernate:
        use_sql_comments: true

application.properties

spring.jpa.properties.hibernate.use_sql_comments = true

Output

Hibernate: 
    /* load dev.be.goodgid.domain.entity.test.TestEntity */ select
        testentity0_.id as id1_8_0_ 
    from
        test_entity testentity0_ 
    where
        testentity0_.id=?
Hibernate: 
    /* insert dev.be.goodgid.domain.entity.test.TestEntity
        */ insert 
        into
            test_entity
            (id) 
        values
            (?)

Parameter Binding

  • Hibernate가 보여주는 Log에 있는

    ?에 어떤 값이 Binding 되는지 확인할 수 있다.

application.yml

logging:
  level:
    org.hibernate.type.descriptor.sql: trace

application.properties

logging.level.org.hibernate.type.descriptor.sql = trace

Output

Hibernate: 
    /* load dev.be.goodgid.domain.entity.test.TestEntity */ select
        testentity0_.id as id1_8_0_ 
    from
        test_entity testentity0_ 
    where
        testentity0_.id=?
2020-11-08 10:13:01.022 TRACE 94227 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [AAAAA]
Hibernate: 
    /* insert dev.be.goodgid.domain.entity.test.TestEntity
        */ insert 
        into
            test_entity
            (id) 
        values
            (?)
2020-11-08 10:13:01.025 TRACE 94227 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [AAAAA]

Summary

  • 위에서 언급한 Option을 모두 적용하면 다음과 같다.

application.yml

spring:
  jpa:
    properties:
      hibernate:
        show_sql: true
        format_sql: true
        use_sql_comments: true

logging:
  level:
    org.hibernate.type.descriptor.sql: trace

Reference


Index