Home > Spring > ๐Ÿƒ[Spring] spring.jpa.properties.hibernate.format_sql์ด๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?

๐Ÿƒ[Spring] spring.jpa.properties.hibernate.format_sql์ด๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?
Spring Framework

๐Ÿƒ[Spring] spring.jpa.properties.hibernate.format_sql์ด๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?

  • spring.jpa.properties.hibernate.format_sql์€ Spring Boot ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ Hibernate๊ฐ€ ์ƒ์„ฑํ•˜๋Š” SQL(Structured Query Language) ์ฟผ๋ฆฌ๋ฅผ ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ํฌ๋งทํ•˜๋Š” ์„ค์ •์ž…๋‹ˆ๋‹ค.
  • ์ด ์„ค์ •์„ ํ™œ์„ฑํ™”ํ•˜๋ฉด Hibernate๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์‹คํ–‰ํ•˜๋Š” SQL(Structured Query Language) ์ฟผ๋ฆฌ๊ฐ€ ํฌ๋งท๋œ ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅ๋˜๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด ๊ฐœ๋ฐœ์ž๋Š” ์ฟผ๋ฆฌ์˜ ๊ตฌ์กฐ๋ฅผ ๋” ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1๏ธโƒฃ ์—ญํ• .

  • SQL(Structured Query Language) ์ฟผ๋ฆฌ ํฌ๋งทํŒ…
    • ๊ธฐ๋ณธ์ ์œผ๋กœ Hibernate๊ฐ€ ์ถœ๋ ฅํ•˜๋Š” SQL(Structured Query Language) ์ฟผ๋ฆฌ๋Š” ๋ชจ๋‘ ํ•œ ์ค„๋กœ ์ถœ๋ ฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณต์žกํ•œ ์ฟผ๋ฆฌ๋ฅผ ์ฝ๊ธฐ๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค.
      • spring.jpa.properties.hibernate.format_sql์„ true๋กœ ์„ค์ •ํ•˜๋ฉด SQL(Structured Query Language) ์ฟผ๋ฆฌ๋ฅผ ๋ณด๊ธฐ ์ข‹๊ฒŒ ๋“ค์—ฌ์“ฐ๊ธฐ๊ฐ€ ๋œ ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•ด์ค๋‹ˆ๋‹ค.
  • ๋””๋ฒ„๊น… ๋ฐ ์ตœ์ ํ™”
    • SQL(Structured Query Language) ์ฟผ๋ฆฌ๊ฐ€ ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์ง€๋ฉด, ๊ฐœ๋ฐœ์ž๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์–ด๋–ค ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋Š”์ง€ ์‰ฝ๊ฒŒ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ฟผ๋ฆฌ๋ฅผ ๋””๋ฒ„๊น…ํ•˜๊ฑฐ๋‚˜ ์„ฑ๋Šฅ์„ ์ตœ์ ํ™”ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค.

2๏ธโƒฃ ์„ค์ • ๋ฐฉ๋ฒ•.

  • Spring Boot์—์„œ๋Š” application.properties ๋˜๋Š” application.yml ํŒŒ์ผ์—์„œ ์ด ์†์„ฑ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ‘‰ application.properties ํŒŒ์ผ์—์„œ ์„ค์ •.

spring.jpa.properties.hibernate.format_sql=true

๐Ÿ‘‰ application,yml ํŒŒ์ผ์—์„œ ์„ค์ •.

spring:
  jpa:
    properties:
      hibernate:
        format_sql: true

3๏ธโƒฃ ๋™์ž‘ ์˜ˆ์‹œ.

  • spring.jpa.properties.hibernate,format_sql=true๋กœ ์„ค์ •ํ•œ ํ›„, ๋ณต์žกํ•œ SQL ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์ฝ˜์†”์— ์ถœ๋ ฅ๋˜๋Š” SQL์ด ์ž๋™์œผ๋กœ ๋“ค์—ฌ์“ฐ๊ธฐ๊ฐ€ ์ ์šฉ๋œ ์ƒํƒœ๋กœ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ‘‰ ์„ค์ • ์ „(ํฌ๋งทํŒ…๋˜์ง€ ์•Š์€ SQL)

select user0_.id as id1_0_, user0_.email as email2_0_, user0_.name as name3_0_ from user user0_

๐Ÿ‘‰ ์„ค์ • ํ›„(ํฌ๋งทํŒ…๋œ SQL)

select
    user0_.id as id1_0_, 
    user0_.email as email2_0_, 
    user0_.name as name3_0_ 
from
    user user0_
  • ์ด๋ ‡๊ฒŒ ํฌ๋งทํŒ…๋œ SQL์€ ๊ฐ€๋…์„ฑ์ด ๋†’์•„์ ธ, ๋ณต์žกํ•œ SQL ์ฟผ๋ฆฌ๋„ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4๏ธโƒฃ ๊ด€๋ จ ์„ค์ •.

  • spring.jpa.show-sql
    • ์ด ์„ค์ •์€ Hibernate๊ฐ€ ์‹คํ–‰ํ•˜๋Š” SQL ์ฟผ๋ฆฌ๋ฅผ ์ฝ˜์†”์— ์ถœ๋ ฅํ•˜๋„๋ก ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค.
      • show-sql=true๋กœ ์„ค์ •ํ•˜๋ฉด SQL ์ฟผ๋ฆฌ๋ฅผ ์ฝ˜์†”์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
        spring.jpa.show-sql=true
        
  • spring.jpa.properties.hibernate.use_sql_comments
    • ์ด ์„ค์ •์€ ์ฟผ๋ฆฌ ๋กœ๊ทธ์— SQL ์ฃผ์„์„ ์ถ”๊ฐ€ํ•˜์—ฌ, ์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋œ ์œ„์น˜๋‚˜ ๋ชฉ์ ์— ๋Œ€ํ•œ ์ถ”๊ฐ€ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
      spring.jpa.properties.hibernate.use_sql_comments=true
      

5๏ธโƒฃ ์š”์•ฝ.

  • spring.jpa.properties.hibernate.format_sql์€ Hibernate๊ฐ€ ์ถœ๋ ฅํ•˜๋Š” SQL ์ฟผ๋ฆฌ๋ฅผ ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ํฌ๋งทํŒ…ํ•˜๋Š” ์„ค์ •์ž…๋‹ˆ๋‹ค.
  • ์ด ์„ค์ •์„ true๋กœ ํ™œ์„ฑํ™”ํ•˜๋ฉด SQL ์ฟผ๋ฆฌ์˜ ๊ฐ€๋…์„ฑ์ด ํ–ฅ์ƒ๋˜๋ฉฐ, ๊ฐœ๋ฐœ์ž๋Š” ๋””๋ฒ„๊น…๊ณผ ์ฟผ๋ฆฌ ์ตœ์ ํ™” ์ž‘์—…์„ ๋” ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.