Hibernate:Mapping a class several times (from a book)
Mapping a class several times
Imagine that you have several tables with some columns in common. For example, you could have ITEM_AUCTION and ITEM_SALE tables. Usually you map each table to an entity persistent class, ItemAuction and ItemSale respectively. With the help of entity names, you can save work and implement a single persistent class.
To map both tables to a single persistent class, use different entity names (and usually different property mappings):
…
entity-name=”ItemSale” table=”ITEM_SALE”>
…
The model.Item persistent class has all the properties you mapped: id, description, initialPrice, and salesPrice. Depending on the entity name you use at runtime, some properties are considered persistent and others transient:
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”);
Domain models and metadata
itemForSale.setSalesPrice( new BigDecimal(123) );
session.save(“ItemSale”, itemForSale);
Thanks to the logical entity name, Hibernate knows into which table it should insert the data. Depending on the entity name you use for loading and querying entities, Hibernate selects from the appropriate table.
Rohit Prabhakar
www.rohitprabhakar.com
content from http://prabhakars.blogspot.com/atom.xml
Rohit on Facebook
Tweets
- Divan Indo-Pak Cuisine on #Yelp: Went there for the first time and the food was delicious. The place is very high e... http://t.co/SZohU9A3 4 hours ago
- Beat way to eliminate distractions, die! There is no other way. You can overcome distractions, but not eliminate. #BWW 3 weeks ago
- Happy new year! 1 month ago
- Check this video out -- The difference: What 40 Dollars Means http://t.co/dcXffbJq via @youtube 1 month ago
- PMA is the starting point of all riches, whether they be riches of a material nature or intangible riches #napoleanhill 1 month ago














