Session dom4jSession = session.getSession(EntityMode.DOM4J); Element userXML = (Element) dom4jSession.get(UserPojo.class,new Long(1));此处返回的是一个dom4j Element,可以用这个dom4j API来读取和操作它。例如,可以用下列片段把它很漂亮地打印到控制台:
try { OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(System.out, format); writer.write(userXML); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }假设你重用来自前面那些例子中的POJO类和数据,你会看到一个UserPojo实例和两个ItemPojo实例。
<UserPojo> <id>1</id> <username>zhangsan</username> <itemsForSale> <Item> <id>2</id> <initialPrice>99</initialPrice> <description>An item for auction</description> <seller>1</seller> </Item> <Item> <id>3</id> <initialPrice>1000</initialPrice> <description>Another item for auction</description> <seller>1</seller> </Item> </itemsForSale> </UserPojo>注意:此时,如果你运行,应该会报错JVM溢出异常,解决方案
<hibernate-mapping package="cn.jbit.entity"> <class name="ItemPojo" table="TBL_ITEM_ENTITY" node="item"> <id name="id" column="ITEM_ID" type="long" node="@id"> <generator class="sequence"> <param name="sequence">SEQ_ITEM_ENTITY_ID</param> </generator> </id> <property name="initialPrice" type="big_decimal" column="INIT_PRICE" node="item-details/@inital-price" /> <property name="description" type="string" column="DESCRIPTION" node="item-details/@description" /> <many-to-one name="seller" class="UserPojo" column="USER_ID" embed-xml="false" node="@seller-id" /> </class> <class name="UserPojo" table="TBL_USER_ENTITY"> <id name="id" column="USER_ID" type="long" node="@id"> <generator class="sequence"> <param name="sequence">SEQ_USER_ENTITY_ID</param> </generator> </id> <property name="username" type="string" column="UNAME" node="@username" /> <bag name="itemsForSale" inverse="true" cascade="all" embed-xml="true" node="item-for-sale"> <key column="USER_ID" /> <one-to-many class="ItemPojo" /> </bag> </class> </hibernate-mapping>每一个node属性都定义了XML表示法:
<UserPojo id="1" username="zhangsan"> <item-for-sale> <item id="1" seller-id="1"> <item-details inital-price="100" description="description 1"/> </item> <item id="2" seller-id="1"> <item-details inital-price="400045" description="description 2"/> </item> </item-for-sale> </UserPojo>最后,以XML表示的数据是事务和持久化的,因此你可以修改被查询的XML元素,并让Hibernate更新底层的表:
Element itemXML = (Element) dom4jSession.get(ItemPojo.class,new Long(2)); itemXML.element("item-details").attribute("inital-price").setValue("100000"); dom4jSession.flush();//Hibernate executes UPDATES 可以理解为本来应该延迟至事务提交修改,而现在立刻执行修改 Element userXML = (Element) dom4jSession.get(UserPojo.class,new Long(1)); Element newItem = DocumentHelper.createElement("item"); Element newItemDetails = newItem.addElement("item-details"); newItem.addAttribute("seller-id", userXML.attribute("id").getValue());// 设置关联关系 // 添加商品详细信息 newItemDetails.addAttribute("inital-price", "9999"); newItemDetails.addAttribute("description", "描述信息"); dom4jSession.save(Item.class.getName(), newItem); dom4jSession.flush();最后,注解如果你喜欢,可以同时使用所有三种内建的实体模式。可以映射领域模型的一个静态的POJO实现,为一般的用户界面切换到动态的映射,并把数据导到XML里面。或者,可以编写一个没有任何领域类的应用,只有动态的映射和XML。可是,必须提醒你,软件行业中的原型经常意味着,客户以这个没有人想要抛弃的原型而告终——你愿意买一辆样板车吗?强烈建议你依赖静态的领域模型,如果要创建可维护的系统的话。
Hibernate实战_笔记22(表示XML中的数据),布布扣,bubuko.com
原文:http://blog.csdn.net/com185272358/article/details/21255351