Hibernate Chapter 1 – Practical with Netbeans
Netbeans ႏွင့္ Hibernate Project တည္ေဆာက္ပုံ အဆင့္ဆင့္ကို ေဖာ္ျပထားပါသည္။ ဤေလ့က်င့္ခန္းတြင္ MySQL Database တစ္ခု လိုအပ္မည္ ျဖစ္ၿပီး Hibernate အား Reverse Engineering ပုံစံျဖင့္ တည္ေဆာက္ေသာ ပုံစံျဖစ္ပါသည္။
Step 1
MySQl database ကိုတည္ေဆာက္ပါ။ ၄င္းေနာက္ Netbean တြင္ Project အသစ္တစ္ခု တည္ေဆာက္ပါ။
Step 2
File menu ထဲမွ New Project ကို ႏွိပ္ပါ သို႔မဟုတ္ Toolbar ေပၚမွ New Project ကိုႏွိပ္ပါ။ ထို႔ေနာက္ New Project Dialog ေပၚလာပါမည္။ ပုံ – ၁ တြင္ၾကည့္ပါ။
Step 3
New Project Dialog မွ Java Web Folder ကိုေရြးပါ။ Web Application ကို ေရြးပါ။ Next Button ကိုႏွိပ္ပါ။ ထို႔ေနာက္ New Web Application Dialog ေပၚလာပါမည္။ ပုံ – ၂ တြင္ၾကည့္ပါ။
Step 4
New Web Application Dialog မွ Name and Location Session ထဲတြင္ရွိေသာ Project Name တြင္ Project နာမည္ေပးပါ။ Project Location တြင္ ကိုယ္သိမ္းခ်င္ေသာ Location ကို Browse ကိုႏွိပ္၍ ေရြးခ်ယ္ပါ။ Project Folder တြင္ Project ၏ Folder Path ကိုျပသေပးမည္ျဖစ္ပါသည္။ (ပုံ – ၂ တြင္ၾကည့္ပါ)။ Next Button ကိုႏွိပ္ပါ။ ထို႔ေနာက္ Server and Settings ကိုေရာက္ပါမည္။ (ပုံ – ၃ တြင္ၾကည့္ပါ)။
Step 5
Server and Settings တြင္ Server Combo box မွ လက္ရွိ ရွိေသာ Servers မ်ားမွ တစ္ခုကိုေရြးခ်ယ္ႏိုင္ပါသည္။ Java EE Version တြင္ Java EE ႏွင့္ပါတ္သက္ေသာ Version ကိုေရြးခ်ယ္ပါ Java EE 5 ကိုေရြးပါ။ Context Path တြင္ hibernate1 ေရးပါ။ ထို႔ေနာက္ Next Button ကိုႏွိပ္ပါ။ Frameworks session ေပၚလာပါမည္။ (ပုံ – ၄ တြင္ၾကည့္ပါ)
Step 6 – Part 1
Frameworks session တြင္ Hibernate 3.2.5 ကိုေရြးၿပီးေသာ အခါ Hibernate 3.2.5 Configuration ေပၚလာပါမည္။ (ပုံ – ၄ တြင္ၾကည့္ပါ)။ Hibernate Configuration အတြင္းမွ Database Connection မွ ကိုယ့္ Database Connection ကိုေရြးေပးပါ။ မရွိပါက New Database Connection… ကိုေရြးပါ။
Step 6 – Optional
New Database Connection… ကိုေရြးခ်ယ္လိုက္ေသာ အခါ New Database Connection Dialog က်လာပါမည္။ Dialog တြင္ Data Input Mode မွ Field Entry ကိုေရြးပါ။ Driver Name တြင္ MySQL (Connector/J driver) ကိုေရြးေပးပါ။ Host တြင္ ကိုယ့္ Database ၏ IP or URL ကိုထည့္ေပးပါ။ Port တြင္ Database ၏ Port ကိုထည့္ပါ။ Database တြင္ Database နာမည္ ကိုထည့္ပါ။ User Name တြင္ root ကိုထည့္ပါ။ Password တြင္ ကိုယ့္ Database ၏ password ကိုရိုက္ထည့္ပါ။ Rember Password တြင္ အမွန္ျခစ္ပါ။ ထို႔ေနာက္ Ok ကိုႏွိပ္ပါ။ (ပုံ – ၅ တြင္ၾကည့္ပါ)။
Step 6 – Part 2
Hibernate 3.2.5 Configuration တြင္ ေျပာင္းလဲသြားေသာ ပုံစံအားစစ္ပါ။ (ပုံ – ၆ တြင္ၾကည့္ပါ)။ Finish Button ကိုႏွိပ္ပါ။ Hibernate Library ပါေသာ Project တစ္ခု ဖန္တီးၿပီးသြားပါၿပီ။ (ပုံ – ၇ တြင္ၾကည့္ပါ)။
Step 7
Projects box (ပုံ – ၇) မွ <default package> ကိုေရြးပါ။ ထို႔ေနာက္ Toolbar မွ New File ကိုႏွိပ္ပါ။ New File Dialog က်လာပါမည္။ (ပုံ – ၈ တြင္ၾကည့္ပါ)။
ပုံ – ၈
Step 8
New File Dialog (ပုံ – ၈ တြင္ၾကည့္ပါ) မွ Hibernate Folder ကိုေရြးပါ။ ၄င္းေနာက္ Hibernate Reverse Engineering Wizard ကိုေရြးပါ။ Next Button ကိုႏွိပ္ပါ။ New Hibernate Reverse Engineering Wizard ေပၚလာပါမည္။ Next ကိုႏွိပ္ပါ။ Database Table Session ေပၚလာပါမည္။ (ပုံ – ၉ တြင္ၾကည့္ပါ)။ Add All ကိုႏွိပ္ပါ။ ထို႔ေနာက္ Finish ကိုႏွိပ္ပါ။
Step 9
Project Box (ပုံ – ၁၀ တြင္ၾကည့္ပါ) မွ Source Packages ရွိ Model Package ကိုေရြးပါ။ New File ကိုႏွိပ္ပါ။ New File Dialog က်လာေသာအခါ Hibernate Folder ေအာက္မွ Hibernate Mapping and POJOs from Database ကိုေရြးပါ။ New Hibernate Mapping and POJOs from Database Dialog က်လာပါမည္။ Hibernate Configuration File ႏွင့္ Hibernate Reverse Engineering File အား ေရြးေပးရန္။ (ပုံ – ၁၁ တြင္ၾကည့္ပါ)။ Finish ကိုႏွိပ္ပါ။ Database တြင္ရွိေသာ Table မ်ားအတိုင္း POJOs မ်ားဖန္တီးၿပီးသား ျဖစ္သြားပါမည္။ (ပုံ – ၁၂ တြင္ၾကည့္ပါ)။
Step 10
အထက္ပါ အဆင့္မ်ားသည္ Hibernate Configuration ႏွင့္ POJOs မ်ားတည္ေဆာက္ပုံ အဆင့္ဆင့္ျဖစ္ပါသည္။ ယခုအဆင့္ တြင္ Servlet ေပၚတြင္ Hibernate အတြက္လိုအပ္ေသာ Action မ်ားေရးသားပုံကို ဆက္လက္ျပသေပးမည္ ျဖစ္ပါသည္။ Project Box (ပုံ – ၁၂ တြင္ၾကည့္ပါ) မွ controller package ကိုေရြးခ်ယ္ၿပီး New File ကိုႏွိပ္ပါ။ New File Dialog က်လာပါမည္။ New File Dialog မွ Web folder ကိုေရြးၿပီး Servlet ကိုေရြးပါ၊ (ပုံ – ၁၃ ကိုၾကည့္ပါ)။ Next ကိုႏွိပ္ပါ။
Step 11
New Servlet Dialog (ပုံ – ၁၄ ကိုၾကည့္ပါ) ၏ Class Name တြင္ Servlet အတြက္ နာမည္ေပးပါ။ Next ကိုႏွိပ္ပါ။
Step 12
Configuration Servlet Deployment Session တြင္ URL Pattern တြင္ /formAction ေရးပါ။ ထို႔ေနာက္ Finish ကိုႏွိပ္ပါ။
Step 13
index.jsp ကိုဖြင့္ ၍ လိုင္းနံပါတ္ ၁၈ တြင္ ေအာက္ပါ Code မ်ားကို ျဖည့္စြတ္ေရးသားပါ။
<form action="formAction" method="post"> E Mail:<input name="email" value="" /><br> Name:<input type="text" value="" /><br> Address:<input type="text" value="" /><br> Phone:<input type="text" value="" /><br> <input value="Save" /> </form>File အျပည့္အစုံမွာ ေအာက္ပါအတိုင္းျဖစ္ပါသည္။
<%-- Document : index Created on : Jun 6, 2010, 9:41:10 PM Author : Joe Minhan --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Hello World!</h1> <form action="formAction" method="post"> E Mail:<input type="text" value="" /><br> Name:<input type="text" value="" /><br> Address:<input type="text" value="" /><br> Phone:<input name="phone" value="" /><br> <input value="Save" /> </form> </body> </html>Step 14
formAction servlet file ကိုဖြင့္၍ processRequest method ၏ လိုင္းနံပါတ္ ၃၀ မွ ၄၅ အတြင္းရွိcode မ်ားေနရာတြင္ ေအာက္ပါ code မ်ားကို ျဖည့္ပါ။
org.hibernate.Session hbSession = null; try { //This step will read hibernate.cfg.xml and prepare hibernate for use. org.hibernate.SessionFactory sessionFactory = new org.hibernate.cfg.Configuration().configure().buildSessionFactory(); hbSession = sessionFactory.openSession(); //Create new instance of Member entity bean and set values models.Member member = new models.Member(); member.setMemberId(request.getParameter("email")); member.setName(request.getParameter("name")); member.setAddress(request.getParameter("address")); member.setPhone(request.getParameter("phone")); //Save the database hbSession.save(member); } catch(java.lang.Exception ex) { //Some exception throw in here! response.sendRedirect("success.jsp?err=" + ex.getMessage()); } finally { //Actual member's information is saved in here! hbSession.flush(); hbSession.close(); response.sendRedirect("success.jsp"); }ထို႔ေနာက္ success.jsp file တစ္ခုတည္ေဆာက္ပါ။ အထက္ပါ အဆင့္မ်ားၿပီးေနာက္ Project အား Run ၾကည့္ပါ သင္တည္ေဆာက္ေသာ Project ေအာင္ျမင္စြာ အလုပ္လုပ္ေၾကာင္းေတြ႔ရပါမည္။
Facebook comments:


















ကောင်းတယ်ဗျာ ဆက်ရေးပါဥိးဗျ ဆက်လေ့လာနေပါ့မယ်