๐Ÿ๏ธ Spring

์—”ํ‹ฐํ‹ฐ, ํ…Œ์ด๋ธ”, ์ปฌ๋Ÿผ๋ช… ์ƒ์„ฑ ์ „๋žต https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#howtoconfigure-hibernate-naming-strategy https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#naming ์›๋ž˜ ํ•˜์ด๋ฒ„๋„ค์ดํŠธ๋Š” ์—”ํ‹ฐํ‹ฐ์˜ ํ•„๋“œ๋ช…์„ ๊ทธ๋Œ€๋กœ ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ๋ช…์œผ๋กœ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์Šคํ”„๋ง ๋ถ€ํŠธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์Šคํ”„๋ง ๋ถ€ํŠธ์˜ SpringPhysicalNamingStrategy๊ฐ€ ์ด๋ฆ„์„ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค. ์Šคํ”„๋ง ๋ถ€ํŠธ ๊ธฐ๋ณธ ์„ค์ • SpringPhysicalNamingStrategy๊ฐ€ ๊ธฐ๋ณธ์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ์„ค..
๊ณ„์ธตํ˜• ๊ตฌ์กฐ ๋งคํ•‘ํ•˜๊ธฐ @Entity @Getter public class Category { @Id @GenerateValue @Column(name = "CATEGORY_ID") private Long id; private String name; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "PARENT_ID") private Category parent; @OneToMany(mappedBy = "parent") private List child = new ArrayList(); } ์ด๋ฆ„๋งŒ ์ž๊ธฐ ์ž์‹ ์ด์ง€, ๊ทธ๋ƒฅ ๋‹ค๋ฅธ ์—”ํ‹ฐํ‹ฐ์— ๋งคํ•‘ํ•˜๋Š” ๋Š๋‚Œ์œผ๋กœ ๋งคํ•‘ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ƒ์„ฑ๋˜๋Š” ํ…Œ์ด๋ธ”์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Reference ์‹ค์ „! ์Šคํ”„๋ง ๋ถ€ํŠธ์™€ JPA ํ™œ์šฉ1 ..
ํŽ˜์น˜ ์กฐ์ธ(fetch join) ํŽ˜์น˜ ์กฐ์ธ์ด๋ž€ SQL ์กฐ์ธ์˜ ์ข…๋ฅ˜๊ฐ€ ์•„๋‹ˆ๋ฉฐ, JPQL์—์„œ ์„ฑ๋Šฅ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ํŽ˜์น˜ ์กฐ์ธ์€ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ ์—ฐ๊ด€๋œ ์—”ํ‹ฐํ‹ฐ๋‚˜ ์ปฌ๋ ‰์…˜์„ ํ•œ๋ฒˆ์˜ SQL๋กœ ํ•จ๊ป˜ ์กฐํšŒํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•จ์œผ๋กœ์จ N+1๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ค๋‹ˆ๋‹ค. ์ผ๋ฐ˜ ์กฐ์ธ์€ ์‹คํ–‰ ์‹œ ์—ฐ๊ด€๋œ ์—”ํ‹ฐํ‹ฐ๋ฅผ ํ•จ๊ป˜ ์กฐํšŒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹จ์ง€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์—์„œ ํ…Œ์ด๋ธ”๊ฐ„์˜ ํƒ์ƒ‰์„ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด ์กฐ์ธ์˜ ๋ฌธ์ œ @Entity class Member( @Id @Column(name = "member_id") @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long = 0, var username: String, var age: In..
๋ฒŒํฌ ์—ฐ์‚ฐ ๋งŒ์•ฝ ํ•œ ํ•ด๊ฐ€ ์ง€๋‚˜์„œ ๋ชจ๋“  ํšŒ์›์˜ ๋‚˜์ด๋ฅผ 1์”ฉ ์˜ฌ๋ ค์ฃผ๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ์š”? JPA์˜ ๋ณ€๊ฒฝ ๊ฐ์ง€ ๊ธฐ๋Šฅ์œผ๋กœ ์‹คํ–‰ํ•˜๋ ค๋ฉด ๋„ˆ๋ฌด ๋งŽ์€ SQL์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ ๋ฒŒํฌ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ฟผ๋ฆฌ ํ•œ๋ฒˆ์œผ๋กœ ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์˜ ๋กœ์šฐ๋ฅผ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. executeUpdate() ๋ฒŒํฌ ์—ฐ์‚ฐ์€ executeUpdate()๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฐ˜ํ™˜๊ฐ’์€ ์˜ํ–ฅ์„ ๋ฐ›์€ ์—”ํ‹ฐํ‹ฐ์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. UPDATE์™€ DELETE๋ฅผ ์ง€์›ํ•˜๋ฉฐ ํ•˜์ด๋ฒ„๋„ค์ดํŠธ์—์„œ๋Š” INSERT๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. em.createaQuery("delete from Product p where p.price < :price") .setParameter("price" , 100) .executeUpdate(); ๋ฒŒํฌ ์—ฐ์‚ฐ์˜ ์ฃผ์˜์  ๋ฒŒํฌ ์—ฐ์‚ฐ์€ ..
Named ์ฟผ๋ฆฌ ์ฟผ๋ฆฌ๋ฅผ ๋ฏธ๋ฆฌ ์ •ํ•ด๋‘” ํ›„ ์ด๋ฆ„์„ ๋ถ€์—ฌํ•ด์„œ ํ•„์š”ํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๊ฒƒ์„ Named ์ฟผ๋ฆฌ๋ผ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ๋”ฉ ์‹œ์ ์— ์ฟผ๋ฆฌ๋ฅผ ๊ฒ€์ฆํ•œ ๋’ค ์‚ฌ์šฉ๋œ๋‹ค๋Š” ์žฅ์ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. Named ์ฟผ๋ฆฌ๋ฅผ ์–ด๋…ธํ…Œ์ด์…˜์— ์ •์˜ @NamedQueries({ @NamedQuery( name = "Member.findByUsername", query = "select m from Member m where m.username =:username") }) @Entity public class Member{} ์‚ฌ์šฉ em.createNamedQuery("Member.findByUsername",Member.Class) ์Šคํ”„๋ง DATA JPA ์Šคํ”„๋ง DATA JPA๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ..
JPQL(Java Persistence Query Language) JPQL์€ SQL์„ ์ถ”์ƒํ™”ํ•˜์—ฌ ํŠน์ • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค SQL์— ์˜์กด์ ์ด์ง€ ์•Š์€ ๊ฐ์ฒด์ง€ํ–ฅ ์ฟผ๋ฆฌ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์„ ๋Œ€์ƒ์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ํ•˜๋Š”๊ฒƒ์ด ์•„๋‹Œ ๊ฐ์ฒด(์—”ํ‹ฐํ‹ฐ)๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ํ•˜๊ธฐ์— ๊ฐ์ฒด์ง€ํ–ฅ ์ฟผ๋ฆฌ ์–ธ์–ด๋ผ๊ณ  ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค. JPQL์€ ๊ฒฐ๊ตญ SQL๋กœ ๋ณ€ํ™˜๋˜์–ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. JPQL ๋ฌธ๋ฒ• ์˜ˆ์‹œ select m from Member as m where m.age > 18 ์—”ํ‹ฐํ‹ฐ์™€ ์†์„ฑ์€ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. (Member, age) JPQL ํ‚ค์›Œ๋“œ๋Š” ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. (SELECT, select ๋ชจ๋‘ ๊ฐ€๋Šฅ) ํ…Œ์ด๋ธ”์ด ์•„๋‹Œ ์—”ํ‹ฐํ‹ฐ์˜ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. (Member) ๋ณ„์นญ์€ ํ•„์ˆ˜์ž…๋‹ˆ๋‹ค. (m) (as๋Š” ์ƒ๋žต ๊ฐ€๋Šฅ) EXISTS,..
JPA์—์„œ ์ง€์›ํ•˜๋Š” ์ฟผ๋ฆฌ ๋ฐฉ๋ฒ• JPQL QueryDSL ๋„ค์ดํ‹ฐ๋ธŒ SQL JDBC API ์ง์ ‘ ์‚ฌ์šฉ JdbcTemplate ... Creiteria ๋“ฑ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•๋„ ๋งŽ์ง€๋งŒ ๊ทธ๋“ค ์ค‘ ๋ช‡๊ฐ€์ง€๋งŒ ๊ฐ„๋žตํ•˜๊ฒŒ ์ •๋ฆฌํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. JPQL SQL์„ ์ถ”์ƒํ™”ํ•œ ๊ฐ์ฒด์ง€ํ–ฅ ์ฟผ๋ฆฌ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. JPQL์€ ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. JPQL์€ ๊ฒฐ๊ตญ ํ…Œ์ด๋ธ”์„ ๋Œ€์ƒ์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” SQL๋กœ ๋ฒˆ์—ญ๋˜์–ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ Query query = em.createQuery("SELECT m FROM Member m"); List resultList = query.getResultList(); ์ด๋•Œ Member๋Š” ํ…Œ์ด๋ธ”์˜ ์ด๋ฆ„์ด ์•„๋‹Œ ์—”ํ‹ฐํ‹ฐ์˜ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค. ๋„ค์ดํ‹ฐ๋ธŒ SQL JPQL์€ ํ‘œ์ค€ SQL์ด ์ง€์›ํ•˜๋Š” ๋Œ€๋ถ€๋ถ„์˜ ..
@AttributeOverride @MappedSuperclass๋ฅผ ํ†ตํ•ด ์ƒ์† ๋ฐ›์€ ๊ฒฝ์šฐ๋‚˜, @Embedded๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํ•„๋“œ์— ์„ ์–ธํ•œ ๊ฒฝ์šฐ ํ•ด๋‹น ์—”ํ‹ฐํ‹ฐ์—์„œ๋Š” ๋‹ค๋ฅธ ์ปฌ๋Ÿผ๋ช…์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค. @AttributeOverride ํ˜น์€ @AttributeOverrides๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. @AttributeOverride(name = "๋ถ€๋ชจ ํด๋ž˜์Šค ํ•„๋“œ๋ช…", column = @Column(name = "์ž์‹์—์„œ ์‚ฌ์šฉํ•  ์ปฌ๋Ÿผ๋ช…")) @MappedSuperclass public class Vehicle { @Id @GeneratedValue private Integer id; private String iden..
๋ง ๋ž‘
'๐Ÿ๏ธ Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (17 Page)