由於項目(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
1、SearchView.java
- public class SearchView implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = -1372050399492830775L;
- private SearchViewId id;
- public SearchViewId getId() {
- return id;
- }
- public void setId(SearchViewId id) {
- this.id = id;
- }
- }
public class SearchView implements Serializable { /** * */ private static final long serialVersionUID = -1372050399492830775L; private SearchViewId id; public SearchViewId getId() { return id; } public void setId(SearchViewId id) { this.id = id; } }
2、SearchViewId.java
- public class SearchViewId implements Serializable {
- private static final long serialVersionUID = -2960868353091674237L;
- private Integer id;
- private String naviTitle;
- private Boolean naviShow = true;
- private Boolean treeShow = true;
- private Boolean jumpHref = false;
- private Boolean windowOpen = false;
- private Integer auditStatus;
- private Date createDate;
- private Date updateDate;
- private String content;
- private String accessPath;
- private String jumpHrefUrl;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getNaviTitle() {
- return naviTitle;
- }
- public void setNaviTitle(String naviTitle) {
- this.naviTitle = naviTitle;
- }
- public Boolean getNaviShow() {
- return naviShow;
- }
- public void setNaviShow(Boolean naviShow) {
- this.naviShow = naviShow;
- }
- public Boolean getTreeShow() {
- return treeShow;
- }
- public void setTreeShow(Boolean treeShow) {
- this.treeShow = treeShow;
- }
- public Boolean getJumpHref() {
- return jumpHref;
- }
- public void setJumpHref(Boolean jumpHref) {
- this.jumpHref = jumpHref;
- }
- public Boolean getWindowOpen() {
- return windowOpen;
- }
- public void setWindowOpen(Boolean windowOpen) {
- this.windowOpen = windowOpen;
- }
- public Integer getAuditStatus() {
- return auditStatus;
- }
- public void setAuditStatus(Integer auditStatus) {
- this.auditStatus = auditStatus;
- }
- public Date getCreateDate() {
- return createDate;
- }
- public void setCreateDate(Date createDate) {
- this.createDate = createDate;
- }
- public Date getUpdateDate() {
- return updateDate;
- }
- public void setUpdateDate(Date updateDate) {
- this.updateDate = updateDate;
- }
- public String getContent() {
- return content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- public String getAccessPath() {
- return accessPath;
- }
- public void setAccessPath(String accessPath) {
- this.accessPath = accessPath;
- }
- public String getJumpHrefUrl() {
- return jumpHrefUrl;
- }
- public void setJumpHrefUrl(String jumpHrefUrl) {
- this.jumpHrefUrl = jumpHrefUrl;
- }
- }
public class SearchViewId implements Serializable { private static final long serialVersionUID = -2960868353091674237L; private Integer id; private String naviTitle; private Boolean naviShow = true; private Boolean treeShow = true; private Boolean jumpHref = false; private Boolean windowOpen = false; private Integer auditStatus; private Date createDate; private Date updateDate; private String content; private String accessPath; private String jumpHrefUrl; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getNaviTitle() { return naviTitle; } public void setNaviTitle(String naviTitle) { this.naviTitle = naviTitle; } public Boolean getNaviShow() { return naviShow; } public void setNaviShow(Boolean naviShow) { this.naviShow = naviShow; } public Boolean getTreeShow() { return treeShow; } public void setTreeShow(Boolean treeShow) { this.treeShow = treeShow; } public Boolean getJumpHref() { return jumpHref; } public void setJumpHref(Boolean jumpHref) { this.jumpHref = jumpHref; } public Boolean getWindowOpen() { return windowOpen; } public void setWindowOpen(Boolean windowOpen) { this.windowOpen = windowOpen; } public Integer getAuditStatus() { return auditStatus; } public void setAuditStatus(Integer auditStatus) { this.auditStatus = auditStatus; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public Date getUpdateDate() { return updateDate; } public void setUpdateDate(Date updateDate) { this.updateDate = updateDate; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getAccessPath() { return accessPath; } public void setAccessPath(String accessPath) { this.accessPath = accessPath; } public String getJumpHrefUrl() { return jumpHrefUrl; } public void setJumpHrefUrl(String jumpHrefUrl) { this.jumpHrefUrl = jumpHrefUrl; } }
3、SearchView.hbm.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="org.bgi.cms.domain" >
- <class name="SearchView" table="search_view" >
- <span style="color: rgb(255, 0, 0);"><composite-id name="id" class="SearchViewId">
- </span> <key-property name="id" type="integer">
- <column name="id"/>
- </key-property>
- <key-property name="naviTitle" type="string">
- <column name="navi_title" length="255" />
- </key-property>
- <key-property name="naviShow" type="yes_no">
- <column name="is_navi_show"/>
- </key-property>
- <key-property name="treeShow" type="yes_no">
- <column name="is_tree_show"/>
- </key-property>
- <key-property name="jumpHref" type="yes_no">
- <column name="is_jump_href"/>
- </key-property>
- <key-property name="windowOpen" type="yes_no">
- <column name="is_window_open"/>
- </key-property>
- <key-property name="auditStatus" type="integer">
- <column name="audit_status"/>
- </key-property>
- <key-property name="createDate" type="timestamp">
- <column name="create_date"/>
- </key-property>
- <key-property name="updateDate" type="timestamp">
- <column name="update_date"/>
- </key-property>
- <key-property name="content" type="text">
- <column name="content"/>
- </key-property>
- <key-property name="jumpHrefUrl" type="string">
- <column name="jump_href_url" length="255"/>
- </key-property>
- <key-property name="accessPath" type="string">
- <column name="access_path" length="45"/>
- </key-property>
- <span style="color: rgb(255, 0, 0);"></composite-id>
- </span> </class>
- </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.bgi.cms.domain" >
<class name="SearchView" table="search_view" >
<composite-id name="id" class="SearchViewId">
<key-property name="id" type="integer">
<column name="id"/>
</key-property>
<key-property name="naviTitle" type="string">
<column name="navi_title" length="255" />
</key-property>
<key-property name="naviShow" type="yes_no">
<column name="is_navi_show"/>
</key-property>
<key-property name="treeShow" type="yes_no">
<column name="is_tree_show"/>
</key-property>
<key-property name="jumpHref" type="yes_no">
<column name="is_jump_href"/>
</key-property>
<key-property name="windowOpen" type="yes_no">
<column name="is_window_open"/>
</key-property>
<key-property name="auditStatus" type="integer">
<column name="audit_status"/>
</key-property>
<key-property name="createDate" type="timestamp">
<column name="create_date"/>
</key-property>
<key-property name="updateDate" type="timestamp">
<column name="update_date"/>
</key-property>
<key-property name="content" type="text">
<column name="content"/>
</key-property>
<key-property name="jumpHrefUrl" type="string">
<column name="jump_href_url" length="255"/>
</key-property>
<key-property name="accessPath" type="string">
<column name="access_path" length="45"/>
</key-property>
</composite-id> </class> </hibernate-mapping>
注:由於視圖是沒有主鍵的,所以Myeclipse生成的hbm.xml配置文件會將所有字段放在一起當做聯合主鍵,這樣做有一個問題就是,一旦視圖中某個字段為null的話,該條數據在做查詢時是查不出來的,所以現在暫時還不能完全確定可以使用該配置文件(如果這些聯合主鍵中的所有字段都是不能為空的話就沒有問題,完全可以放心的使用工具生成的改配置文件),因為在我的項目中,視圖中的content、jumpHrefUrl以及accessPath字段都是可以為空的,所以需要修改這3個文件,如下:
1、SearchView.java(紅色的為修改的)
- public class SearchView implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = -1372050399492830775L;
- private SearchViewId id;
- <span style="color: rgb(255, 0, 0);"> private String content;
- private String accessPath;
- private String jumpHrefUrl;
- </span>
- public SearchViewId getId() {
- return id;
- }
- public void setId(SearchViewId id) {
- this.id = id;
- }
- <span style="color: rgb(255, 0, 0);">public String getContent() {
- return content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- public String getAccessPath() {
- return accessPath;
- }
- public void setAccessPath(String accessPath) {
- this.accessPath = accessPath;
- }
- public String getJumpHrefUrl() {
- return jumpHrefUrl;
- }
- public void setJumpHrefUrl(String jumpHrefUrl) {
- this.jumpHrefUrl = jumpHrefUrl;
- }
- </span>}
public class SearchView implements Serializable {
/**
*
*/
private static final long serialVersionUID = -1372050399492830775L;
private SearchViewId id;
private String content;
private String accessPath;
private String jumpHrefUrl;
public SearchViewId getId() {
return id;
}
public void setId(SearchViewId id) {
this.id = id;
}
public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getAccessPath() { return accessPath; } public void setAccessPath(String accessPath) { this.accessPath = accessPath; } public String getJumpHrefUrl() { return jumpHrefUrl; } public void setJumpHrefUrl(String jumpHrefUrl) { this.jumpHrefUrl = jumpHrefUrl; } }
2、SearchViewId.java(紅色的為修改的)
- public class SearchViewId implements Serializable {
- private static final long serialVersionUID = -2960868353091674237L;
- private Integer id;
- private String naviTitle;
- private Boolean naviShow = true;
- private Boolean treeShow = true;
- private Boolean jumpHref = false;
- private Boolean windowOpen = false;
- private Integer auditStatus;
- private Date createDate;
- private Date updateDate;
- <span style="color: rgb(255, 0, 0);"> // private String content;
- // private String accessPath;
- // private String jumpHrefUrl;
- </span>
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getNaviTitle() {
- return naviTitle;
- }
- public void setNaviTitle(String naviTitle) {
- this.naviTitle = naviTitle;
- }
- public Boolean getNaviShow() {
- return naviShow;
- }
- public void setNaviShow(Boolean naviShow) {
- this.naviShow = naviShow;
- }
- public Boolean getTreeShow() {
- return treeShow;
- }
- public void setTreeShow(Boolean treeShow) {
- this.treeShow = treeShow;
- }
- public Boolean getJumpHref() {
- return jumpHref;
- }
- public void setJumpHref(Boolean jumpHref) {
- this.jumpHref = jumpHref;
- }
- public Boolean getWindowOpen() {
- return windowOpen;
- }
- public void setWindowOpen(Boolean windowOpen) {
- this.windowOpen = windowOpen;
- }
- public Integer getAuditStatus() {
- return auditStatus;
- }
- public void setAuditStatus(Integer auditStatus) {
- this.auditStatus = auditStatus;
- }
- public Date getCreateDate() {
- return createDate;
- }
- public void setCreateDate(Date createDate) {
- this.createDate = createDate;
- }
- public Date getUpdateDate() {
- return updateDate;
- }
- public void setUpdateDate(Date updateDate) {
- this.updateDate = updateDate;
- }
- <span style="color: rgb(255, 0, 0);"> //</span> <span style="color: rgb(255, 0, 0);">public String getContent() {
- // return content;
- // }
- //
- // public void setContent(String content) {
- // this.content = content;
- // }
- //
- // public String getAccessPath() {
- // return accessPath;
- // }
- //
- // public void setAccessPath(String accessPath) {
- // this.accessPath = accessPath;
- // }
- //
- // public String getJumpHrefUrl() {
- // return jumpHrefUrl;
- // }
- //
- // public void setJumpHrefUrl(String jumpHrefUrl) {
- // this.jumpHrefUrl = jumpHrefUrl;
- // }
- </span>}
public class SearchViewId implements Serializable {
private static final long serialVersionUID = -2960868353091674237L;
private Integer id;
private String naviTitle;
private Boolean naviShow = true;
private Boolean treeShow = true;
private Boolean jumpHref = false;
private Boolean windowOpen = false;
private Integer auditStatus;
private Date createDate;
private Date updateDate;
// private String content;
// private String accessPath;
// private String jumpHrefUrl;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNaviTitle() {
return naviTitle;
}
public void setNaviTitle(String naviTitle) {
this.naviTitle = naviTitle;
}
public Boolean getNaviShow() {
return naviShow;
}
public void setNaviShow(Boolean naviShow) {
this.naviShow = naviShow;
}
public Boolean getTreeShow() {
return treeShow;
}
public void setTreeShow(Boolean treeShow) {
this.treeShow = treeShow;
}
public Boolean getJumpHref() {
return jumpHref;
}
public void setJumpHref(Boolean jumpHref) {
this.jumpHref = jumpHref;
}
public Boolean getWindowOpen() {
return windowOpen;
}
public void setWindowOpen(Boolean windowOpen) {
this.windowOpen = windowOpen;
}
public Integer getAuditStatus() {
return auditStatus;
}
public void setAuditStatus(Integer auditStatus) {
this.auditStatus = auditStatus;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
// public String getContent() { // return content; // } // // public void setContent(String content) { // this.content = content; // } // // public String getAccessPath() { // return accessPath; // } // // public void setAccessPath(String accessPath) { // this.accessPath = accessPath; // } // // public String getJumpHrefUrl() { // return jumpHrefUrl; // } // // public void setJumpHrefUrl(String jumpHrefUrl) { // this.jumpHrefUrl = jumpHrefUrl; // } }
3、SearchView.hbm.xml(紅色的為修改的)
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="org.bgi.cms.domain" >
- <class name="SearchView" table="search_view" >
- <composite-id name="id" class="SearchViewId">
- <key-property name="id" type="integer">
- <column name="id"/>
- </key-property>
- <key-property name="naviTitle" type="string">
- <column name="navi_title" length="255" />
- </key-property>
- <key-property name="naviShow" type="yes_no">
- <column name="is_navi_show"/>
- </key-property>
- <key-property name="treeShow" type="yes_no">
- <column name="is_tree_show"/>
- </key-property>
- <key-property name="jumpHref" type="yes_no">
- <column name="is_jump_href"/>
- </key-property>
- <key-property name="windowOpen" type="yes_no">
- <column name="is_window_open"/>
- </key-property>
- <key-property name="auditStatus" type="integer">
- <column name="audit_status"/>
- </key-property>
- <key-property name="createDate" type="timestamp">
- <column name="create_date"/>
- </key-property>
- <key-property name="updateDate" type="timestamp">
- <column name="update_date"/>
- </key-property>
- </composite-id>
- <span style="color: rgb(255, 0, 0);"> <property name="content" column="content" type="text" />
- <property name="jumpHrefUrl" column="jump_href_url" type="string" length="255" />
- <property name="accessPath" column="access_path" type="string" length="45" />
- </span> </class>
- </hibernate-mapping>