📄 xml.html
字号:
embed-xml="true"> <key column="CUSTOMER_ID" not-null="true"/> <map-key column="SHORT_DESC" node="@short-desc" type="string"/> <one-to-many entity-name="Account" embed-xml="false" node="account"/> </map> <component name="name" node="name"> <property name="firstName" node="first-name"/> <property name="initial" node="initial"/> <property name="lastName" node="last-name"/> </component> ... </class></pre><p> 在这个例子中,我们决定嵌入帐目号码(account id)的集合,但不嵌入实际的帐目数据。下面的HQL查询: </p><pre class="programlisting">from Customer c left join fetch c.accounts where c.lastName like :lastName</pre><p> 返回的数据集将是这样: </p><pre class="programlisting"><customer id="123456789"> <account id="987632567" short-desc="Savings"/> <account id="985612323" short-desc="Credit Card"/> <name> <first-name>Gavin</first-name> <initial>A</initial> <last-name>King</last-name> </name> ...</customer></pre><p> 如果你把一对多映射<tt class="literal"><one-to-many></tt>的embed-xml属性置为真(<tt class="literal">embed-xml="true"</tt>), 则数据看上去就像这样: </p><pre class="programlisting"><customer id="123456789"> <account id="987632567" short-desc="Savings"> <customer id="123456789"/> <balance>100.29</balance> </account> <account id="985612323" short-desc="Credit Card"> <customer id="123456789"/> <balance>-2370.34</balance> </account> <name> <first-name>Gavin</first-name> <initial>A</initial> <last-name>King</last-name> </name> ...</customer></pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="xml-manipulation"></a>18.3. 操作XML数据</h2></div></div><div></div></div><p> 让我们来读入和更新应用程序中的XML文档。通过获取一个dom4j会话可以做到这一点: </p><pre class="programlisting">Document doc = ....; Session session = factory.openSession();Session dom4jSession = session.getSession(EntityMode.DOM4J);Transaction tx = session.beginTransaction();List results = dom4jSession .createQuery("from Customer c left join fetch c.accounts where c.lastName like :lastName") .list();for ( int i=0; i<results.size(); i++ ) { //add the customer data to the XML document Element customer = (Element) results.get(i); doc.add(customer);}tx.commit();session.close();</pre><pre class="programlisting">Session session = factory.openSession();Session dom4jSession = session.getSession(EntityMode.DOM4J);Transaction tx = session.beginTransaction();Element cust = (Element) dom4jSession.get("Customer", customerId);for ( int i=0; i<results.size(); i++ ) { Element customer = (Element) results.get(i); //change the customer name in the XML and database Element name = customer.element("name"); name.element("first-name").setText(firstName); name.element("initial").setText(initial); name.element("last-name").setText(lastName);}tx.commit();session.close();</pre><p> 将这一特色与Hibernate的<tt class="literal">replicate()</tt>操作结合起来对于实现的基于XML的数据导入/导出将非常有用. </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="filters.html">上一页</a> </td><td width="20%" align="center"><a accesskey="u" href="index.html">上一级</a></td><td width="40%" align="right"> <a accesskey="n" href="performance.html">下一页</a></td></tr><tr><td width="40%" align="left" valign="top">第 17 章 过滤数据 </td><td width="20%" align="center"><a accesskey="h" href="index.html">起始页</a></td><td width="40%" align="right" valign="top"> 第 19 章 提升性能 </td></tr></table></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -