转:hibernate映射视图的两种方式
程序员文章站
2022-03-04 22:01:40
...
本文转自:http://blog.csdn.net/id19870510/article/details/5453965
1.数据库已经建立视图,hibernate只是把视图当作普通的表来映射。
视图VIEW_MER_INST_POS:
select MER.DAYS_MERCHT_ID MER_ID, INST.DAYS_MERCHT_ID INST_ID, POS.POS_ID POS_ID from tbl_days_mercht_attr MER, tbl_days_mercht_info INST, tbl_days_mercht_pos_info POS where MER.days_mercht_id = INST.up_days_mercht_id and INST.days_mercht_id = POS.days_mercht_id
hbm.xml配置:
<class name="db.po.ViewMerInstPos" table="VIEW_MER_INST_POS"> <composite-id> <key-property name="merId" column="MER_ID" type="java.lang.String" length="8"/> <key-property name="instId" column="INST_ID" type="java.lang.String" length="8"/> <key-property name="posId" column="POS_ID" type="java.lang.String" length="8"/> </composite-id> </class>
2.数据库没有视图,用hibernate自己做视图映射:
hbm配置如下:
<hibernate-mapping package="huntersxp.db.pojo"> <class name="ViewMerInstPos"> <meta attribute="sync-DAO">false</meta> <subselect> select MER.DAYS_MERCHT_ID MER_ID,INST.SHOP_NM SHOP_NM,POS.POS_ID POS_ID from tbl_days_mercht_attr MER, tbl_days_mercht_info INST, tbl_days_mercht_pos_info POS where MER.days_mercht_id = INST.up_days_mercht_id and INST.days_mercht_id = POS.days_mercht_id </subselect> <synchronize table="tbl_days_mercht_attr"/> <synchronize table="tbl_days_mercht_info"/> <synchronize table="tbl_days_mercht_pos_info"/> <composite-id> <key-property name="merId" column="MER_ID" type="java.lang.String" length="8"/> <key-property name="shopNm" column="SHOP_NM" type="java.lang.String" length="40" /> <key-property name="posId" column="POS_ID" type="java.lang.String" length="40"/> </composite-id> </class> </hibernate-mapping>其中synchronize表示当表的数据发生变化的时候,视图的数据也会发生变化。