DEV Community

slumboy
slumboy

Posted on

4

พื้นฐาน JPA[ ep.2 ]

เกริ่นไปเรื่อย


การเขียนโปรแกรมติดต่อฐานข้อมูลด้วยภาษา Java ส่วนใหญ่พื้นฐานเราจะใช้ JDBC ในการติดต่อ นอกจาก JDBC แล้วบางคนก็อาจจะใช้ Framework หรือ API ประเภท ORM (Object Relation Mapping) ถึงตรงนี้แล้วบางคนอาจสงสัยว่า ORM คืออะไร เอาง่ายๆ เลยนะคับ ORM คือเครื่องมือที่ช่วยให้เราสามารถมองฐานข้อมูลเป็น Object ถ้าเราแก้ไขข้อมูลใน Object ก็จะท าให้ข้อมูลในฐานข้อมูลมีการเปลี่ยนแปลง เหมือนกับการใช้ SQL Command ตรงๆ


ยกตัวอย่างเช่น เรามีตารางฐานข้อมูล เก็บข้อมูลลูกค้าตามรูปข้างล่าง(1) หากเรามองเป็น Object เรามักจะเขียน Code (2) ประมาณนี้

alt text


ซึ่งถ้าเป็น ORM เราสามารถสร้าง Record ใหม่ด้วยการ New Instance ของ Object Employee และสั่งให้ Framework หรือ API ทำการสร้าง Record ได้เลย โดยที่ไม่ต้องเขียน Statement Insert เหมือนใน JDBC ที่พูดมายืดยาวก็เพื่อให้พอเห็นภาพของ ORM โดยเจ้าตัว ORM นี้ใน Java ก็มีด้วยกันหลายตัวเช่น Hibernate , JPA (Java Persistence API) , … etc.


สำหรับในวันนี้จะขอพูดถือตัว JPA

JPA ย่อมาจาก Java Persistence API เป็น API มาตรฐานตัวหนึ่งที่อยู่ใน Technology Java EE ใช้สำหรับการทำ ORM วิธีการสร้างหรือการใช้งานก็ไม่ยุ่งยาก โดยขอแบ่งเป็นขั้นตอนง่ายๆ ดังนี้

  1. ทำการสร้าง Object เพื่อ Mapping ตารางหรือ Field ต่างๆ ในฐานข้อมูล
  2. ทำการเขียน Configuration ไฟล์สำหรับการเชื่อมฐานข้อมูลในไฟล์ persistence.xml
  3. ใช้งาน Object ผ่านทาง Entity Manager

  1. ทำการสร้ำง Object เพื่อ Mapping ตารางหรือ Field ต่างๆ ในฐานข้อมูล ขั้นตอนการทำ Object Mapping คือการที่เราเอาทำการเขียน Object ให้มีความสัมพันธ์กับฐานข้อมูลโดยมีรายละเอียดดังนี้

@Entity
ใช้สำหรับกำหนด Class ที่จะ Map กับตารางในฐานข้อมูล
alt text
*กรณีไม่กำหนดชื่อตาราง ชื่อ Entity นั้นๆ จะหมายถึงชื่อตารางในฐานข้อมูล


@Table
ใช้สำหรับกำหนดชื่อตารางฐานข้อมูลที่ Map กับ Class ที่เราสร้าง
alt text


@id และ @GeneratedValue
ใช้สำหรับกำหนด Field ที่เป็น Primary Key กรณีที่เป็น id ประเภท Auto Generate จะใช้ @GeneratedValue
alt text


@Column
ใช้สำหรับกำหนดชนิดของ column ของฐานข้อมูล เช่น
alt text

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (2)

Collapse
 
boonphokittikun profile image
Kittikun Boonpho

เยี่ยม

Collapse
 
tantaizaza profile image
Tantai

ขอบคุณครับผม

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay