由于项目(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>