?? myeclipse_hibernate_quickstart_zh_cn.html
字號:
<li><strong>Hibernate Query Results</strong> 視圖可以查看多個查詢結果集; 結果的屬性顯示在 <strong>Properties</strong> 視圖. </li>
<li><strong>Query Parameters</strong> 視圖可以很方便的執行帶有參數的查詢. </li>
<li>項目選擇器允許你隨時切換不同的 Hibernate 項目中的 Hibernate 配置.
</li>
</ul>
<br>
下面的步驟將指導你如何使用 HQL 編輯器
<ol>
<li>右鍵點擊 Package Explorer 中的 <strong>HibernateDemo</strong> 項目. </li>
<li>在右鍵菜單中的 <strong>MyEclipse</strong> 項目下, 選擇 <strong>Open HQL Editor...</strong>
<br>
<strong><u>注意:</u></strong> HQL 編輯器也會在你嘗試查看后綴為 hql 的文件的時候打開.
<p><img atl="" src="MyEclipse_Hibernate_Quickstart_zh_CN_files/hibernate-openhql.gif">
<br>
<font class="figureCaption">圖 20. 打開 HQL 編輯器</font></p>
</li>
<li>如果你沒有在 Hibernate 透視圖下工作, 你將會被提示切換到相應的視圖, 同意切換即可.
<br>
<strong><u>注意:</u></strong> 總是可以切換到這個視圖, 通過選擇菜單項 <strong>Window > Open Perspective >
Other > MyEclipse Hibernate</strong> </li>
<li>當編輯器打開時, 鍵入 "from EchoMessage"
</li>
<li>點擊 <strong>Run</strong> 圖標或者按下 <strong>Ctrl + F9</strong> 來執行查詢語句.
<p><img alt="" src="MyEclipse_Hibernate_Quickstart_zh_CN_files/hibernate-hql-1-annot.gif">
<br>
<font class="figureCaption">圖 21. 在 HSQL 編輯器中執行查詢</font></p>
</li>
<li>使用 <strong>Query Parameters</strong> 視圖來執行帶參數的查詢.
<p><img alt="" src="MyEclipse_Hibernate_Quickstart_zh_CN_files/hibernate-hql-2.gif">
<br>
<font class="figureCaption">圖 22. Query Parameters 視圖</font></p>
<strong><u>注意:</u> 如果你在 HSQL 編輯器為特定的項目初始化后修改了配置,
映射文件或者數據類, 一定要記得點擊嵌入的工具欄上的 Refresh 按鈕來讓編輯器使用最新的配置信息.</strong></li>
</ol>
<p></p>
<p class="normal"><strong><a href="#top">返回頁首</a></strong></p>
<h2>7. <a name="testing"></a>測試 <em>HibernateDemo</em>
應用</h2>
<p class="normal">既然 Hibernate 持久化功能已經整合進了 <em>HibernateDemo</em> 項目, 接下來的步驟就是對它進行測試. 一種測試 Hibernate 持久化功能的方法是創建一個帶 main 方法的 Java 類. 測試類將使用 Hibernate 向導所生成的 Java 對象和 HibernateSessionFactory. 請查看圖 18 中所示的 HibernateSessionFactory 類中所生成的兩個重要的方法, 在測試代碼中將會使用這兩個方法.</p>
<table border="0" cellpadding="1" cellspacing="1" width="100%">
<tbody>
<tr>
<td width="234">
<p align="left"><img alt="" src="MyEclipse_Hibernate_Quickstart_zh_CN_files/hibernate-sessionmethods-annot.gif" border="0"><br>
<font class="figureCaption">圖 23. HibernateSessionFactory 方法</font></p>
</td>
<td>
<p align="left">顯示了 <span class="normal">HibernateSessionFactory</span> 中的兩個重要的靜態方法.</p>
<p align="left"><strong>getSession():</strong> 這個方法將會返回一個 <strong>Session</strong> 變量, 可以用來訪問 Hibernate Session 類. 任何時間你想使用 Hibernate 會話的時候你都可以調用這個方法來獲取緩存過的這個 Hibernate 會話對象.</p>
<p align="left"><strong>closeSession():</strong> 如果一個會話已經啟動過, 這個方法就簡單的關閉它.</p>
</td>
</tr>
</tbody>
</table>
<br>
<p class="normal">下面是創建測試類源代碼的步驟列表:</p>
<ol>
<li>
<div class="normal">創建一個新的 Java 類: <strong>File > New >
Class</strong></div>
</li><li>
<div class="normal">輸入包名</div>
</li><li>
<div class="normal">輸入類名(Name): <em>HibernateReadTest</em></div>
</li><li>
<div class="normal">如 圖 19 所示完成頁面設置, 然后選擇 <strong>Finish<br>
<br>
<img alt="" src="MyEclipse_Hibernate_Quickstart_zh_CN_files/hibernate-newmain.gif" border="0"><br>
</strong> <font class="figureCaption">圖 24. 新建 Java 測試類</font><br>
<br>
</div>
</li><li>
<div class="normal">
接下來, 打開文件
<em>HibernateReadTest.java</em> , 替換源代碼為圖 20 中所列的內容.
<br>
<br>
<table class="code" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="codeHeader">
HibernateReadTest.java
</td>
</tr>
<tr>
<!-- start source code -->
<td align="left" nowrap="nowrap" valign="top">
<code>
<font color="#7f0055"><b>package </b>
</font><font color="#000000">com.genuitec.hibernate;</font>
<br>
<font color="#ffffff"></font>
<br>
<font color="#7f0055"><b>import </b>
</font><font color="#000000">org.hibernate.HibernateException;</font>
<br>
<font color="#ffffff"></font>
<br>
<font color="#7f0055"><b>public class </b>
</font><font color="#000000">HibernateReadTest </font><font color="#000000">{</font>
<br>
<font color="#ffffff"></font>
<br>
<font color="#ffffff"> </font><font color="#7f0055"><b>public static </b>
</font><font color="#7f0055"><b>void </b>
</font><font color="#000000">main</font><font color="#000000">(</font><font color="#000000">String</font><font color="#000000">[] </font><font color="#000000">args</font><font color="#000000">) {</font>
<br>
<font color="#ffffff"> </font><font color="#3f7f5f">// Step 1 - Create the DAO</font>
<br>
<font color="#ffffff"> </font><font color="#000000">EchoMessageDAO dao = </font><font color="#7f0055"><b>new </b>
</font><font color="#000000">EchoMessageDAO</font><font color="#000000">()</font><font color="#000000">;</font>
<br>
<font color="#ffffff"> </font><font color="#7f0055"><b>try </b>
</font><font color="#000000">{</font>
<br>
<font color="#ffffff"> </font><font color="#3f7f5f">// Step 2 - Use findById to retrieve the message with ID = 1</font>
<br>
<font color="#ffffff"> </font><font color="#000000">EchoMessage echoMessage = dao.findById</font><font color="#000000">(</font><font color="#7f0055"><b>new </b>
</font><font color="#000000">Long</font><font color="#000000">(</font><font color="#990000">1</font><font color="#000000">))</font><font color="#000000">;</font>
<br>
<font color="#ffffff"> </font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#2a00ff">"Entity retrieval successful, message is: "</font>
<br>
<font color="#ffffff"> </font><font color="#000000">+ echoMessage.getMsg</font><font color="#000000">())</font><font color="#000000">;</font>
<br>
<font color="#ffffff"> </font><font color="#000000">} </font><font color="#7f0055"><b>catch </b>
</font><font color="#000000">(</font><font color="#000000">HibernateException e</font><font color="#000000">) {</font>
<br>
<font color="#ffffff"> </font><font color="#000000">System.err.println</font><font color="#000000">(</font><font color="#2a00ff">"Entity retrieval failed."</font><font color="#000000">)</font><font color="#000000">;</font>
<br>
<font color="#ffffff"> </font><font color="#000000">e.printStackTrace</font><font color="#000000">()</font><font color="#000000">;</font>
<br>
<font color="#ffffff"> </font><font color="#000000">} </font><font color="#7f0055"><b>finally </b>
</font><font color="#000000">{</font>
<br>
<font color="#ffffff"> </font><font color="#7f0055"><b>try </b>
</font><font color="#000000">{</font>
<br>
<font color="#ffffff"> </font><font color="#000000">dao.getSession</font><font color="#000000">()</font><font color="#000000">.close</font><font color="#000000">()</font><font color="#000000">;</font>
<br>
<font color="#ffffff"> </font><font color="#000000">} </font><font color="#7f0055"><b>catch </b>
</font><font color="#000000">(</font><font color="#000000">Exception e</font><font color="#000000">) {</font>
<br>
<font color="#ffffff"> </font><font color="#3f7f5f">// do nothing</font>
<br>
<font color="#ffffff"> </font><font color="#000000">}</font>
<br>
<font color="#ffffff"> </font><font color="#000000">}</font>
<br>
<font color="#ffffff"> </font><font color="#000000">}</font>
<br>
<font color="#ffffff"></font>
<br>
<font color="#000000">}</font>
</code>
</td>
<!-- end source code -->
</tr>
</tbody>
</table>
<font class="figureCaption">圖 25. HibernateTest.java
源代碼清單</font>
<br>
<br>
</div></li><li>
<div class="normal">保存 <em>HibernateReadTest.java</em></div>
</li><li>
<div class="normal">在
<strong>Package Explorer</strong> 中選中文件 <em>HibernateReadTest.java</em></div>
</li><li>
<div class="normal">在頂部菜單欄中選擇 <strong>Run</strong> 菜單, 接著選擇 <strong>Run > Run as > Java Application<br>
<br>
</strong> 這是, Console 視圖將會打開, 如果測試成功的話你將會看到如下面的 圖 21所示的輸出內容.
<br>
<strong><u>注意:</u></strong> 如果你看到了 log4j 警告, 你可以忽略它們即可.<br>
<br>
<img alt="" src="MyEclipse_Hibernate_Quickstart_zh_CN_files/hibernate-testconsole.gif" border="0"><br>
<font class="figureCaption">圖 26. HibernateReadTest Console 輸出</font></div>
</li></ol><br><p class="normal"><strong><a href="#top">返回頁首</a></strong></p>
<hr>
<h2>8. <a name="demoapp"></a> 使用示例 <em>HibernateDemo</em> 項目</h2>
<p class="normal">
</p><ol>
<li>下載 <a href="http://127.0.0.1:61071/help/topic/com.genuitec.myeclipse.doc/html/quickstarts/hibernate/HibernateDemo.zip">HibernateDemo.zip</a> </li>
<li>選擇 <strong>Import > Existing Projects into Workspace</strong>, 選中 <strong>Select archive file</strong> 選項然后指向下載的 zip 文件.
點擊 <strong>Finish</strong> 來導入到你的 Eclipse 工作臺.</li>
<li>需要做以下修改來重新配置這個項目和你的開發環境保持一致.
<ul>
<li>
從你的數據庫復制 JDBC 驅動 JAR 文件到項目中. 通過右鍵點擊文件然后選擇
<strong>Build Path > Add to Build Path</strong> 來將它添加到項目的構造路徑中. </li>
<li>
連接到你的數據庫然后執行 <font face="Courier New">DDL/DDL.sql</font> 文件中的代碼來創建示例數據庫并填充里面的值.
<br>
在執行之前, 也許需要修改表格的數據庫前綴并使它和你的數據庫的 DDL 規范一致.
</li>
<li>
修改配置文件 <font face="Courier New">hibernate.cfg.xml</font> , 將里面的連接設置修改為你自己的數據庫配置.
<br>
還可能需要修改
<font face="Courier New">EchoMessage.hbm.xml</font> 來修改 <em>class</em> 元素里面指定的 <em>schema </em>屬性為對應的數據庫名稱. </li>
</ul>
</li></ol>
一些其它的文件已經包含在這里:
<br>
<font face="Courier New">log4j.properties</font> 用來控制 log4j 類庫里面的日志等級.
<br>
<font face="Courier New">ehcache.xml</font> 用來控制 ehcache 類庫.<br>
<font face="Courier New">HibernateWriteTest.java</font> 是一個簡單的演示類來展示 DAO 是如何寫入數據到數據庫的.
<p></p>
<p class="normal"><strong><a href="#top">返回頁首</a></strong></p>
<hr>
<h2>9. <a name="summary"></a> 總結</h2>
<p class="normal">使用 MyEclipse 進行 Hibernate 開發的教程到此結束. 我們還有更多的快速入門文檔, 包括如何使用 Database
Explorer, Struts, JSF, Web Projects, 修改, 應用服務器配置, EJB 開發, 以及企業應用項目. 更多信息請訪問 <a href="http://myeclipseide.com/ContentExpress-display-ceid-67.html#quickstarts">MyEclipse
Quickstart library</a> . </p>
<p class="normal"><strong><a href="#top">返回頁首</a></strong></p>
<hr>
<h2>10. <a name="feedback"></a>用戶反饋</h2>
<p class="normal">如果你對本文有評論或者建議, 請提交到 <a href="http://www.myeclipseide.com/PNphpBB2+file-viewforum-f-6.html">MyEclipse
Documentation Forum</a> . </p>
<p class="normal"><strong><a href="#top">返回頁首</a></strong></p>
<hr>
<h2>11.<a name="resources"></a>資源</h2>
<ul>
<li><a href="http://127.0.0.1:61071/help/topic/com.genuitec.myeclipse.doc/html/quickstarts/hibernate/HibernateDemo.zip">HibernateDemo</a> 示例項目</li>
<li><a href="http://127.0.0.1:61071/help/topic/com.genuitec.myeclipse.doc/html/quickstarts/hibernate/myeclipse_templates_5.5GA.zip">MyEclipse Reverse-Engineering 模版</a><br>
</li><li>
<div>從 Sun
Microsystems.下載 <a href="http://java.sun.com/j2se/downloads/index.html%20"><font color="#006699">Java 2 SDK, Standard Edition 1.4</font></a> from </div>
</li><li>
<div>從 Eclipse Foundation 下載 <a href="http://www.eclipse.org/downloads/index.php">Eclipse
3.2 SDK 或者更高版本</a> </div>
</li><li>
<div>下載 <a href="http://www.myeclipseide.com/ContentExpress-display-ceid-10.html">MyEclipse
5.0</a> 30 天試用版; 過期后需要注冊.</div>
</li><li>
<div>要了解更多 Hibernate 的信息請訪問 <a href="http://hibernate.org/5.html">官方 Hibernate Documentation
站點</a>.</div>
</li><li>
<div>訪問 <a href="http://myeclipseide.com/ContentExpress-display-ceid-67.html#quickstarts">MyEclipse
Quickstart Library</a> 獲得更多 MyEclipse 教程.</div>
</li>
</ul>
<p class="normal"><strong><a href="#top">返回頁首</a></strong></p>
<hr>
<p><font class="fineprint">Copyright ?2004 - 2006 by Genuitec L.L.C.</font></p>
<p>中文版權 (c) 2007 by BeanSoft(beansoft@126.com) <br>
</p>
</body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -