動(dòng)態(tài)domain,用一個(gè)Map來(lái)代替對像,把原來(lái)domain中的屬性做key值存進(jìn)來(lái)
實(shí)體映射
<hibernate-mapping>
<class entity-name="ItemEntity" table="ITEM_ENTITY">
<id name="id" type="long" column="ITEM_ID">
<generator class="native"/>
</id>
<property name="initialPrice"
type="big_decimal"
column="INIT_PRICE"/>
<property name="description"
type="string"
column="DESCRIPTION"/>
<many-to-one name="seller"
entity-name="UserEntity"
column="USER_ID"/>
</class>
<class entity-name="UserEntity" table="USER_ENTITY">
<id name="id" type="long" column="USER_ID">
<generator class="native"/>
</id>
<property name="username"
type="string"
column="USERNAME"/>
<bag name="itemsForSale" inverse="true" cascade="all">
<key column="USER_ID"/>
<one-to-many entity-name="ItemEntity"/>
</bag>
</class>
</hibernate-mapping>
注意:
1.<class name="...">變?yōu)?lt;class entity-name="...">
2.<many-to-one>和<one-to-many> 中的class屬性變?yōu)閑ntity-name
動(dòng)態(tài)domain的工作方式
Map user = new HashMap();
user.put("username","davide");
Map item1 = new HashMap();
item1.put("description","an item for auction");
item1.put("initialPrice",new BigDecimal(99));
item1.put("seller",user);
Map item2 = new HashMap();
item2.put("description", "Another item for auction");
item2.put("initialPrice", new BigDecimal(123));
item2.put("seller", user);
Collection itemsForSale = new ArrayList();
itemsForSale.add(item1);
itemsForSale.add(item2);
user.put("itemsForSale", itemsForSale);
session.save("UserEntity", user);
第一個(gè)Map為UserEntity,接下來(lái)的兩個(gè)是ItemEntitys
為兩個(gè)Map建立了seller user鏈接.
一個(gè)Collection在inverse方設置one-to-many關(guān)聯(lián)初始化
使用方法
Long storedItemId = (Long) item1.get("id");
Map loadedItemMap = (Map) session.load("ItemEntity", storedItemId);
loadedItemMap.put("initialPrice", new BigDecimal(100));
多次映射一個(gè)類(lèi)
<hibernate-mapping>
<class name="model.Item"
entity-name="ItemAuction"
table="ITEM_AUCTION">
<id name="id" column="ITEM_AUCTION_ID">...</id>
<property name="description" column="DESCRIPTION"/>
<property name="initialPrice" column="INIT_PRICE"/>
</class>
<class name="model.Item"
entity-name="ItemSale"
table="ITEM_SALE">
<id name="id" column="ITEM_SALE_ID">...</id>
<property name="description" column="DESCRIPTION"/>
<property name="salesPrice" column="SALES_PRICE"/>
</class>
</hibernate-mapping>
model.Item持久化類(lèi)映射了id,description,initialPrice,salesPrice屬性.
處決于你運行時(shí)的實(shí)體名,有些屬性是持久化的有的則不是.
Item itemForAuction = new Item();
itemForAuction.setDescription("An item for auction");
itemForAuction.setInitialPrice( new BigDecimal(99) );
session.save("ItemAuction", itemForAuction);
Item itemForSale = new Item();
itemForSale.setDescription("An item for sale");
itemForSale.setSalesPrice( new BigDecimal(123) );
session.save("ItemSale", itemForSale);
正是由于有了邏輯名,hibernate才知道向哪個(gè)表中插入數據.
將數據保存到xml文件
Session dom4jSession = session.getSession(EntityMode.DOM4J);
Element userXML =
(Element) dom4jSession.load(User.class, storedUserId);
可以通過(guò)以下方式打印到控件臺
try {
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter( System.out, format);
writer.write( userXML );
} catch (IOException ex) {
throw new RuntimeException(ex);
}
若是繼續的前面的例子,你可能會(huì )得以下結果
<User>
<id>1</id>
<username>johndoe</username>
<itemsForSale>
<Item>
<id>2</id>
<initialPrice>99</initialPrice>
<description>An item for auction</description>
<seller>1</seller>
</Item>
<Item>
<id>3</id>
<initialPrice>123</initialPrice>
<description>Another item for auction</description>
<seller>1</seller>
</Item>
</itemsForSale>
</User>
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。