![Page 1: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/1.jpg)
Object-Relational Mapping
![Page 2: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/2.jpg)
Problem : OO Class & RDBMS
OO World RDBMS World
?
![Page 3: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/3.jpg)
Hibernate: Introduction
Gavin King
Hibernate ถกสรางขนโดยนกพฒนาจากประเทศออสเตรเลย Gavin King
ทตองการแกไขปญหาการทางานระหวางโปรแกรมเชงวตถและฐานขอมล
เชงสมพนธจนเปนทมาของ Hibernate ซอฟตแวรแบบรหสเปดทใชงาน
ในปจจบน
Hibernate เปนเครองมอทไมมคาใชจายท
ใชในการโปรแกรมดวยภาษาจาวาเพอ
บนทก อพเดท หรอลบขอมลออกจาก
ระบบฐานขอมลเชงสมพนธได
พฒนาขนตงแตปลายป 2001
![Page 4: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/4.jpg)
Problem Solved : Hibernate
![Page 5: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/5.jpg)
Object Relational Mapping
เปนโปรแกรมทใชแปลงออบเจกตจากโปรแกรมภาษาหนง ๆ ใหอยในรป
ของตารางหรอเอนตทในระบบฐานขอมลเชงสมพนธ
ทาหนาทเปนสะพานเชอมระหวางออบเจกตและตารางฐานขอมล
โดยคลาสจะถกแปลงใหอยในรปของตาราง
สวนออบเจกตเองจะถกอางถงขอมลแถวภายในตารางในฐานขอมล
ในขณะทแอททรบวตจะถกอางถงขอมลคอลมน
![Page 6: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/6.jpg)
Available solutions
ORM frameworks ทมอยในทองตลาดปจจบน
Language Name Type
Java Hibernate Open-Source
Java IBATISASF
(Apache Software Foundation)
Java Top-Link Oracle
.NET NHibernate Open-source
.NET Genome commercial
![Page 7: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/7.jpg)
Hibernate: Introduction
แนวคดหลกของ Hibernate คอ : โปรแกรมเมอรสรางคลาสแบบ
POJOs [Plain Old Java Objects] ดวยจาวาแลว ทาไมตองใชภาษาท
สอง SQL สาหรบการทางานรวมกบฐานขอมลดวย ?
Hibernate ทาหนาทเปนออบเจกตแบบ Persistence ทสามารถทางาน
รวมกบฐานขอมลเชงสมพนธไดโดยตรง รวมทงสามารถใชควรได
นนหมายความวา Hibernate บนทกออบเจกตลงในฐานขอมล และ
อานขอมลในรปออบเจกตออกจากฐานขอมลเชงสมพนธได
Hibernate จงเปนสงทตอบสนองตอความตองการของจาวา
โปรแกรมเมอร
![Page 8: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/8.jpg)
Persistence
หมายถงการบนทกขอมลแบบถาวรเพอใหสามารถนาไปใชไดในภายหลง
Persistence แบบทไดการยอมรบมากทสดไดแก ฐานขอมลเชงสมพนธ
(Relational Database)
จาวาแอพพลเคชนสามารถเชอมตอกบระบบฐานขอมลเชงสมพนธ ผาน
JDBC โดยมการทางานรวมกบภาษาสอบถามขอมล SQL
![Page 9: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/9.jpg)
Problem with Persistence
Identity และ Equality
จาวาใชเมธอด equals() และเครองหมาย == เพอสนบสนนการทางาน
แบบ Equality และ Identity ตามลาดบ
ตวอยางเชน การใชเครองหมาย == เพอตรวจสอบวาทงสองออบเจกตม
การอางองไปยงตาแหนงแอดเดรสภายในหนวยความจาเดยวกนหรอไม
เมธอด equals() ถกใชในการเปรยบเทยบขอมลของทงสองออบเจกตม
คาเทากนหรอไม
ในจาวาระหวาง Identity และ Equality มความแตกตางกนอยางชดเจน แต
ในฐานขอมลเชงสมพนธจะไมมอะไรทเกยวของกบนยามดงกลาวแมแต
นอย
![Page 10: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/10.jpg)
Problem with Persistence (cont)
วธการแกไขความไมสอดคลองกนทาไดโดยการเพมแอททรบวตพเศษลง
ภายในจาวาคลาส เพอใชเปนคอลมนพเศษทเรยกวา Identifier หรอคยหลก
ทอยในตารางฐานขอมล
![Page 11: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/11.jpg)
Pure Java Driver
JavaApplication DB Client
JDBC API JDBC Driver
Data Source
Server
STEP 1: Register JDBC driverSTEP 2: Open a connectionSTEP 3: Execute a querySTEP 4: Extract data from result setSTEP 5: Clean-up environment
2/1/2019
![Page 12: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/12.jpg)
Steps : Java with JDBC
//STEP 1: Register JDBC driverClass.forName("com.mysql.jdbc.Driver");
//STEP 2: Open a connectionconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student",”root”,”1234”);
//STEP 3: Execute a querystmt = conn.createStatement();String sql = "SELECT * FROM Table’s name";ResultSet rs = stmt.executeQuery(sql);
//STEP 4: Extract data from result setwhile(rs.next()){
//Retrieve by column name//Display values
}
//STEP 5: Clean-up environmentrs.close();stmt.close();conn.close();
![Page 13: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/13.jpg)
Hibernate และ JDBC
JDBC เมอเปดการเชอมตอกบเซรฟเวอรฐานขอมล ตองปดหลงการใชงาน
เสมอ อาจสงผลใหเกดปญหาเกยวกบจานวนการใชงานทเกนกาหนดได
การแกปญหาดงกลาวอาจจาเปนตองใชกลไกทเรยกวา Connection Pool ซง
เปนกลไกภายในการทางานหลกของ Hibernate อยแลว
ในแอพลเคชนแบบเรยลไทมการสรางตารางพรอมกบคยหลกดวย JDBC
จาเปนตองเขยนโคดเปนจานวนมากในการทางานในลกษณะดงกลาว แต
ใน Hibernate สามารถจดการไดโดยงาย
ใน JDBC จาเปนตองเขยนโคดเพมเพอใชในการจดการ Transactions แตใน
Hibernate สามารถใชโคดภายในจดการ Transaction โดยตรง
![Page 14: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/14.jpg)
ADV : Hibernate
Open Source เปนซอฟตแวรแบบรหสเปดทไมมคาใชจายในการใชงาน
Fast performance เนองจากใชกลไกทเรยกวา แคช (Cache)
Database Independent query ใชการควรขอมลผานกลไกทเรยกวา HQL
Automatic Table Creation ใน Hibernate ไดจดเตรยมกลไกในการสราง
ตารางฐานขอมลแบบอตโนมต
Simplifies complex join มคณสมบตทชวยลดความซบซอนของการ join
ระหวางตารางตาง ๆ ภายในฐานขอมล
Provides Query Statistics และ Database Status มการจดเตรยมขอมลทาง
สถตไวสาหรบการควรและแสดงสถานะของฐานขอมลได
![Page 15: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/15.jpg)
Hibernate Architecture
Hibernate มการทางานทมลกษณะเปนลาดบชนทอยระหวางแอพพลเคชน
และฐานขอมล
การเชอมตอระหวางคลาสในรปของ Persistence ออบเจกตไปยงฐานขอมล
โดยอาศยการทางานรวมกบคณสมบตตาง ๆ ของ Hibernate รวมไปถง
ขอมลทกาหนดไวในรปของ XML ไฟลตาง ๆ ดงรปการทางานตอไปน
![Page 16: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/16.jpg)
Hibernate Architecture
การทางานของ Hibernate เรมตนดงรปตอไปน
![Page 17: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/17.jpg)
Hibernate Core’s
สวนประกอบหลกในการทางานของ Hibernate
![Page 18: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/18.jpg)
SessionFactory
เปน interface ทใชในการสราง session ออบเจกตเพอสงตอไปยง
Hibernate แอพพลเคชน
![Page 19: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/19.jpg)
SessionFactory (cont.)
Session มลกษณะเปน heavy weighted ออบเจกต ดงนนจงถกออกมาให
ใช SessionFactory สรางขนเพยงออบเจกตเดยวโดยใชรปแบบของ
Singleton แพตเทรน
การเรยกใช Session Factory ออบเจกตสามารถทาไดดงคาสงตอไปน
Configuration configuration=new Configuration();configuration.configure();
ServiceRegistry sr = ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
SessionFactory session = configuration.buildSessionFactory(sr);
![Page 20: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/20.jpg)
Session
เปน single-threaded มชวงชวตสน ๆ เพอทางานรวมกบโปรแกรมและ
การจดการ persistence
หอหมการทางานของ JDBC connection ไวภายใน
![Page 21: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/21.jpg)
Persistent Objects and Collections
เปนออบเจกตจากคลาส POJO แตมความพเศษอยตรงทเกยวของกบ
Session ออบเจกตหนง ๆ
สนสดสถานะภาพ Persistent ออบเจกตเมอ Session ถกปดการทางาน
![Page 22: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/22.jpg)
Transient Objects and Collections
เปนออบเจกตของจาวาคลาสปกตทเกดขนจากคาสง new ไมมความ
เกยวของใด ๆ กบ Session
ยงไมเกยวของใด ๆ กบฐานขอมลเชงสมพนธ
![Page 23: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/23.jpg)
Transaction
Hibernate สราง transaction ผาน session interface
หากตองการใหฐานขอมลทางาน ตองสตารท transaction และเมอเสรจ
งานแลวตองปด transaction เพอสนสดการทางานกบฐานขอมล
![Page 24: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/24.jpg)
Hibernate Transaction Management
Transaction เปนเพยงการนาเสนอในรป unit ของงาน
ดงนนเมอขนตอนหนง fails ใน transaction ทงหมดจะ fails ตามไปดวย
การใชงานเมธอดหลกของ Transaction ใน Hibernate หลก ๆ ไดแก
commit(), rollback()
![Page 25: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/25.jpg)
การทางานรวมกบ Hibernate transaction
Configuration configuration = new Configuration().configure();StandardServiceRegistryBuilder builder = new
StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
Session session = sessionFactory.openSession();
try {session.beginTransaction();
session.getTransaction().commit();}catch (HibernateException e) {
e.printStackTrace();session.getTransaction().rollback();
}session.close();sessionFactory.close()
![Page 26: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/26.jpg)
Hibernate Programming
การโปรแกรมดวยจาวารวมกบ Hibernate เพอแปลงคลาสใหเปน
ฐานขอมลสามารถทาไดสองวธไดแก
การโปรแกรมโดยใชไฟล XML
การโปรแกรมโดยใชสญลกษณ Annotation
![Page 27: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/27.jpg)
Steps to create first Hibernate
การโปรแกรมดวย Hibernate มขนตอนดงตอไปน :
สราง Persistent คลาส
สรางไฟล XML สาหรบการ mapping จาก Persistent คลาส
สรางไฟล XML สาหรบการตงคาโครงแบบ (Configuration)
สรางออบเจกตสาหรบการบนทกหรอการอานคา persistent ออบ
เจกต
สราง Schema ของฐานขอมลเพอรองรบการทางานไวกอน
รนคลาสสาหรบการทดสอบการทางานของโปรแกรม
![Page 28: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/28.jpg)
Create the Persistent class
Persistent คลาสตองเปนไปตามเกณฑดงตอไปน :
A no-arg constructor: ใชคอนสตรคเตอรแบบ default เทานน
Provide an identifier property (optional): เพอใชในคอลมนทเปนคย
หลกของฐานขอมล
Declare getter and setter methods (optional): สรางเมธอดเฉพาะ
ประเภท getter และ setter เทานน โดยใชชอทเปน default
Prefer non-final class: Hibernate ใชแนวคดในการทางานแบบ
proxies ทการประกาศไมควรเปน final
![Page 29: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/29.jpg)
Hibernate & XML
ขนตอนการสรางจาวาคลาสpackage com.itsci.hibernate;public class Student {
private int studentId;private String studentName;
public int getStudentId() {return studentId;
}public void setStudentId(int studentId) {
this.studentId = studentId;}public String getStudentName() {
return studentName;}public void setStudentName(String studentName) {
this.studentName = studentName;}
}
![Page 30: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/30.jpg)
Mapping ไฟลสาหรบจาวาออบเจกต
การแปลงคลาสออบเจกตใหอยในรปแถวขอมลภายในตารางฐานขอมลดวย
Hibernate จาเปนตองใหรายละเอยดขอมลตาง ๆ ทจาเปนสาหรบการทางาน
ซงขอมลดงกลาวจะถกบนทกไวในไฟลชอ student.hbm.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class name="com.itsci.hibernate.Student" table="student">
<id name="studentId"> <generator class="increment"></generator> </id>
<property name="studentName"></property> </class> </hibernate-mapping>
![Page 31: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/31.jpg)
Hibernate Mapping ไฟล
<id> ใชเปนคาคยหลก (ID) เพอชวยปองกนความซ าซอนของขอมล
name – ชอของ id
column – ชอคอลมนในฐานขอมลสาหรบ id
<generator> ใชสราง id ทไมซ ากนทกครงทมการเรยกใชการทางาน
increment – แตละครงจะสราง id ใหมทเลอกจากคาสงสดในตาราง
ฐานขอมลและเพมไปอกหนงคาถดไป
<property> ทก ๆ แอททรบวตของจาวาคลาสทตองการแปลงใหเปน
ตารางฐานขอมลจาเปนตองกาหนด tags นไวในแตละคา ไดแก
name ระบชอของ property โดยอกษรตวแรกตองเปนตวเลกเสมอ
column ระบชอคอลมนในฐานขอมลทตองการบนทกคาจะแอททร
บวตนน ๆ
![Page 32: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/32.jpg)
Configuration : Hibernate
ในการแปลงจาวาคลาสใหเปนตารางฐานขอมลดวย Hibernate จาเปนตองใช
ขอมลทจาเปนทถกจดเกบไวในไฟลทเรยกวา hibernate.cfg.xml
<property name="connection.driver_class">: ใชสาหรบระบคลาส
JDBC driver
<property name="hibernate.connection.url "> : ใชสาหรบระบหมายเลข
พอรตและชอตารางฐานขอมลของ JDBC ทตองการเรยกใช
<property name="hibernate.connection.username " > : ใชระบ username
ของฐานขอมล
<property name="hibernate.connection.password" >: ใชระบ password
ของฐานขอมล
![Page 33: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/33.jpg)
การตงคาโครงแบบใน Hibernate
<property name="hibernate.connection.pool_size">: เปนคณสมบตทใช
จากดจานวนการหยดรอการเชอมตอกบฐานขอมลใน Hibernate
<property name="show_sql">: สงผลใหมการรนการทางานของ
Hibernate จะแสดงผลทก ๆ คาสง SQL ทเรยกใชภายในคอนโซลเสมอ
<property name="hbm2ddl.auto">: ใชระบการทางานทเกดขนภายใน
ระบบฐานขอมล เชน create, update หรอ drop ฐานขอมล เปนตน
<mapping class="com.itsci.himernate.student">: เปนการระบตาแหนง
ของจาวาคลาสพรอมแพคเกจตามลาดบ
![Page 34: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/34.jpg)
hibernate.cfg.xml
<hibernate-configuration><session-factory>
<!-- Database connection settings --><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/student</property><property name="connection.username">root</property><property name="connection.password">1234</property>
<!-- Echo all executed SQL to stdout --><property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup --><property name="hbm2ddl.auto">create</property>
<!-- Mention here all the model classes along with their package name --><mapping resource="student.hbm.xml"/>
</session-factory></hibernate-configuration>
![Page 35: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/35.jpg)
Run
public static void main(String[] args) {Configuration configuration = new Configuration().configure();StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());SessionFactory sessionFactory =
configuration.buildSessionFactory(builder.build());
Session session = sessionFactory.openSession();Transaction tx = null;Student student1 = new Student(); student1.setStudentName("Somchai");Student student2 = new Student(); student2.setStudentName("Somsri");
tx = session.beginTransaction();session.save(student1);session.save(student2);tx.commit();session.close();
sessionFactory.close();}
}
save Persistent Object
การ insert จะสมบรณเมอ
tx.commit();
![Page 36: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/36.jpg)
Hibernate with Annotations
ในยคแรก Hibernate ใชวธการแปลงคลาสใหเปนตารางฐานขอมลได
ตองผาน hbm.xml ไฟลเทานน
หากมความซบซอนมากขนจะสงผลใหขอมลภายในไฟลดงกลาวทม
จานวนมากขนดวย นอกจากนนยงเกดผลเสยอน ๆ ทตามมาดงตอไปน
ขอมลเกยวกบจาวาคลาสจะตองถกดาเนนการจากไฟลทอยภายนอก
ในกรณทมการทางานรวมกบจาวาคลาสจานวนมากขนขอ มลภายใน
ไฟล XML จะมขนาดใหญขนตามไปดวย สงผลใหจดการไดยาก
ขอผดพลาดทเกดขนในไฟล XML จะสงผลกระทบโดยตรงตอ
โปรแกรมทถกพฒนาขน
ดงนนจงไดพฒนาวธการทางานแบบใหมโดยใชสญลกษณทเรยกวา
Annotation มาใชในการโปรแกรม
![Page 37: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/37.jpg)
Hibernate Annotation
ผใช Hibernate สามารถเลอกใชการทางานผานสญลกษณ Annotation
หรอใชงานรวมกบไฟล .hbm.xml กได
ตวอยางการใช Annotation พนฐานสาหรบการโปรแกรมรวมกบ
Hibernate สาหรบตวอยางบางสวนของโปรแกรมมดงตอไปน
@Entitypublic class Sample {
@Id@GeneratedValue(strategy = GenerationType.IDENTITY)public Integer id;public String name;
}
![Page 38: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/38.jpg)
Hibernate Annotation
Annotation ประกอบดวยสญลกษณ @ ตามดวยชอของ Annotation ท
ตองการเพอประยกตใชกบการทางานในระดบของเมธอดหรอคลาส
Annotation พนฐานทถกกาหนดไวใน Hibernate และมกพบบอย ๆใน
การใชงานมดงตอไปน
@Entity
ใชระบ ไวกอนหนาคลาสใหอยในรปของเอนตททตองการแปลงให
เปนตารางในฐานขอมล ในบางกรณหากผใชตองการเปลยนชอเอน
ตทสามารถทาไดดงตวอยางคาสงตอไปน
@Entity(name="Entity_name")
![Page 39: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/39.jpg)
Hibernate Annotation
@Table
โดยปกตแลว Hibernate สรางตารางโดยใชชอเดยวกบคลาส แตการ
ใช @Table ยอมใหผใชสามารถระบชอของตารางไดตามตองการ
@Table(name="Table_name")
@Id
แตละเอนตทจาเปนตองมคยหลก ซงสามารถกาหนดผาน @Id ดงน
@Idprivate Long studentId;
![Page 40: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/40.jpg)
Hibernate Annotation
@Id ทใชในการระบคยหลกมกใชรวมกบ @GeneratedValue เพอสราง
คาคยหลกโดยอตโนมต
@GeneratedValue รบคาพารามเตอรสองชนดไดแก strategy และ
generator สาหรบ strategy ใชสาหรบกาหนดชนดของการเพมคาโดยม
รปแบบและรายละเอยดในการทางานดงตอไปน(strategy=GenerationType.AUTO) (strategy=GenerationType.IDENTITY) (strategy=GenerationType.SEQUENCE) (strategy=GenerationType.TABLE)
ในกรณทไมมการระบ strategy ไว Hibernate จะใชคา Default ไดแก
GenerationType.AUTO แทนทเสมอ
![Page 41: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/41.jpg)
Import package needs
org.hibernate.annotations.Table: ใชรวมกบ annotation เพอแปลงใหอยใน
รปตาราง
javax.persistence.Column: ใชสาหรบการแปลงเอนตทฟลดรวมกบ
คอลมนในตาราง
javax.persistence.Entity: ใชสาหรบระบจาวาคลาสปกตใหอยในรปเอนต
ทคลาส
javax.persistence.GeneratedValue: ใชในการสรางคาสาหรบคยหลก
javax.persistence.Id: ใชสาหรบกาหนดคยหลกของเอนตท
javax.persistence.Table: ใชสาหรบการแปลงใหเปนตารางฐานขอมล
![Page 42: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/42.jpg)
Annotation Example
@Entity@Table (name="STUDENT")public class Student {
@Id @GeneratedValueprivate int studentId;private String studentName;
public int getStudentId() { return studentId; }public void setStudentId(int studentId) {
this.studentId = studentId;}public String getStudentName() { return studentName; }public void setStudentName(String studentName) {
this.studentName = studentName;}public String toString() {
return "Student ID: "+this.studentId + " Student Name: "+this.studentName;}
}
![Page 43: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/43.jpg)
Simple Mappings (Annotations)
public class Student {
int studentId;
String studentName;
}
STUDENTstudentID studentNAME
@Entity
@Id
4
![Page 44: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/44.jpg)
hibernate.cfg.xml
<hibernate-configuration><session-factory>
<!-- Database connection settings --><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/student</property><property name="connection.username">root</property><property name="connection.password">1234</property>
<!-- Echo all executed SQL to stdout --><property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup --><property name="hbm2ddl.auto">create</property>
<!-- Mention here all the model classes along with their package name --><mapping class="com.itsci.hibernate.Student"/>
</session-factory></hibernate-configuration>
แกไขเปนชอคลาส
ทตองการ mappaing
![Page 45: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/45.jpg)
Hibernate Connection
public class HibernateConnection {
static String url = "jdbc:mysql://localhost:3306/StudentDB?characterEncoding=UTF-8";static String uname = "root";static String pwd = "1234";
public static SessionFactory sessionFactory;
public static SessionFactory doHibernateConnection() {Properties database = new Properties();database.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");database.setProperty("hibernate.connection.username", uname);database.setProperty("hibernate.connection.password", pwd);database.setProperty("hibernate.connection.url", url);database.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");Configuration cfg = new Configuration().setProperties(database).addPackage("bean")
.addAnnotatedClass(Student.class);StandardServiceRegistryBuilder ssrb =
new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());sessionFactory = cfg.buildSessionFactory(ssrb.build());return sessionFactory;
}
}
กรณท 2 สามารถเขยนเปนคลาส แทนการสราง hibernate.cfg.xml ไดดงน
![Page 46: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/46.jpg)
Run Program
Session session = sessionFactory.openSession();Transaction tx = null;Student student1 = new Student();student1.setStudentName("Somchai");Student student2 = new Student();student2.setStudentName("Somsri");try{
tx = session.beginTransaction();session.save(student1);session.save(student2);tx.commit();
}catch (HibernateException e) {if (tx!=null) tx.rollback();e.printStackTrace();
}finally {session.close();
}sessionFactory.close();
}
![Page 47: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/47.jpg)
Output
Hibernate: select max(studentId) from studentHibernate: insert into student (studentName, studentId) values (?, ?)Hibernate: insert into student (studentName, studentId) values (?, ?)
‘select max(studentID) from student’?Hibernate first finding out the highest number and then it uses that value for the insert. All just by saying “Save”.
![Page 48: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/48.jpg)
Read Object : session.get()
เมธอด get() ทางานโดยการผานคาสง SQL ไปยงฐานขอมลเพออาน
ขอมลจากตารางทเกยวของและสรางผลลพธในรปออบเจกตเมธอด get()
รบคาชอของเอนตทและคาคยหลกทตองการ
session.save(student1);session.save(student2);tx.commit();
Student dbStudent1 = (Student) session.get(Student.class, 1);System.out.println("Valid Student get called");System.out.println(dbStudent1);
Student dbStudent2 = (Student) session.get(Student.class, 20);System.out.println("Invalid Student get called");System.out.println(dbStudent2);
![Page 49: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/49.jpg)
Output : session.get()
Hibernate: insert into STUDENT (studentName) values (?)Hibernate: insert into STUDENT (studentName) values (?)Student1 saved with ID: 1Student2 saved with ID: 2Valid Student get calledStudent ID: 1 Student Name: SomchaiHibernate: select student0_.studentId as studentI1_0_0_, student0_.studentName as studentN2_0_0_ from STUDENT student0_ where student0_.studentId=?Invalid Student get calledNull
![Page 50: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/50.jpg)
Get ID from session.save()
int id1 = (Integer) session.save(student1);nt id2 = (Integer) session.save(student2);
session.getTransaction().commit();Student dbStudent1 = (Student) session.get(Student.class, id1);System.out.println(“Student ID “ + dbStudent1.getStudentID +
“Student Name : “+ dbStudent1.getStudentName());
Student dbStudent2 = (Student) session.get(Student.class, id2);System.out.println(“Student ID “ + dbStudent2.getStudentID +
“Student Name : “+ dbStudent2.getStudentName());
Hibernate: insert into STUDENT (studentName) values (?)Hibernate: insert into STUDENT (studentName) values (?)
Student ID: 1 Student Name: Somchai
Student ID: 2 Student Name: Somsri
![Page 51: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/51.jpg)
Read Object : session.load()
เมธอด session.load()
การอานคาโดยอาศยคยหลกนอกจากจะใชโดยการอานคาจากฐานขอ มล
โดยตรงแลวยงสามารถใชในการอานขอมลจากกลไกทเรยกวาแคช (Cache)
ซงมวตถประสงคในการลดจานวนครงในการเขาถงฐานขอมลโดยตรงสงผล
ใหโปรแกรมทางานไดเรวขน
session.save(student1);session.save(student2);tx.commit();
Student dbStudent1 = (Student) session.load(Student.class, 1);System.out.println("Valid Student get called");System.out.println(dbStudent1);Student dbStudent2 = (Student) session.load(Student.class, 20);System.out.println("Invalid Student get called");System.out.println(dbStudent2);
![Page 52: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/52.jpg)
Output : session.load()
Hibernate: insert into STUDENT (studentName) values (?)Hibernate: insert into STUDENT (studentName) values (?)Student1 saved with ID: 1Student2 saved with ID: 2Valid Student load calledStudent ID: 1 Student Name: SomchaiInvalid Student load calledHibernate: select student0_.studentId as studentI1_0_0_, student0_.studentName as studentN2_0_0_ from STUDENT student0_ where student0_.studentId=?org.hibernate.ObjectNotFoundException:
![Page 53: Object-Relational Mappingitsci.mju.ac.th/sayan/it214/slides/Java2_Hibernate_01... · 2019-02-01 · Hibernate: Introduction Gavin King. Hibernate ถูกสร้างขึ้นโดยนักพัฒนาจากประเทศออสเตรเลีย](https://reader034.vdocuments.site/reader034/viewer/2022042223/5ec99b7993134d4f0d05d15c/html5/thumbnails/53.jpg)
session.get() vs session.load()
student1 = (Student) session.get(Student.class, 1);System.out.println("Student get called");System.out.println(student1.getClass().getName());System.out.println(student1);student2 = (Student) session.load(Student.class, 2);System.out.println("Student load called");System.out.println(student2.getClass().getName());System.out.println(student2);
Hibernate: select student0_.studentId as studentI1_0_0_, student0_.studentName as studentN2_0_0_ from STUDENT student0_ where student0_.studentId=?com.itsci.hibernate.StudentStudent ID: 1 Student Name: Somchaicom.itsci.hibernate.Student_$$_jvstde_0Hibernate: select student0_.studentId as studentI1_0_0_, student0_.studentName as studentN2_0_0_ from STUDENT student0_ where student0_.studentId=?Student ID: 2 Student Name: Somsri
เมธอด get() คนคา
ชอคลาส Student
เมธอด load() คนคาชอ
คลาส Student_$$_jvstde_0