Hibernate操纵视图
①有如下视图:
图1
②hibernate逆向生成之后的代码如下:
CountView.java
- package com.yaxing.entity;
- /**
- * CountView entity. @author MyEclipse Persistence Tools
- */
- public class CountView implements java.io.Serializable {
- // Fields
- private CountViewId id;
- // Constructors
- /** default constructor */
- public CountView() {
- }
- /** full constructor */
- public CountView(CountViewId id) {
- this.id = id;
- }
- // Property accessors
- public CountViewId getId() {
- return this.id;
- }
- public void setId(CountViewId id) {
- this.id = id;
- }
- }
CountViewId.java
- package com.yaxing.entity;
- /**
- * CountViewId entity. @author MyEclipse Persistence Tools
- */
- public class CountViewId implements java.io.Serializable {
- // Fields
- private Integer countAthlete;
- private Integer countTeam;
- private long eveId;
- private String race;
- private long userId;
- private long id;
- // Constructors
- /** default constructor */
- public CountViewId() {
- }
- /** minimal constructor */
- public CountViewId(long id) {
- this.id = id;
- }
- /** full constructor */
- public CountViewId(Integer countAthlete, Integer countTeam, long eveId,
- String race, long userId, long id) {
- this.countAthlete = countAthlete;
- this.countTeam = countTeam;
- this.eveId = eveId;
- this.race = race;
- this.userId = userId;
- this.id = id;
- }
- // Property accessors
- public Integer getCountAthlete() {
- return this.countAthlete;
- }
- public void setCountAthlete(Integer countAthlete) {
- this.countAthlete = countAthlete;
- }
- public Integer getCountTeam() {
- return this.countTeam;
- }
- public void setCountTeam(Integer countTeam) {
- this.countTeam = countTeam;
- }
- public long getEveId() {
- return this.eveId;
- }
- public void setEveId(long eveId) {
- this.eveId = eveId;
- }
- public String getRace() {
- return this.race;
- }
- public void setRace(String race) {
- this.race = race;
- }
- public long getUserId() {
- return this.userId;
- }
- public void setUserId(long userId) {
- this.userId = userId;
- }
- public long getId() {
- return this.id;
- }
- public void setId(long id) {
- this.id = id;
- }
- public boolean equals(Object other) {
- if ((this == other))
- return true;
- if ((other == null))
- return false;
- if (!(other instanceof CountViewId))
- return false;
- CountViewId castOther = (CountViewId) other;
- return ((this.getCountAthlete() == castOther.getCountAthlete()) || (this
- .getCountAthlete() != null
- && castOther.getCountAthlete() != null && this
- .getCountAthlete().equals(castOther.getCountAthlete())))
- && ((this.getCountTeam() == castOther.getCountTeam()) || (this
- .getCountTeam() != null
- && castOther.getCountTeam() != null && this
- .getCountTeam().equals(castOther.getCountTeam())))
- && (this.getEveId() == castOther.getEveId())
- && ((this.getRace() == castOther.getRace()) || (this.getRace() != null
- && castOther.getRace() != null && this.getRace()
- .equals(castOther.getRace())))
- && (this.getUserId() == castOther.getUserId())
- && (this.getId() == castOther.getId());
- }
- public int hashCode() {
- int result = 17;
- result = 37
- * result
- + (getCountAthlete() == null ? 0 : this.getCountAthlete()
- .hashCode());
- result = 37 * result
- + (getCountTeam() == null ? 0 : this.getCountTeam().hashCode());
- result = 37 * result + (int) this.getEveId();
- result = 37 * result
- + (getRace() == null ? 0 : this.getRace().hashCode());
- result = 37 * result + (int) this.getUserId();
- result = 37 * result + (int) this.getId();
- return result;
- }
- }
我们知道,视图是没有主键的,视图就是一张虚拟表。hibernate对没有主键的表逆向生成的时候,是会生成
CountView.java
CountViewId.java
两个类的,XxxId.java这个类里面的属性才是我们需要的数据.
③映射文件如下:
CountView.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">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.yaxing.entity.CountView" table="countView" schema="dbo" catalog="sportSys">
- <composite-id name="id" class="com.yaxing.entity.CountViewId">
- <key-property name="countAthlete" type="integer">
- <column name="countAthlete" />
- </key-property>
- <key-property name="countTeam" type="integer">
- <column name="countTeam" />
- </key-property>
- <key-property name="eveId" type="long">
- <column name="eve_id" precision="18" scale="0" />
- </key-property>
- <key-property name="race" type="string">
- <column name="race" length="50" />
- </key-property>
- <key-property name="userId" type="long">
- <column name="userId" precision="18" scale="0" />
- </key-property>
- <key-property name="id" type="long">
- <column name="id" precision="18" scale="0" />
- </key-property>
- </composite-id>
- </class>
- </hibernate-mapping>
④CountViewDaoImpl .java如下:
- package com.yaxing.daoImpl;
- import java.util.List;
- import org.hibernate.Query;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import com.yaxing.dao.CountViewDao;
- import com.yaxing.entity.CountView;
- import com.yaxing.util.PageModel;
- public class CountViewDaoImpl extends HibernateDaoSupport implements CountViewDao {
- @Override
- public List<CountView> listCountView() {
- // TODO Auto-generated method stub
- return this.getHibernateTemplate().find("select id.countAthlete,id.countTeam,id.race,id.eveId from CountView");
- }
- }
可以看到第17行:
- return this.getHibernateTemplate().find("select id.countAthlete,id.countTeam,id.race,id.eveId from CountView");
字段是id.countAthlete这种,因为CountView.java的11行
- private CountViewId id;
我们这只有一个返回所有的List集合
⑤Action中如下:
- public String listCountView() throws Exception {
- try {
- List allValue = this.countViewService.listCountView();
- listCountView = new ArrayList();
- Iterator it = allValue.iterator();
- while (it.hasNext()) {
- Object[] all = (Object[]) it.next();
- CountViewId countViewId = new CountViewId();
- countViewId.setCountAthlete((Integer) all[0]);
- countViewId.setCountTeam((Integer) all[1]);
- countViewId.setRace((String) all[2]);
- countViewId.setEveId( (Long)all[3]);
- listCountView.add(countViewId);
- }
- } catch (Exception e) {
- e.printStackTrace();
- return INPUT;
- }
- return SUCCESS;
- }
第3行,如果直接写成
- listCountView = this.countViewService.listCountView();
这个页面查询的是没有结果的!
是因为:Hibernate 映射视图会生成联合主键.在查询时,如果联合主键里有一项值为null,则整个结果返回null。然而,我们的查询中不可避免的存在值为null的情况,这种情况下该怎么办呢?
解决办法:需要做4-11行代码的转换才行!
给 listCountView 迭代的添加内容!
本文转自 w156445045 51CTO博客,原文链接:http://blog.51cto.com/enetq/845699,如需转载请自行联系原作者