﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-Innate Solitary</title><link>http://www.blogjava.net/kick191/</link><description>登高者必自卑，行远者必自迩，在这个世界上，重要的不是你正站在哪里，而是你正朝什么方向移动</description><language>zh-cn</language><lastBuildDate>Sun, 03 May 2026 13:36:17 GMT</lastBuildDate><pubDate>Sun, 03 May 2026 13:36:17 GMT</pubDate><ttl>60</ttl><item><title>Python核心编程（第二版）第九章习题，9－7的实现</title><link>http://www.blogjava.net/kick191/articles/Python_core_9-7.html</link><dc:creator>天独</dc:creator><author>天独</author><pubDate>Thu, 15 Jul 2010 04:47:00 GMT</pubDate><guid>http://www.blogjava.net/kick191/articles/Python_core_9-7.html</guid><wfw:comment>http://www.blogjava.net/kick191/comments/326183.html</wfw:comment><comments>http://www.blogjava.net/kick191/articles/Python_core_9-7.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kick191/comments/commentRss/326183.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kick191/services/trackbacks/326183.html</trackback:ping><description><![CDATA[9－7习题是要求些一個解析配置文件的类，用来解析Win32，POSIX，或着其他平台下的配置文件，我在这里只写了一個Win32文件的ini文件的解析<br />
下面是我的实现代码：<br />
<div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 128);">&nbsp;&nbsp;1</span>&nbsp;<span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">coding=utf-8</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;&nbsp;2</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;&nbsp;3</span>&nbsp;<span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;&nbsp;4</span>&nbsp;<span style="color: rgb(128, 0, 0);">Created&nbsp;on&nbsp;2010-7-12<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;&nbsp;5</span>&nbsp;<span style="color: rgb(128, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;&nbsp;6</span>&nbsp;<span style="color: rgb(128, 0, 0);">@author:&nbsp;Innate&nbsp;Solitary<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;&nbsp;7</span>&nbsp;<span style="color: rgb(128, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;&nbsp;8</span>&nbsp;<span style="color: rgb(128, 0, 0);">ini文件解析类<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;&nbsp;9</span>&nbsp;<span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;10</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;11</span>&nbsp;<span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;ConfigParser<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;12</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;13</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;14</span>&nbsp;<span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;ConfigFile(object):<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;15</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;16</span>&nbsp;<span style="color: rgb(128, 0, 0);">&nbsp;&nbsp;&nbsp; 配置文件解析，处理的简单实现类<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;17</span>&nbsp;<span style="color: rgb(128, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;18</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;19</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;20</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self,&nbsp;file_name):<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;21</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;22</span>&nbsp;<span style="color: rgb(128, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Constructor<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;23</span>&nbsp;<span style="color: rgb(128, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;24</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.file_name&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;file_name<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;25</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.file&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;file(file_name)<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;26</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.cfg&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;ConfigParser.ConfigParser()<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;27</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.read_handle&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;None<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;28</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.write_handle&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;None<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;29</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sections&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;[]<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;30</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.items&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;{}<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;31</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.init_flag&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;False<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;32</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;33</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;initialization(self):<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;34</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;35</span>&nbsp;<span style="color: rgb(128, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 初始化配置文件<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;36</span>&nbsp;<span style="color: rgb(128, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;37</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);">:<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;38</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.read_handle&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;open(self.file_name,&nbsp;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">r</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;39</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.write_handle&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;open(self.file_name,&nbsp;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">w</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;40</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.cfg.readfp(self.read_handle)<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;41</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sections&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;self.cfg.sections()<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;42</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;section&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;self.sections:<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;43</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.items[section]&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;self.cfg.items(section)<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;44</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.init_flag&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;True<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;45</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">except</span><span style="color: rgb(0, 0, 0);">&nbsp;Exception,&nbsp;e:<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;46</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">初始化配置文件错误:</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,&nbsp;str(e)<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;47</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.init_flag&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;False<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;48</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;self.init_flag<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;49</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;50</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;close(self):<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;51</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;52</span>&nbsp;<span style="color: rgb(128, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 关闭配置文件<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;53</span>&nbsp;<span style="color: rgb(128, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;54</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.read_handle.close()<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;55</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);">&nbsp;self.file<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;56</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);">&nbsp;self.read_handle<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;57</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);">&nbsp;self.file_name<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;58</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);">&nbsp;self.cfg<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;59</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);">&nbsp;self.sections<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;60</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);">&nbsp;self.items<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;61</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;62</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;get_value(self,&nbsp;section,&nbsp;key,&nbsp;default&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">''</span><span style="color: rgb(0, 0, 0);">):<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;63</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;64</span>&nbsp;<span style="color: rgb(128, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 得到指定section下的key的值，如果沒有給其一個空字符串做为默认值，并加入文件中<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;65</span>&nbsp;<span style="color: rgb(128, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;66</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);">&nbsp;self.init_flag:<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;67</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">raise</span><span style="color: rgb(0, 0, 0);">&nbsp;Exception(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">未初始化配置文件，请在处理文件前调用initialization(self)进行配置文件初始化</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;68</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;69</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;70</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);">&nbsp;self.cfg.has_section(section):<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;71</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.cfg.add_section(section)<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;72</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);">&nbsp;self.cfg.has_option(section,&nbsp;key):<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;73</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.cfg.set(section,&nbsp;key,&nbsp;default)<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;74</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;75</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;self.cfg.get(section,&nbsp;key)<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;76</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;77</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;78</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;value<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;79</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;80</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;set_value(self,&nbsp;section,&nbsp;key,&nbsp;value):<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;81</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;82</span>&nbsp;<span style="color: rgb(128, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 设置值<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;83</span>&nbsp;<span style="color: rgb(128, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;84</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;85</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);">&nbsp;self.cfg.has_section(section):<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;86</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.cfg.add_section(section);<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;87</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;88</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.cfg.set(section,&nbsp;key,&nbsp;value)<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;89</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;90</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;write_to_file(self,&nbsp;file&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;None):<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;91</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;file&nbsp;</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">&nbsp;None&nbsp;</span><span style="color: rgb(0, 0, 255);">and</span><span style="color: rgb(0, 0, 0);">&nbsp;file.mode&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">r</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;92</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">raise</span><span style="color: rgb(0, 0, 0);">&nbsp;Exception(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">文件模式不可写</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;93</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;file&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;None:<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;94</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.cfg.write(self.write_handle)<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;95</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">:<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;96</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.cfg.write(file)<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;97</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;98</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;99</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">100</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: rgb(0, 128, 128);">101</span>&nbsp;</div>
我在这里使用了ConfigParser模块，这里我的这段代码因为工作的原因只实现了一部分，并沒有把我当初想到的功能实现，但是习题9－7要求的功能都实现了，代码很简单个人覺沒有什么难得所以沒有加注释。<br />
<br />
<img src ="http://www.blogjava.net/kick191/aggbug/326183.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kick191/" target="_blank">天独</a> 2010-07-15 12:47 <a href="http://www.blogjava.net/kick191/articles/Python_core_9-7.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>EJB连接超时异常，解决办法！</title><link>http://www.blogjava.net/kick191/articles/EJB.html</link><dc:creator>天独</dc:creator><author>天独</author><pubDate>Sat, 12 Jun 2010 08:03:00 GMT</pubDate><guid>http://www.blogjava.net/kick191/articles/EJB.html</guid><wfw:comment>http://www.blogjava.net/kick191/comments/323440.html</wfw:comment><comments>http://www.blogjava.net/kick191/articles/EJB.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kick191/comments/commentRss/323440.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kick191/services/trackbacks/323440.html</trackback:ping><description><![CDATA[当遇到Exception in thread "main" javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out]时<br />
1.首先检查你的Context.PROVIDER_URL配置是否正确，例如Jboss4.0.2中是jnp://localhost:1099，当然这个1099端口也可能换，如果不知道请到%JBOSS_HOME%\server\default\conf\下查找jboss-service.xml在里面找到如下代码：<br />
<div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 128);">&nbsp;1</span>&nbsp;<span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mbean&nbsp;</span><span style="color: rgb(255, 0, 0);">code</span><span style="color: rgb(0, 0, 255);">="org.jboss.naming.NamingService"</span><span style="color: rgb(255, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;2</span>&nbsp;<span style="color: rgb(255, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name</span><span style="color: rgb(0, 0, 255);">="jboss:service=Naming"</span><span style="color: rgb(255, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;3</span>&nbsp;<span style="color: rgb(255, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xmbean-dd</span><span style="color: rgb(0, 0, 255);">="resource:xmdesc/NamingService-xmbean.xml"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;4</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);">&nbsp;The&nbsp;call&nbsp;by&nbsp;value&nbsp;mode.&nbsp;true&nbsp;if&nbsp;all&nbsp;lookups&nbsp;are&nbsp;unmarshalled&nbsp;using<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;5</span>&nbsp;<span style="color: rgb(0, 128, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the&nbsp;caller's&nbsp;TCL,&nbsp;false&nbsp;if&nbsp;in&nbsp;VM&nbsp;lookups&nbsp;return&nbsp;the&nbsp;value&nbsp;by&nbsp;reference.<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;6</span>&nbsp;<span style="color: rgb(0, 128, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;7</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">attribute&nbsp;</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="CallByValue"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">false</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">attribute</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;8</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);">&nbsp;The&nbsp;listening&nbsp;port&nbsp;for&nbsp;the&nbsp;bootstrap&nbsp;JNP&nbsp;service.&nbsp;Set&nbsp;this&nbsp;to&nbsp;-1<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;9</span>&nbsp;<span style="color: rgb(0, 128, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to&nbsp;run&nbsp;the&nbsp;NamingService&nbsp;without&nbsp;the&nbsp;JNP&nbsp;invoker&nbsp;listening&nbsp;port.<br />
</span><span style="color: rgb(0, 128, 128);">10</span>&nbsp;<span style="color: rgb(0, 128, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">11</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">attribute&nbsp;</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="Port"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">1099</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">attribute</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">12</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);">&nbsp;The&nbsp;bootstrap&nbsp;JNP&nbsp;server&nbsp;bind&nbsp;address.&nbsp;This&nbsp;also&nbsp;sets&nbsp;the&nbsp;default<br />
</span><span style="color: rgb(0, 128, 128);">13</span>&nbsp;<span style="color: rgb(0, 128, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RMI&nbsp;service&nbsp;bind&nbsp;address.&nbsp;Empty&nbsp;==&nbsp;all&nbsp;addresses<br />
</span><span style="color: rgb(0, 128, 128);">14</span>&nbsp;<span style="color: rgb(0, 128, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">15</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">attribute&nbsp;</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="BindAddress"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">${jboss.bind.address}</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">attribute</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">16</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);">&nbsp;The&nbsp;port&nbsp;of&nbsp;the&nbsp;RMI&nbsp;naming&nbsp;service,&nbsp;0&nbsp;==&nbsp;anonymous&nbsp;</span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">17</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">attribute&nbsp;</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="RmiPort"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">1098</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">attribute</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">18</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);">&nbsp;The&nbsp;RMI&nbsp;service&nbsp;bind&nbsp;address.&nbsp;Empty&nbsp;==&nbsp;all&nbsp;addresses<br />
</span><span style="color: rgb(0, 128, 128);">19</span>&nbsp;<span style="color: rgb(0, 128, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">20</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">attribute&nbsp;</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="RmiBindAddress"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">${jboss.bind.address}</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">attribute</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">21</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);">&nbsp;The&nbsp;thread&nbsp;pool&nbsp;service&nbsp;used&nbsp;to&nbsp;control&nbsp;the&nbsp;bootstrap&nbsp;lookups&nbsp;</span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">22</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">depends&nbsp;</span><span style="color: rgb(255, 0, 0);">optional-attribute-name</span><span style="color: rgb(0, 0, 255);">="LookupPool"</span><span style="color: rgb(255, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">23</span>&nbsp;<span style="color: rgb(255, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy-type</span><span style="color: rgb(0, 0, 255);">="attribute"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">jboss.system:service=ThreadPool</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">depends</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">24</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">mbean</span><span style="color: rgb(0, 0, 255);">&gt;</span></div>
&nbsp;<span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">attribute&nbsp;</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="Port"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">1099</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">attribute</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"> 看这里的端口号，修改这里的端口号为你provider_url配置的端口号，或着反过来修改你的provider_url的端口号为这里的端口号。<br />
<br />
2.如果1中配置沒有问题，请检查你的防火墙，杀毒软件等安全工具是否禁止了端口，我用的mcafee，需要在mcafee的访问保护-&gt;属性-&gt;用户自定义规则添加一条规则，根据提示写入需要排除的端口号，然后在排除项中添加java.exe和javaw.exe确定即可，在试试，应该就可以了！<br />
</span>
<img src ="http://www.blogjava.net/kick191/aggbug/323440.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kick191/" target="_blank">天独</a> 2010-06-12 16:03 <a href="http://www.blogjava.net/kick191/articles/EJB.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JPA入门－3</title><link>http://www.blogjava.net/kick191/articles/319147.html</link><dc:creator>天独</dc:creator><author>天独</author><pubDate>Thu, 22 Apr 2010 12:01:00 GMT</pubDate><guid>http://www.blogjava.net/kick191/articles/319147.html</guid><wfw:comment>http://www.blogjava.net/kick191/comments/319147.html</wfw:comment><comments>http://www.blogjava.net/kick191/articles/319147.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kick191/comments/commentRss/319147.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kick191/services/trackbacks/319147.html</trackback:ping><description><![CDATA[有半个月没有更新了，倒不是我没有继续学习，工作比较忙，晚上学习的时间短，没有太多时间写blog<br />
今天，说CallBack，JPA为我们提供了一些在进行CURD前后的一些callback methods，这些方法可以在我们进行数据持久化和查询前后，进行一些操作，比如纪录日志。<br />
先来介绍写有几种callback methods annotation：<br />
@PerPresist 持久化之前调用调用<br />
@PostPersist 持久化之后（官方文档是在Object状态变成 Persist 状态）<br />
@PostLoad 查询之后<br />
@PreUpdate和@PostUpdate 这两个分别是在更新之前和之后，不过这两个必须是在PostLoad之后，对查询出来的数据进行更新，才会触发<br />
@PreRemove和@PostReomve 分别是删除前和删除后（官方文档是Object状态变为deleted 状态）<br />
<br />
下面是我的例子，当然这里只有关键代码：<br />
2010年4月22日 星期四<br />
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;">@PrePersist<br />
</span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;logPrePersist()&nbsp;{<br />
</span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info(</span><span style="color: #000000;">"</span><span style="color: #000000;">PrePersist&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.getTitle());<br />
</span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;@PostPersist<br />
</span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;logPostPersist()&nbsp;{<br />
</span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info(</span><span style="color: #000000;">"</span><span style="color: #000000;">PostPersist&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.getTitle());<br />
</span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;@PostLoad<br />
</span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;logPostLoad()&nbsp;{<br />
</span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info(</span><span style="color: #000000;">"</span><span style="color: #000000;">PostLoad&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.getTitle());<br />
</span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;@PreUpdate<br />
</span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;logPreUpdate()&nbsp;{<br />
</span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info(</span><span style="color: #000000;">"</span><span style="color: #000000;">PreUpdate&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.getTitle());<br />
</span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;@PostUpdate<br />
</span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;logPostUpdate()&nbsp;{<br />
</span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info(</span><span style="color: #000000;">"</span><span style="color: #000000;">PostUpdate&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.getTitle());<br />
</span><span style="color: #008080;">24</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">25</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">26</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;@PreRemove<br />
</span><span style="color: #008080;">27</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;logPreRemove()&nbsp;{<br />
</span><span style="color: #008080;">28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info(</span><span style="color: #000000;">"</span><span style="color: #000000;">PreRemove&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.getTitle());<br />
</span><span style="color: #008080;">29</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">30</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">31</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;@PostRemove<br />
</span><span style="color: #008080;">32</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;logPostRemove()&nbsp;{<br />
</span><span style="color: #008080;">33</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info(</span><span style="color: #000000;">"</span><span style="color: #000000;">PostRemove&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.getTitle());<br />
</span><span style="color: #008080;">34</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}</span></div>
当然也可以用Entity Listeners
，如：<br />
在实体类上加上这个注解：@EntityListeners(value = { MagazineLog.class })<br />
然后写MagazineLog.java:<br />
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;MagazineLog&nbsp;{<br />
</span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/**</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Logger&nbsp;for&nbsp;this&nbsp;class<br />
</span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #008000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br />
</span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">final</span><span style="color: #000000;">&nbsp;Logger&nbsp;logger&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;LoggerFactory.getLogger(MagazineLog.</span><span style="color: #0000ff;">class</span><span style="color: #000000;">);<br />
</span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;"><br />
</span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;@PrePersist<br />
</span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;logPrePersist(Object&nbsp;pc)&nbsp;{<br />
</span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info(</span><span style="color: #000000;">"</span><span style="color: #000000;">PrePersist&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;((Magazine)pc).getTitle());<br />
</span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;@PostPersist<br />
</span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;logPostPersist(Object&nbsp;pc)&nbsp;{<br />
</span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info(</span><span style="color: #000000;">"</span><span style="color: #000000;">PostPersist&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;((Magazine)pc).getTitle());<br />
</span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;@PostLoad<br />
</span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;logPostLoad(Object&nbsp;pc)&nbsp;{<br />
</span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info(</span><span style="color: #000000;">"</span><span style="color: #000000;">PostLoad&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;((Magazine)pc).getTitle());<br />
</span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;@PreUpdate<br />
</span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;logPreUpdate(Object&nbsp;pc)&nbsp;{<br />
</span><span style="color: #008080;">24</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info(</span><span style="color: #000000;">"</span><span style="color: #000000;">PreUpdate&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;((Magazine)pc).getTitle());<br />
</span><span style="color: #008080;">25</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">26</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">27</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;@PostUpdate<br />
</span><span style="color: #008080;">28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;logPostUpdate(Object&nbsp;pc)&nbsp;{<br />
</span><span style="color: #008080;">29</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info(</span><span style="color: #000000;">"</span><span style="color: #000000;">PostUpdate&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;((Magazine)pc).getTitle());<br />
</span><span style="color: #008080;">30</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">31</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">32</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;@PreRemove<br />
</span><span style="color: #008080;">33</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;logPreRemove(Object&nbsp;pc)&nbsp;{<br />
</span><span style="color: #008080;">34</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info(</span><span style="color: #000000;">"</span><span style="color: #000000;">PreRemove&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;((Magazine)pc).getTitle());<br />
</span><span style="color: #008080;">35</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">36</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">37</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;@PostRemove<br />
</span><span style="color: #008080;">38</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;logPostRemove(Object&nbsp;pc)&nbsp;{<br />
</span><span style="color: #008080;">39</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info(</span><span style="color: #000000;">"</span><span style="color: #000000;">PostRemove&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;((Magazine)pc).getTitle());<br />
</span><span style="color: #008080;">40</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">41</span>&nbsp;<span style="color: #000000;">}</span></div>
就这些，这个比较简单<br />
<br />
<img src ="http://www.blogjava.net/kick191/aggbug/319147.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kick191/" target="_blank">天独</a> 2010-04-22 20:01 <a href="http://www.blogjava.net/kick191/articles/319147.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JPA入门-2</title><link>http://www.blogjava.net/kick191/articles/JPA2.html</link><dc:creator>天独</dc:creator><author>天独</author><pubDate>Tue, 06 Apr 2010 14:24:00 GMT</pubDate><guid>http://www.blogjava.net/kick191/articles/JPA2.html</guid><wfw:comment>http://www.blogjava.net/kick191/comments/317597.html</wfw:comment><comments>http://www.blogjava.net/kick191/articles/JPA2.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/kick191/comments/commentRss/317597.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kick191/services/trackbacks/317597.html</trackback:ping><description><![CDATA[这里接上次的一些没有说完的，openJPA的2两个常用属性<br />
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property&nbsp;</span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="openjpa.jdbc.SynchronizeMappings"</span><span style="color: #ff0000;">&nbsp;value</span><span style="color: #0000ff;">="buildSchema"</span><span style="color: #ff0000;">&nbsp;</span><span style="color: #0000ff;">/&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #008000;">&lt;!--</span><span style="color: #008000;">&nbsp;这个是自动创建表的&nbsp;</span><span style="color: #008000;">--&gt;</span><span style="color: #000000;"><br />
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property&nbsp;</span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="openjpa.Log"</span><span style="color: #ff0000;">&nbsp;value</span><span style="color: #0000ff;">="DefaultLevel=WARN,SQL=TRACE"</span><span style="color: #ff0000;">&nbsp;</span><span style="color: #0000ff;">/&gt;</span><span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #008000;">&lt;!--</span><span style="color: #008000;">&nbsp;这是打印SQL语句&nbsp;</span><span style="color: #008000;">--&gt;</span></div>
这两个是比较常用的属性<br />
今天说的是一对多的关联，这里有三个类，非别是User，Post，Comment他们之间的关系是，User中有多个Post和
Comment，Post有多个Comment，而Comment有只有一个User和Post，这里我就不列出来表结构了，因为可以用JPA的自动生成
表<br />
下面是User的关键代码:<br />
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;">@Entity<br />
</span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;User&nbsp;{<br />
</span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;@Id<br />
</span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;@GeneratedValue(strategy&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;GenerationType.SEQUENCE)<br />
</span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;id;<br />
</span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;String&nbsp;name;<br />
</span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;String&nbsp;password;<br />
</span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;String&nbsp;gender;<br />
</span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;@OneToMany(cascade&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;CascadeType.ALL)<br />
</span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Comment</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;comments;<br />
</span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;@OneToMany(cascade&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;CascadeType.ALL)<br />
</span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Post</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;posts;<br />
</span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">}</span></div>
没有什么可说的，只说两点<br />
1.@GeneratedValue(strategy =
GenerationType.SEQUENCE)这是说为id自动生成一个序列，这个选项湖自动在数据库中创建一个用来生成序列的表，一共有四种<br />
<div class="itemizedlist">
<ul type="circle">
    <li>
    <p><code class="literal">GeneratorType.AUTO</code>: The default.
    Assign the  field a generated value, leaving the details to the JPA
    vendor.</p>
    </li>
    <li>
    <p><code class="literal">GenerationType.IDENTITY</code>: The
    database will  assign an identity value on insert.</p>
    </li>
    <li>
    <p><code class="literal">GenerationType.SEQUENCE</code>: Use a
    datastore  sequence to generate a field value.</p>
    </li>
    <li>
    <p><code class="literal">GenerationType.TABLE</code>: Use a sequence
    table  to generate a field value</p>
    </li>
</ul>
</div>
这事Apache官方文档说的， 英文不好我就不翻译了，应该说的很明白，也很容易懂<br />
GenerationValue还有另外一个属性generator这个我只在AUTO下实验了，他有下面几种：<br />
<ul type="disc">
    <li>
    <p><code class="literal">uuid-string</code>: OpenJPA will generate a
    128-bit type 1 UUID unique within the network, represented as a
    16-character string. For  more information on UUIDs, see the IETF UUID
    draft specification at: <a href="http://www1.ics.uci.edu/%7Eejw/authoring/uuid-guid/" target="_top">
    http://www1.ics.uci.edu/~ejw/authoring/uuid-guid/</a></p>
    </li>
    <li>
    <p><code class="literal">uuid-hex</code>: Same as <code class="literal">  uuid-string</code>, but represents the type 1 UUID as a
    32-character hexadecimal string.</p>
    </li>
    <li>
    <p><code class="literal">uuid-type4-string</code>: OpenJPA will
    generate a  128-bit type 4 pseudo-random UUID, represented as a
    16-character string. For more information on UUIDs, see the IETF UUID
    draft specification at: <a href="http://www1.ics.uci.edu/%7Eejw/authoring/uuid-guid/" target="_top">
    http://www1.ics.uci.edu/~ejw/authoring/uuid-guid/</a></p>
    </li>
    <li>
    <p><code class="literal">uuid-type4-hex</code>: Same as <code class="literal"> uuid-type4-string</code> , but represents the type 4
    UUID as a 32-character hexadecimal string.</p>
    </li>
</ul>
<br />
2.@OneToMany(cascade = CascadeType.ALL),级联，我这里是所有，级联也是有4中（还是考的官方文档）：<br />
<ul type="disc">
    <li>
    <p><code class="literal">CascadeType.PERSIST</code>: When persisting
    an  entity, also persist the entities held in this field. We suggest
    liberal application of this  cascade rule, because if the <code class="classname">EntityManager</code> finds a  field that references a
    new entity during flush, and the field does not use <code class="literal"> CascadeType.PERSIST</code>, it is an error.</p>
    </li>
    <li>
    <p><code class="literal">CascadeType.REMOVE</code>: When deleting an
    entity, also delete the entities held in this field.</p>
    </li>
    <li>
    <p><code class="literal">CascadeType.REFRESH</code>: When refreshing
    an  entity, also refresh the entities held in this field.</p>
    </li>
    <li>
    <p><code class="literal">CascadeType.MERGE</code>: When merging
    entity  state, also merge the entities held in this field.</p>
    </li>
</ul>
具体的还是要看官方文档，说的很详细<br />
这里也可以使用其中的几种，1种或着多重，例如：@ManyToOne(cascade=
{CascadeType.PERSIST,CascadeType.REMOVE,CascadeType.REFRESH,CascadeType.MERGE})<br />
下来是Post和Comment的关键代码：<br />
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;">@Entity<br />
</span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;Post&nbsp;{<br />
</span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;@Id<br />
</span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;@GeneratedValue(strategy&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;GenerationType.SEQUENCE)<br />
</span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;id;<br />
</span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;Date&nbsp;createDate;<br />
</span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;String&nbsp;title;<br />
</span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;String&nbsp;content;<br />
</span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;@ManyToOne(cascade&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;CascadeType.ALL)<br />
</span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;User&nbsp;author;<br />
</span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;@OneToMany(cascade&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;CascadeType.ALL)<br />
</span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Comment</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;comments;<br />
</span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">}<br />
</span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;"><br />
</span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">@Entity<br />
</span><span style="color: #008080;">17</span>&nbsp;<span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;Comment&nbsp;{<br />
</span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;@Id<br />
</span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;@GeneratedValue(strategy&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;GenerationType.SEQUENCE)<br />
</span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;id;<br />
</span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;Date&nbsp;createDate;<br />
</span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;String&nbsp;title;<br />
</span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;String&nbsp;content;<br />
</span><span style="color: #008080;">24</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;@ManyToOne(cascade&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;CascadeType.ALL)<br />
</span><span style="color: #008080;">25</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;User&nbsp;author;<br />
</span><span style="color: #008080;">26</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;@ManyToOne(cascade&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;CascadeType.ALL)<br />
</span><span style="color: #008080;">27</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;Post&nbsp;post;<br />
</span><span style="color: #008080;">28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">29</span>&nbsp;<span style="color: #000000;">}</span></div>
<span style="color: #000000;">下来的应该没有什么可说的，下面是我的测试代码：<br />
</span>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;">&nbsp;EntityManagerFactory&nbsp;factory&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;Persistence.createEntityManagerFactory(</span><span style="color: #000000;">"</span><span style="color: #000000;">test</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
</span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">EntityManager&nbsp;em&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;factory.createEntityManager();<br />
</span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">em.getTransaction().begin();<br />
</span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;">User&nbsp;user&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;User();<br />
</span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;">user.setName(</span><span style="color: #000000;">"</span><span style="color: #000000;">Innate</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
</span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">user.setPassword(</span><span style="color: #000000;">"</span><span style="color: #000000;">Innate</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
</span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">user.setGender(</span><span style="color: #000000;">"</span><span style="color: #000000;">男</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
</span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Post</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;posts&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;ArrayList</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Post</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">();<br />
</span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;"><br />
</span><span style="color: #008080;">10</span>&nbsp;<span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">5</span><span style="color: #000000;">;&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br />
</span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;Post&nbsp;post&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Post();<br />
</span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;post.setAuthor(user);<br />
</span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;post.setContent(</span><span style="color: #000000;">"</span><span style="color: #000000;">test</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
</span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;post.setCreateDate(</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Date());<br />
</span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;post.setTitle(</span><span style="color: #000000;">"</span><span style="color: #000000;">Innate</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
</span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;posts.add(post);<br />
</span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Comment</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;comments&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;ArrayList</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Comment</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">();<br />
</span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;j&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;j&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">5</span><span style="color: #000000;">;&nbsp;j</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br />
</span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;Comment&nbsp;comment&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Comment();<br />
</span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;comment.setAuthor(user);<br />
</span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;comment.setContent(</span><span style="color: #000000;">"</span><span style="color: #000000;">testtest</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
</span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;comment.setCreateDate(</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Date());<br />
</span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;comment.setPost(post);<br />
</span><span style="color: #008080;">24</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;comment.setTitle(</span><span style="color: #000000;">"</span><span style="color: #000000;">InnateInnate</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
</span><span style="color: #008080;">25</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;comments.add(comment);<br />
</span><span style="color: #008080;">26</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">27</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;post.setComments(comments);<br />
</span><span style="color: #008080;">28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;user.setComments(comments);<br />
</span><span style="color: #008080;">29</span>&nbsp;<span style="color: #000000;">}<br />
</span><span style="color: #008080;">30</span>&nbsp;<span style="color: #000000;">user.setPosts(posts);<br />
</span><span style="color: #008080;">31</span>&nbsp;<span style="color: #000000;">em.persist(user);<br />
</span><span style="color: #008080;">32</span>&nbsp;<span style="color: #000000;">em.getTransaction().commit();</span></div>
源代码（包含1，2的）:<a title="JTATest.7z" href="http://d.namipan.com/d/48df0623558f1e23a64678cfdec02c080e59dde99f6b6400">JTATest.7z</a><br />
<iframe style="position: absolute; display: block; opacity: 0.7; z-index: 500; width: 20px; height: 26px; top: 730px; right: 911px;" src="http://www.google.com/notebook/static_files/blank.html" id="gnotes-notemagic" frameborder="0"></iframe>
<img src ="http://www.blogjava.net/kick191/aggbug/317597.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kick191/" target="_blank">天独</a> 2010-04-06 22:24 <a href="http://www.blogjava.net/kick191/articles/JPA2.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JPA入门-1</title><link>http://www.blogjava.net/kick191/articles/JPA1.html</link><dc:creator>天独</dc:creator><author>天独</author><pubDate>Sat, 03 Apr 2010 08:01:00 GMT</pubDate><guid>http://www.blogjava.net/kick191/articles/JPA1.html</guid><wfw:comment>http://www.blogjava.net/kick191/comments/317394.html</wfw:comment><comments>http://www.blogjava.net/kick191/articles/JPA1.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kick191/comments/commentRss/317394.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kick191/services/trackbacks/317394.html</trackback:ping><description><![CDATA[这里我用的是openJPA<br />
persistence.xml文件的配置<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 128);">&nbsp;1</span>&nbsp;<span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">persistence-unit&nbsp;</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="test"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;2</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">cn.is.test.Test</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;3</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;4</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">properties</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;5</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property&nbsp;</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="openjpa.ConnectionURL"</span><span style="color: rgb(255, 0, 0);">&nbsp;value</span><span style="color: rgb(0, 0, 255);">="jdbc:mysql://localhost:3306/test"</span><span style="color: rgb(255, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;6</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property&nbsp;</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="openjpa.ConnectionDriverName"</span><span style="color: rgb(255, 0, 0);">&nbsp;value</span><span style="color: rgb(0, 0, 255);">="com.mysql.jdbc.Driver"</span><span style="color: rgb(255, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;7</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property&nbsp;</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="openjpa.ConnectionUserName"</span><span style="color: rgb(255, 0, 0);">&nbsp;value</span><span style="color: rgb(0, 0, 255);">="root"</span><span style="color: rgb(255, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;8</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">property&nbsp;</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="openjpa.ConnectionPassword"</span><span style="color: rgb(255, 0, 0);">&nbsp;value</span><span style="color: rgb(0, 0, 255);">="root"</span><span style="color: rgb(255, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;9</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">properties</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">10</span>&nbsp;<span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">persistence-unit</span><span style="color: rgb(0, 0, 255);">&gt;</span></div>
<br />
这里有个问题，我一般用mysql驱动的org.gjt.mm.mysql.Driver这个驱动类，但是在这里，运行的时候报驱动类或着连接URL错误<br />
<br />
<br />
下面的是实体类<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 128);">&nbsp;1</span>&nbsp;<span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);">&nbsp;cn.is.test;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;2</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;3</span>&nbsp;<span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;java.io.Serializable;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;4</span>&nbsp;<span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;javax.persistence.Entity;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;5</span>&nbsp;<span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;javax.persistence.Id;<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;6</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;7</span>&nbsp;<span style="color: rgb(0, 0, 0);">@Entity<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;8</span>&nbsp;<span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;Test&nbsp;</span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);">&nbsp;Serializable&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;9</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;@Id<br />
</span><span style="color: rgb(0, 128, 128);">10</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);">&nbsp;String&nbsp;id;<br />
</span><span style="color: rgb(0, 128, 128);">11</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">12</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);">&nbsp;String&nbsp;userName;<br />
</span><span style="color: rgb(0, 128, 128);">13</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">14</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);">&nbsp;String&nbsp;password;<br />
</span><span style="color: rgb(0, 128, 128);">15</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">16</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);">&nbsp;String&nbsp;sex;<br />
</span><span style="color: rgb(0, 128, 128);">17</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">18</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);">&nbsp;serialVersionUID&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1L</span><span style="color: rgb(0, 0, 0);">;<br />
</span><span style="color: rgb(0, 128, 128);">19</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">20</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;Test()&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">21</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">super</span><span style="color: rgb(0, 0, 0);">();<br />
</span><span style="color: rgb(0, 128, 128);">22</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">23</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">24</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;String&nbsp;getId()&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">25</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.id;<br />
</span><span style="color: rgb(0, 128, 128);">26</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">27</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">28</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;setId(String&nbsp;id)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">29</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.id&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;id;<br />
</span><span style="color: rgb(0, 128, 128);">30</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">31</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">32</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;String&nbsp;getUserName()&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">33</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.userName;<br />
</span><span style="color: rgb(0, 128, 128);">34</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">35</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">36</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;setUserName(String&nbsp;username)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">37</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.userName&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;username;<br />
</span><span style="color: rgb(0, 128, 128);">38</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">39</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">40</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;String&nbsp;getPassword()&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">41</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.password;<br />
</span><span style="color: rgb(0, 128, 128);">42</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">43</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">44</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;setPassword(String&nbsp;password)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">45</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.password&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;password;<br />
</span><span style="color: rgb(0, 128, 128);">46</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">47</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">48</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;String&nbsp;getSex()&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">49</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.sex;<br />
</span><span style="color: rgb(0, 128, 128);">50</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">51</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">52</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;setSex(String&nbsp;sex)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">53</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.sex&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;sex;<br />
</span><span style="color: rgb(0, 128, 128);">54</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;}<br />
</span><span style="color: rgb(0, 128, 128);">55</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">56</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">57</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">58</span>&nbsp;<span style="color: rgb(0, 0, 0);">}<br />
</span><span style="color: rgb(0, 128, 128);">59</span>&nbsp;</div>
<br />
下来的是测试代码：<br />
<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 128);">&nbsp;1</span>&nbsp;<span style="color: rgb(0, 0, 0);">EntityManagerFactory&nbsp;factory&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;Persistence.createEntityManagerFactory(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">test</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;2</span>&nbsp;<span style="color: rgb(0, 0, 0);">EntityManager&nbsp;em&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;factory.createEntityManager();<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;3</span>&nbsp;<span style="color: rgb(0, 0, 0);">Test&nbsp;test&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;Test();<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;4</span>&nbsp;<span style="color: rgb(0, 0, 0);">test.setId(UUID.randomUUID().toString());<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;5</span>&nbsp;<span style="color: rgb(0, 0, 0);">test.setSex(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">男</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;6</span>&nbsp;<span style="color: rgb(0, 0, 0);">test.setUserName(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Innate</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;7</span>&nbsp;<span style="color: rgb(0, 0, 0);">test.setPassword(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">solitary</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;8</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">&nbsp;9</span>&nbsp;<span style="color: rgb(0, 0, 0);">em.getTransaction().begin();<br />
</span><span style="color: rgb(0, 128, 128);">10</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">11</span>&nbsp;<span style="color: rgb(0, 0, 0);">em.persist(test);<br />
</span><span style="color: rgb(0, 128, 128);">12</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">13</span>&nbsp;<span style="color: rgb(0, 0, 0);">em.getTransaction().commit();<br />
</span><span style="color: rgb(0, 128, 128);">14</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">15</span>&nbsp;<span style="color: rgb(0, 0, 0);">em.close();<br />
</span><span style="color: rgb(0, 128, 128);">16</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">17</span>&nbsp;<span style="color: rgb(0, 0, 0);">em&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;factory.createEntityManager();<br />
</span><span style="color: rgb(0, 128, 128);">18</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">19</span>&nbsp;<span style="color: rgb(0, 0, 0);">Query&nbsp;query&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;em.createQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">select&nbsp;t&nbsp;from&nbsp;Test&nbsp;t</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />
</span><span style="color: rgb(0, 128, 128);">20</span>&nbsp;<span style="color: rgb(0, 0, 0);">List</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">Test</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;testList&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;query.getResultList();<br />
</span><span style="color: rgb(0, 128, 128);">21</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">22</span>&nbsp;<span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;(Test&nbsp;_test&nbsp;:&nbsp;testList)&nbsp;{<br />
</span><span style="color: rgb(0, 128, 128);">23</span>&nbsp;<span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;System.out.println(_test.getUserName());<br />
</span><span style="color: rgb(0, 128, 128);">24</span>&nbsp;<span style="color: rgb(0, 0, 0);">}<br />
</span><span style="color: rgb(0, 128, 128);">25</span>&nbsp;<span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 128);">26</span>&nbsp;<span style="color: rgb(0, 0, 0);">em.close();<br />
</span><span style="color: rgb(0, 128, 128);">27</span>&nbsp;<span style="color: rgb(0, 0, 0);">factory.close();</span></div>
<br />
<img src ="http://www.blogjava.net/kick191/aggbug/317394.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kick191/" target="_blank">天独</a> 2010-04-03 16:01 <a href="http://www.blogjava.net/kick191/articles/JPA1.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>