hibernate 动态pojo Map

动态domain,用一个Map来代替对像,把原来domain中的属性做key值存进来
实体映射
<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="...">变为<class entity-name="...">
2.<many-to-one>和<one-to-many> 中的class属性变为entity-name

动态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);
第一个Map为UserEntity,接下来的两个是ItemEntitys
为两个Map建立了seller user链接.
一个Collection在inverse方设置one-to-many关联初始化

使用方法
Long storedItemId = (Long) item1.get("id");
Map loadedItemMap = (Map) session.load("ItemEntity", storedItemId);
loadedItemMap.put("initialPrice", new BigDecimal(100));

多次映射一个类
<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持久化类映射了id,description,initialPrice,salesPrice属性.
处决于你运行时的实体名,有些属性是持久化的有的则不是.
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才知道向哪个表中插入数据.

将数据保存到xml文件
Session dom4jSession = session.getSession(EntityMode.DOM4J);
Element userXML =
(Element) dom4jSession.load(User.class, storedUserId);
可以通过以下方式打印到控件台
try {
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter( System.out, format);
writer.write( userXML );
} catch (IOException ex) {
throw new RuntimeException(ex);
}
若是继续的前面的例子,你可能会得以下结果
<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值