⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myeclipse_hibernate_quickstart_zh_cn.html

📁 Hibernate的电子查询文档
💻 HTML
📖 第 1 页 / 共 5 页
字号:
	<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.&nbsp; 打开 HQL 编辑器</font></p>
	</li>
	<li>如果你没有在 Hibernate 透视图下工作, 你将会被提示切换到相应的视图, 同意切换即可. 
	<br>
	<strong><u>注意:</u></strong> 总是可以切换到这个视图, 通过选择菜单项 <strong>Window &gt; Open Perspective &gt;
	Other &gt; 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.&nbsp; 在 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.&nbsp; 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> 项目, 接下来的步骤就是对它进行测试.&nbsp; 一种测试 Hibernate 持久化功能的方法是创建一个带 main 方法的 Java 类.&nbsp; 测试类将使用 Hibernate 向导所生成的 Java 对象和 HibernateSessionFactory.&nbsp; 请查看图 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.&nbsp;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 类.&nbsp; 任何时间你想使用 Hibernate 会话的时候你都可以调用这个方法来获取缓存过的这个 Hibernate 会话对象.</p>

			<p align="left"><strong>closeSession():</strong>&nbsp;如果一个会话已经启动过, 这个方法就简单的关闭它.</p>
		  </td>
		</tr>
	</tbody>
</table>
<br>


<p class="normal">下面是创建测试类源代码的步骤列表:</p>

<ol>
	<li>
	<div class="normal">创建一个新的 Java 类: <strong>File &gt; New &gt;
	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 中所列的内容.&nbsp;
					<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&nbsp;</b>
										</font><font color="#000000">com.genuitec.hibernate;</font>
										<br>
										<font color="#ffffff"></font>
										<br>
										<font color="#7f0055"><b>import&nbsp;</b>
										</font><font color="#000000">org.hibernate.HibernateException;</font>
										<br>
										<font color="#ffffff"></font>
										<br>
										<font color="#7f0055"><b>public&nbsp;class&nbsp;</b>
										</font><font color="#000000">HibernateReadTest&nbsp;</font><font color="#000000">{</font>
										<br>
										<font color="#ffffff"></font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;</font><font color="#7f0055"><b>public&nbsp;static&nbsp;</b>
										</font><font color="#7f0055"><b>void&nbsp;</b>
										</font><font color="#000000">main</font><font color="#000000">(</font><font color="#000000">String</font><font color="#000000">[]&nbsp;</font><font color="#000000">args</font><font color="#000000">)&nbsp;{</font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#3f7f5f">//&nbsp;Step&nbsp;1&nbsp;-&nbsp;Create&nbsp;the&nbsp;DAO</font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">EchoMessageDAO&nbsp;dao&nbsp;=&nbsp;</font><font color="#7f0055"><b>new&nbsp;</b>
										</font><font color="#000000">EchoMessageDAO</font><font color="#000000">()</font><font color="#000000">;</font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><b>try&nbsp;</b>
										</font><font color="#000000">{</font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#3f7f5f">//&nbsp;Step&nbsp;2&nbsp;-&nbsp;Use&nbsp;findById&nbsp;to&nbsp;retrieve&nbsp;the&nbsp;message&nbsp;with&nbsp;ID&nbsp;=&nbsp;1</font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">EchoMessage&nbsp;echoMessage&nbsp;=&nbsp;dao.findById</font><font color="#000000">(</font><font color="#7f0055"><b>new&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#2a00ff">"Entity&nbsp;retrieval&nbsp;successful,&nbsp;message&nbsp;is:&nbsp;"</font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">+&nbsp;echoMessage.getMsg</font><font color="#000000">())</font><font color="#000000">;</font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}&nbsp;</font><font color="#7f0055"><b>catch&nbsp;</b>
										</font><font color="#000000">(</font><font color="#000000">HibernateException&nbsp;e</font><font color="#000000">)&nbsp;{</font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">System.err.println</font><font color="#000000">(</font><font color="#2a00ff">"Entity&nbsp;retrieval&nbsp;failed."</font><font color="#000000">)</font><font color="#000000">;</font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">e.printStackTrace</font><font color="#000000">()</font><font color="#000000">;</font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}&nbsp;</font><font color="#7f0055"><b>finally&nbsp;</b>
										</font><font color="#000000">{</font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><b>try&nbsp;</b>
										</font><font color="#000000">{</font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}&nbsp;</font><font color="#7f0055"><b>catch&nbsp;</b>
										</font><font color="#000000">(</font><font color="#000000">Exception&nbsp;e</font><font color="#000000">)&nbsp;{</font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#3f7f5f">//&nbsp;do&nbsp;nothing</font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font>
										<br>
										<font color="#ffffff">&nbsp;&nbsp;</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 &gt; Run as &gt; 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 &gt; Existing Projects into Workspace</strong>, 选中 <strong>Select archive file</strong> 选项然后指向下载的 zip 文件. 
	点击 <strong>Finish</strong> 来导入到你的 Eclipse 工作台.</li>
	<li>需要做以下修改来重新配置这个项目和你的开发环境保持一致.
	  <ul>
					<li>
						从你的数据库复制 JDBC 驱动 JAR 文件到项目中. 通过右键点击文件然后选择
						<strong>Build Path &gt; 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> 用来控制&nbsp;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 开发的教程到此结束.&nbsp;我们还有更多的快速入门文档, 包括如何使用 Database
Explorer, Struts, JSF, Web Projects, 修改, 应用服务器配置, EJB 开发, 以及企业应用项目. 更多信息请访问 <a href="http://myeclipseide.com/ContentExpress-display-ceid-67.html#quickstarts">MyEclipse
Quickstart library</a> .&nbsp;</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> .&nbsp;</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>&nbsp;30 天试用版; 过期后需要注册.</div>
	</li><li>
	<div>要了解更多 Hibernate 的信息请访问&nbsp;<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 + -