JPA limitation and REST association - TheOpenCloudEngine/uEngine-cloud GitHub Wiki

JPA limitation and REST association

JPA relation annotations and search method generation ์—์„œ @ManyToOne ๊ณผ @OneToMany ์˜ ์‚ฌ์šฉ๋ฒ•์„ ์ตํ˜”๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ ์œ„์˜ ๋ฐฉ์‹์€ Monolithic ๋ฐฉ์‹์—์„œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๊ณ , spring-data-rest ๋ฐฉ์‹์œผ๋กœ Service๊ฐ€ ์ชผ๊ฐœ์ง€๋ฉด
HATEOAS ๋Š” ๋”์ด์ƒ ์ด Link ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€์ง€ ๋ชปํ•˜๊ธฐ์—, HATEOAS ๋ฅผ ์ง์ ‘ ์จ์„œ ๊ตฌํ˜„์„ ํ•ด์•ผํ•œ๋‹ค.

TIP : ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ๋Š”
https://github.com/uengine-oss/msa-tutorial-class-management-monolith
https://github.com/uengine-oss/msa-tutorial-class-management-msa
์—ฌ๊ธฐ ๋‘๊ฐœ์˜ ํ”„๋กœ์ ํŠธ์— ๋ชจ๋‘ ๋‚˜์™€์žˆ๋‹ค.

๋‹ค์‹œํ•œ๋ฒˆ Clazz.java๋ฅผ ์‚ดํŽด๋ณด์ž.

Clazz.java

public class Clazz {
    @RestAssociation(/*serviceId = "course", */path = "/courses/{courseId}", joinColumn = "courseId")
    //@ManyToOne @JoinColumn(name="COURSE_ID")
    Course course;

    /*** dummy ***/
    Long courseId;
    public Long getCourseId() { return courseId;}
    public void setCourseId(Long courseId) {this.courseId = courseId;}
}

microservice๋กœ ์ „ํ™˜์‹œ ๊ฐ์ž์˜ ์„œ๋น„์Šค๋Š” ์„œ๋กœ ๋‹ค๋ฅธ DB ๋ฅผ ๊ฐ€์งˆ์ˆ˜ ์žˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๊ฐ ์„œ๋น„์Šค๋Š” ์„œ๋กœ ๋‹ค๋ฅธ process ์—์„œ ๊ตฌ๋™์ด ๋˜๊ธฐ์—, ํ•˜๋‚˜์˜ transaction scope ์•ˆ์— ์•ˆ๋“ค์–ด ๊ฐ€์ง„๋‹ค.
ํ’€์–ด ๋งํ•˜์ž๋ฉด Clazz ์ž…์žฅ์—์„œ Course ์˜ courseId ๋ฅผ @ManyToOne ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
@RestAssociation ์„ ์‚ฌ์šฉํ•˜์—ฌ path์™€ joinColumn์„ ์ฃผ๊ณ  dummy๋กœ courseId ๋ฅผ ์„ค์ •ํ•œ๋‹ค.
์ด๋ ‡๊ฒŒ ์ ์šฉ์„ ํ•˜์˜€์„๋•Œ, ์•„๋ž˜์™€ ๊ฐ™์ด ์„œ๋กœ๋‹ค๋ฅธ ์„œ๋น„์Šค๋ผ๋ฆฌ HATEOAS link ์—ฐ๊ฒฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
http localhost:8080/clazzes course="http://localhost:8080/courses/1"

HATEOAS API๋ฅผ ์ง์ ‘ ์“ฐ๋Š” ๋ฐฉ์‹์€ ๊ฐ ์ƒํ™ฉ๋ณ„๋กœ ๋งค๋ฒˆ ๊ตฌํ˜„์„ ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งค์šฐ ์–ด๋ ต๋‹ค.
์ด์— objectpartners๋ผ๋Š” ๋””์ž์ธ ํŒจํ„ด์„ ์—ฐ๊ตฌํ•˜๋Š” ํšŒ์‚ฌ์—์„œ HATEOAS annotation ์„ ์ง์ ‘ ๋งŒ๋“ค์–ด์„œ ์“ฐ๋Š” ํŒจํ„ด์„ ๋งŒ๋“ค์—ˆ๊ณ , ํ•ด๋‹น ํŒจํ„ด์„ ์ ์šฉํ•˜์—ฌ metaworks4์—์„œ @RestAssociation ์„ ๊ตฌํ˜„ํ•ด ๋†“์•˜๋‹ค.

์ฐธ๊ณ 
https://github.com/uengine-oss/metaworks4/blob/master/src/main/java/org/metaworks/springboot/configuration/Metaworks4WebConfig.java
https://objectpartners.com/2016/02/18/mapping-jpa-entities-to-external-rest-resources-in-spring-data-rest/

โš ๏ธ **GitHub.com Fallback** โš ๏ธ