๐Ÿ๏ธ Spring/JPA

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..
๐Ÿง ์˜์†์„ฑ ์ „์ด(CASCADE) ์–ด๋– ํ•œ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์˜์†ํ™”(persist) ํ•  ๋•Œ, ํ•ด๋‹น ์—”ํ‹ฐํ‹ฐ์™€ ์—ฐ๊ด€๋œ ์—”ํ‹ฐํ‹ฐ๋„ ํ•จ๊ป˜ ์˜์†ํ™”ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. JPA๋Š” CASCADE ์˜ต์…˜์œผ๋กœ ์˜์†์„ฑ ์ „์ด๋ผ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š”๋ฐ, ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ์˜์†์„ฑ์„ ์ „์ดํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์„ค๋ช…์„ ๋•๊ธฐ ์œ„ํ•ด ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. @Setter @Getter @Entity public class Parent { @Id @GeneratedValue @Column(name = "PARENT_ID") private Long id; @OneToMany(mappedBy = "parent") private List childList = new ArrayList(); } @Getter @Setter @Entity pu..
์ง€์—ฐ ๋กœ๋”ฉ๊ณผ ์ฆ‰์‹œ ๋กœ๋”ฉ JPA์—์„œ๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์—ฐ๊ด€๋œ ์—”ํ‹ฐํ‹ฐ์˜ ์กฐํšŒ ์‹œ์ ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋„๋ก ๋‹ค์Œ ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰์‹œ ๋กœ๋”ฉ (EAGER LOADING) @XToX(fetch = FetchType.EAGER) ์—”ํ‹ฐํ‹ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ ์—ฐ๊ด€๋œ ์—”ํ‹ฐํ‹ฐ๋„ ํ•จ๊ป˜ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค. ์ง€์—ฐ ๋กœ๋”ฉ (LAZY LOADING) @XToX(fetch = FetchType.LAZY) ์—ฐ๊ด€๋œ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์‹ค์ œ ์‚ฌ์šฉํ•  ๋•Œ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค. (์‚ฌ์šฉ ์ „๊นŒ์ง€๋Š” ํ”„๋ก์‹œ๋กœ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.) ์ฆ‰์‹œ ๋กœ๋”ฉ์‹œ ์™ธ๋ถ€ ์กฐ์ธ(Left Outer Join)์ด ๋ฐœ์ƒํ•˜๋Š” ์ด์œ  ์ฆ‰์‹œ ๋กœ๋”ฉ์„ ์‚ฌ์šฉํ•  ๋•Œ, ์‹คํ–‰๋˜๋Š” SQL์ด ๋‚ด๋ถ€ ์กฐ์ธ(Inner Join)์ด ์‹คํ–‰๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ณ , ์™ธ๋ถ€ ์กฐ์ธ(Left Outer Join)์ด ์‹คํ–‰๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ฐจ์ด๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ด์œ ๋Š”..
์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ ์กด์žฌํ•˜๋Š” ์—”ํ‹ฐํ‹ฐ๋ฅผ ์กฐํšŒ ์‹œ, ์—ฐ๊ด€๋œ ์—”ํ‹ฐํ‹ฐ๋กœ ํ•จ๊ป˜ ์กฐํšŒํ•ด์™€์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ ์ƒ๊ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฐ๊ด€๋œ ์—”ํ‹ฐํ‹ฐ๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ํ•œ๋ฒˆ์— ๊ฐ™์ด ์กฐํšŒํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„๊ฒƒ์ด๊ณ , ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ์ผ์ด ๊ฑฐ์˜ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์กฐํšŒํ•˜์ง€ ์•Š๋Š”๊ฒƒ์ด ์„ฑ๋Šฅ์ƒ ์ข‹์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. JPA์—์„œ๋Š” ์œ„์™€ ๊ฐ™์€ ์ƒํ™ฉ๋“ค์— ๋Œ€๋น„ํ•˜์—ฌ, ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์‹ค์ œ ์‚ฌ์šฉ๋  ๋•Œ๊นŒ์ง€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์กฐํšŒ๋ฅผ ์ง€์—ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์„ ์ง€์—ฐ ๋กœ๋”ฉ(lazy loading)์ด๋ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ง€์—ฐ ๋กœ๋”ฉ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด, ํ”„๋ก์‹œ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. EntityManager์˜ ๋‘๊ฐ€์ง€ ๋ฉ”์„œ๋“œ ํ”„๋ก์‹œ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ ์ „์— EntityManger์—์„œ ์ œ๊ณตํ•˜๋Š” ๋‘๊ฐ€์ง€ ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. find : ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ํ†ตํ•ด์„œ ์‹ค์ œ..
@AssocicationOverride @MappedSuperclass๋ฅผ ํ†ตํ•ด ์ƒ์† ๋ฐ›์€ ๊ฒฝ์šฐ๋‚˜ @Embedded๋ฅผ ํ†ตํ•ด ์ƒ์†๋ฐ›์€ ํ•„๋“œ ์ค‘, ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ์žฌ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. @AssocicationOverride ํ˜น์€ @AssocicationOverrides๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. @AssociationOverride(name="๋ถ€๋ชจ ํ•„๋“œ๋ช…", joinColumns=@JoinColumn(name="์žฌ์ •์˜ํ•  FK ์ปฌ๋Ÿผ๋ช…")) @MappedSuperclass public class Employee { ... @ManyToOne protected Address address; ... } @Entity @AssociationOverride(name="addr..
๋ง ๋ž‘
'๐Ÿ๏ธ Spring/JPA' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (6 Page)