Hibernate:视图映射

虽然Hibernate提供的HQL可以实现多表联合查询,但是我们一般都是将多表查询直接创建为一个视图,直接查询视图就可以了。那么Hibernate怎样映射视图呢?

在网上找了一篇较实用的文章现分享给大家。

文章链接:http://blog.csdn.net/kingkuang2006/article/details/6981974


由于项目(ssh)有需求根据关键字查询位于同一服务器下不同数据库的2张表t_navi和t_news,由于涉及到分页查询,故不想自己手动去写sql语句来实现跨表查询,不但麻烦而且容易写错,所以想用Hibernate视图来完成该功能,因此上网查看了一些资料,并最终完美解决,故将解决方案记录如下:

 一、首先创建一个跨数据库视图

mysql>create view db_cms.search_view as(select * from db_cms.t_navi)union all (select * from db_news.t_news);

注:由于t_navi 和 t_news两张表字段数量和意义完全相同,所以这里使用了union all;

二、生成POJO类和Hibernate配置文件

       如果怕自己容易写错,可以使用Myeclipse通过视图反向生成POJO类和**.hbm.xml,如果通过Myeclipse生成的话,将生成如下几个文件:

       SearchView.java            SearchViewId.java              SearchView.hbm.xml

扫描二维码关注公众号,回复: 3868352 查看本文章

       1、SearchView.java

[java]  view plain copy print ?
  1. public class SearchView implements Serializable {  
  2.     /** 
  3.      *  
  4.      */  
  5.     private static final long serialVersionUID = -1372050399492830775L;  
  6.     private SearchViewId id;  
  7.       
  8.     public SearchViewId getId() {  
  9.         return id;  
  10.     }  
  11.   
  12.     public void setId(SearchViewId id) {  
  13.         this.id = id;  
  14.     }  
  15. }  

        2、SearchViewId.java

[java]  view plain copy print ?
  1. public class SearchViewId implements Serializable {  
  2.     private static final long serialVersionUID = -2960868353091674237L;  
  3.     private Integer id;  
  4.     private String naviTitle;  
  5.     private Boolean naviShow = true;  
  6.     private Boolean treeShow = true;  
  7.     private Boolean jumpHref = false;  
  8.     private Boolean windowOpen = false;  
  9.     private Integer auditStatus;  
  10.     private Date createDate;  
  11.     private Date updateDate;  
  12.     private String content;  
  13.     private String accessPath;  
  14.     private String jumpHrefUrl;  
  15.       
  16.     public Integer getId() {  
  17.         return id;  
  18.     }  
  19.   
  20.     public void setId(Integer id) {  
  21.         this.id = id;  
  22.     }  
  23.   
  24.     public String getNaviTitle() {  
  25.         return naviTitle;  
  26.     }  
  27.   
  28.     public void setNaviTitle(String naviTitle) {  
  29.         this.naviTitle = naviTitle;  
  30.     }  
  31.   
  32.     public Boolean getNaviShow() {  
  33.         return naviShow;  
  34.     }  
  35.   
  36.     public void setNaviShow(Boolean naviShow) {  
  37.         this.naviShow = naviShow;  
  38.     }  
  39.   
  40.     public Boolean getTreeShow() {  
  41.         return treeShow;  
  42.     }  
  43.   
  44.     public void setTreeShow(Boolean treeShow) {  
  45.         this.treeShow = treeShow;  
  46.     }  
  47.   
  48.     public Boolean getJumpHref() {  
  49.         return jumpHref;  
  50.     }  
  51.   
  52.     public void setJumpHref(Boolean jumpHref) {  
  53.         this.jumpHref = jumpHref;  
  54.     }  
  55.   
  56.     public Boolean getWindowOpen() {  
  57.         return windowOpen;  
  58.     }  
  59.   
  60.     public void setWindowOpen(Boolean windowOpen) {  
  61.         this.windowOpen = windowOpen;  
  62.     }  
  63.   
  64.     public Integer getAuditStatus() {  
  65.         return auditStatus;  
  66.     }  
  67.   
  68.     public void setAuditStatus(Integer auditStatus) {  
  69.         this.auditStatus = auditStatus;  
  70.     }  
  71.   
  72.     public Date getCreateDate() {  
  73.         return createDate;  
  74.     }  
  75.   
  76.     public void setCreateDate(Date createDate) {  
  77.         this.createDate = createDate;  
  78.     }  
  79.   
  80.     public Date getUpdateDate() {  
  81.         return updateDate;  
  82.     }  
  83.   
  84.     public void setUpdateDate(Date updateDate) {  
  85.         this.updateDate = updateDate;  
  86.     }  
  87.   
  88.     public String getContent() {  
  89.         return content;  
  90.     }  
  91.   
  92.     public void setContent(String content) {  
  93.         this.content = content;  
  94.     }  
  95.   
  96.     public String getAccessPath() {  
  97.         return accessPath;  
  98.     }  
  99.   
  100.     public void setAccessPath(String accessPath) {  
  101.         this.accessPath = accessPath;  
  102.     }  
  103.   
  104.     public String getJumpHrefUrl() {  
  105.         return jumpHrefUrl;  
  106.     }  
  107.   
  108.     public void setJumpHrefUrl(String jumpHrefUrl) {  
  109.         this.jumpHrefUrl = jumpHrefUrl;  
  110.     }  
  111. }  

          3、SearchView.hbm.xml

[java]  view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  3.   
  4. <hibernate-mapping package="org.bgi.cms.domain" >  
  5.     <class name="SearchView" table="search_view" >  
  6.         <span style="color:#ff0000;"><composite-id name="id" class="SearchViewId">  
  7. </span>         <key-property name="id" type="integer">  
  8.                 <column name="id"/>  
  9.             </key-property>  
  10.             <key-property name="naviTitle" type="string">  
  11.                 <column name="navi_title" length="255" />  
  12.             </key-property>  
  13.             <key-property name="naviShow" type="yes_no">  
  14.                 <column name="is_navi_show"/>  
  15.             </key-property>  
  16.             <key-property name="treeShow" type="yes_no">  
  17.                 <column name="is_tree_show"/>  
  18.             </key-property>  
  19.             <key-property name="jumpHref" type="yes_no">  
  20.                 <column name="is_jump_href"/>  
  21.             </key-property>  
  22.             <key-property name="windowOpen" type="yes_no">  
  23.                 <column name="is_window_open"/>  
  24.             </key-property>  
  25.             <key-property name="auditStatus" type="integer">  
  26.                 <column name="audit_status"/>  
  27.             </key-property>  
  28.             <key-property name="createDate" type="timestamp">  
  29.                 <column name="create_date"/>  
  30.             </key-property>  
  31.             <key-property name="updateDate" type="timestamp">  
  32.                 <column name="update_date"/>  
  33.             </key-property>  
  34.             <key-property name="content" type="text">  
  35.                 <column name="content"/>  
  36.             </key-property>  
  37.             <key-property name="jumpHrefUrl" type="string">  
  38.                 <column name="jump_href_url" length="255"/>  
  39.             </key-property>  
  40.             <key-property name="accessPath" type="string">  
  41.                 <column name="access_path" length="45"/>  
  42.             </key-property>  
  43.         <span style="color:#ff0000;"></composite-id>  
  44. </span> </class>  
  45. </hibernate-mapping>  

        注:由于视图是没有主键的,所以Myeclipse生成的hbm.xml配置文件会将所有字段放在一起当做联合主键,这样做有一个问题就是,一旦视图中某个字段为null的话,该条数据在做查询时是查不出来的,所以现在暂时还不能完全确定可以使用该配置文件(如果这些联合主键中的所有字段都是不能为空的话就没有问题,完全可以放心的使用工具生成的改配置文件),因为在我的项目中,视图中的content、jumpHrefUrl以及accessPath字段都是可以为空的,所以需要修改这3个文件,如下:

       1、SearchView.java红色的为修改的

[java]  view plain copy print ?
  1. public class SearchView implements Serializable {  
  2.     /** 
  3.      *  
  4.      */  
  5.     private static final long serialVersionUID = -1372050399492830775L;  
  6.     private SearchViewId id;  
  7.   
  8. <span style="color:#ff0000;">   private String content;  
  9.     private String accessPath;  
  10.     private String jumpHrefUrl;  
  11. </span>   
  12.     public SearchViewId getId() {  
  13.         return id;  
  14.     }  
  15.   
  16.     public void setId(SearchViewId id) {  
  17.         this.id = id;  
  18.     }  
  19.       
  20.     <span style="color:#ff0000;">public String getContent() {  
  21.         return content;  
  22.     }  
  23.   
  24.     public void setContent(String content) {  
  25.         this.content = content;  
  26.     }  
  27.   
  28.     public String getAccessPath() {  
  29.         return accessPath;  
  30.     }  
  31.   
  32.     public void setAccessPath(String accessPath) {  
  33.         this.accessPath = accessPath;  
  34.     }  
  35.   
  36.     public String getJumpHrefUrl() {  
  37.         return jumpHrefUrl;  
  38.     }  
  39.   
  40.     public void setJumpHrefUrl(String jumpHrefUrl) {  
  41.         this.jumpHrefUrl = jumpHrefUrl;  
  42.     }  
  43. </span>}  
       2、 SearchViewId.java红色的为修改的
[java]  view plain copy print ?
  1. public class SearchViewId implements Serializable {  
  2.     private static final long serialVersionUID = -2960868353091674237L;  
  3.     private Integer id;  
  4.     private String naviTitle;  
  5.     private Boolean naviShow = true;  
  6.     private Boolean treeShow = true;  
  7.     private Boolean jumpHref = false;  
  8.     private Boolean windowOpen = false;  
  9.     private Integer auditStatus;  
  10.     private Date createDate;  
  11.     private Date updateDate;  
  12.   
  13. <span style="color:#ff0000;">       //  private String content;  
  14.        //   private String accessPath;  
  15.        //   private String jumpHrefUrl;  
  16. </span>   
  17.     public Integer getId() {  
  18.         return id;  
  19.     }  
  20.   
  21.     public void setId(Integer id) {  
  22.         this.id = id;  
  23.     }  
  24.   
  25.     public String getNaviTitle() {  
  26.         return naviTitle;  
  27.     }  
  28.   
  29.     public void setNaviTitle(String naviTitle) {  
  30.         this.naviTitle = naviTitle;  
  31.     }  
  32.   
  33.     public Boolean getNaviShow() {  
  34.         return naviShow;  
  35.     }  
  36.   
  37.     public void setNaviShow(Boolean naviShow) {  
  38.         this.naviShow = naviShow;  
  39.     }  
  40.   
  41.     public Boolean getTreeShow() {  
  42.         return treeShow;  
  43.     }  
  44.   
  45.     public void setTreeShow(Boolean treeShow) {  
  46.         this.treeShow = treeShow;  
  47.     }  
  48.   
  49.     public Boolean getJumpHref() {  
  50.         return jumpHref;  
  51.     }  
  52.   
  53.     public void setJumpHref(Boolean jumpHref) {  
  54.         this.jumpHref = jumpHref;  
  55.     }  
  56.   
  57.     public Boolean getWindowOpen() {  
  58.         return windowOpen;  
  59.     }  
  60.   
  61.     public void setWindowOpen(Boolean windowOpen) {  
  62.         this.windowOpen = windowOpen;  
  63.     }  
  64.   
  65.     public Integer getAuditStatus() {  
  66.         return auditStatus;  
  67.     }  
  68.   
  69.     public void setAuditStatus(Integer auditStatus) {  
  70.         this.auditStatus = auditStatus;  
  71.     }  
  72.   
  73.     public Date getCreateDate() {  
  74.         return createDate;  
  75.     }  
  76.   
  77.     public void setCreateDate(Date createDate) {  
  78.         this.createDate = createDate;  
  79.     }  
  80.   
  81.     public Date getUpdateDate() {  
  82.         return updateDate;  
  83.     }  
  84.   
  85.     public void setUpdateDate(Date updateDate) {  
  86.         this.updateDate = updateDate;  
  87.     }  
  88.   
  89. <span style="color:#ff0000;">       //</span>   <span style="color:#ff0000;">public String getContent() {  
  90.        //       return content;  
  91.        //   }  
  92.        //  
  93.        //   public void setContent(String content) {  
  94.        //       this.content = content;  
  95.        //   }  
  96.        //  
  97.        //   public String getAccessPath() {  
  98.        //       return accessPath;  
  99.        //   }  
  100.        //  
  101.        //   public void setAccessPath(String accessPath) {  
  102.        //       this.accessPath = accessPath;  
  103.        //   }  
  104.        //  
  105.        //   public String getJumpHrefUrl() {  
  106.        //       return jumpHrefUrl;  
  107.        //   }  
  108.        //  
  109.        //   public void setJumpHrefUrl(String jumpHrefUrl) {  
  110.        //       this.jumpHrefUrl = jumpHrefUrl;  
  111.        //   }  
  112. </span>}  
           3、 SearchView.hbm.xml红色的为修改的
[java]  view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  3.   
  4. <hibernate-mapping package="org.bgi.cms.domain" >  
  5.     <class name="SearchView" table="search_view" >  
  6.         <composite-id name="id" class="SearchViewId">  
  7.             <key-property name="id" type="integer">  
  8.                 <column name="id"/>  
  9.             </key-property>  
  10.             <key-property name="naviTitle" type="string">  
  11.                 <column name="navi_title" length="255" />  
  12.             </key-property>  
  13.             <key-property name="naviShow" type="yes_no">  
  14.                 <column name="is_navi_show"/>  
  15.             </key-property>  
  16.             <key-property name="treeShow" type="yes_no">  
  17.                 <column name="is_tree_show"/>  
  18.             </key-property>  
  19.             <key-property name="jumpHref" type="yes_no">  
  20.                 <column name="is_jump_href"/>  
  21.             </key-property>  
  22.             <key-property name="windowOpen" type="yes_no">  
  23.                 <column name="is_window_open"/>  
  24.             </key-property>  
  25.             <key-property name="auditStatus" type="integer">  
  26.                 <column name="audit_status"/>  
  27.             </key-property>  
  28.             <key-property name="createDate" type="timestamp">  
  29.                 <column name="create_date"/>  
  30.             </key-property>  
  31.             <key-property name="updateDate" type="timestamp">  
  32.                 <column name="update_date"/>  
  33.             </key-property>  
  34.         </composite-id>  
  35. <span style="color:#ff0000;">       <property name="content" column="content" type="text" />  
  36.         <property name="jumpHrefUrl" column="jump_href_url" type="string" length="255" />  
  37.         <property name="accessPath" column="access_path" type="string" length="45" />  
  38. </span> </class>  
  39. </hibernate-mapping>  

猜你喜欢

转载自blog.csdn.net/wwwwenhuan/article/details/12203217