Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

slumboy
slumboy

Posted on

     

พื้นฐาน JPA

หลักการสร้าง Entity Class


  1. เป็น java class ตั้งชื่อว่าอะไรก็ได้ (ให้สื่อกับความหมายนั้นๆ)
  2. implements Serializable
  3. มี annotation @Entiry อยู่บน class
  4. Entity Class ต้องมี default constructor ที่ไม่มี parameter เท่านั้น เช่น

    public Category(){ }
  5. ในกรณีที่ต้องการกำหนดค่าบางอย่างของ Table ให้ใช้ annotation @Table เช่น

    • การกำหนดชื่อให้ table จะใช้ attribute name
    • แต่ถ้าไม่ได้กำหนดชื่อ default table name จะเป็นชื่อของ entity class นั้นๆ
    • การตั้งชื่อ table ให้ระวังเรื่อง reserve word ของ database แต่ละเจ้า
  6. กำหนด attribute จะต้องมี attribute อย่างน้อย 1 attribute ที่มี annotation @Id เพื่อบอกว่าเป็น primary key

  7. มี method getter และ setter เพื่อ get และ set ค่าให้กับแต่ละ attribute

  8. ในการกำหนดค่า แต่ละ attribute หรือแต่ละ column ให้ใช้ annotation @Colum อยู่บน attribute นั้นๆ

  9. การ map relationship ให้สร้าง attribute ที่มี data type เป็น Entity Class ที่ต้องการ map ด้วย จากนั้นก็ใส่ annotation Mapping ต่างๆ ไว้บน attribute นั้นๆ เช่น

    @OneToOne@OneToMany@ManyToOne@ManyToMany

หลักการคร่าวๆ มีประมาณนี้

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

i'm Java Programmer
  • Location
    Bangkok, Thailand
  • Joined

More fromslumboy

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp