CHAPTER 3 Domain models and metadata is in POJO entity mode, so the following operations return instances of model.ItemPojo: Long storedItemId = item1.getId(); ItemPojo loadedItemPojo = (ItemPojo) session.load(”ItemEntity”, storedItemId); List queriedItemPojos = session.createQuery(”from ItemEntity where initialPrice >= :p”) .setParameter(”p”, new BigDecimal(100)) .list(); You can switch to a dynamic map representation either globally or temporarily, but a global switch of the entity mode has serious consequences. To switch globally, add the following to your Hibernate configuration; e.g., in hibernate.cfg.xml:
dynamic-map All Session operations now either expect or return dynamically typed maps! The previous code examples that stored, loaded, and queried POJO instances no longer work; you need to store and load maps. It s more likely that you want to switch to another entity mode temporarily, so let s assume that you leave the SessionFactory in the default POJO mode. To switch to dynamic maps in a particular Session, you can open up a new temporary Session on top of the existing one. The following code uses such a temporary Session to store a new auction item for an existing seller: Session dynamicSession = session.getSession(EntityMode.MAP); Map seller = (Map) dynamicSession.load(”UserEntity”, user.getId() ); Map newItemMap = new HashMap(); newItemMap.put(”description”, “An item for auction”); newItemMap.put(”initialPrice”, new BigDecimal(99)); newItemMap.put(”seller”, seller); dynamicSession.save(”ItemEntity”, newItemMap); Long storedItemId = (Long) newItemMap.get(”id”); Map loadedItemMap = (Map) dynamicSession.load(”ItemEntity”, storedItemId); List queriedItemMaps = dynamicSession .createQuery(”from ItemEntity where initialPrice >= :p”) .setParameter(”p”, new BigDecimal(100)) .list(); The temporary dynamicSession that is opened with getSession() doesn t need to be flushed or closed; it inherits the context of the original Session. You use it
Check Tomcat Web Hosting services for best quality webspace to host your web application.
This entry was posted
on Wednesday, November 28th, 2007 at 10:21 am and is filed under PHP5.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.