①有如下视图:

 

图1

②hibernate逆向生成之后的代码如下:

CountView.java

  1. package com.yaxing.entity;  
  2.  
  3. /**  
  4.  * CountView entity. @author MyEclipse Persistence Tools  
  5.  */ 
  6.  
  7. public class CountView implements java.io.Serializable {  
  8.  
  9.     // Fields  
  10.  
  11.     private CountViewId id;  
  12.  
  13.     // Constructors  
  14.  
  15.     /** default constructor */ 
  16.     public CountView() {  
  17.     }  
  18.  
  19.     /** full constructor */ 
  20.     public CountView(CountViewId id) {  
  21.         this.id = id;  
  22.     }  
  23.  
  24.     // Property accessors  
  25.  
  26.     public CountViewId getId() {  
  27.         return this.id;  
  28.     }  
  29.  
  30.     public void setId(CountViewId id) {  
  31.         this.id = id;  
  32.     }  
  33.  

CountViewId.java

  1. package com.yaxing.entity;  
  2.  
  3. /**  
  4.  * CountViewId entity. @author MyEclipse Persistence Tools  
  5.  */ 
  6.  
  7. public class CountViewId implements java.io.Serializable {  
  8.  
  9.     // Fields  
  10.  
  11.     private Integer countAthlete;  
  12.     private Integer countTeam;  
  13.     private long eveId;  
  14.     private String race;  
  15.     private long userId;  
  16.     private long id;  
  17.  
  18.     // Constructors  
  19.  
  20.     /** default constructor */ 
  21.     public CountViewId() {  
  22.     }  
  23.  
  24.     /** minimal constructor */ 
  25.     public CountViewId(long id) {  
  26.         this.id = id;  
  27.     }  
  28.  
  29.     /** full constructor */ 
  30.     public CountViewId(Integer countAthlete, Integer countTeam, long eveId,  
  31.             String race, long userId, long id) {  
  32.         this.countAthlete = countAthlete;  
  33.         this.countTeam = countTeam;  
  34.         this.eveId = eveId;  
  35.         this.race = race;  
  36.         this.userId = userId;  
  37.         this.id = id;  
  38.     }  
  39.  
  40.     // Property accessors  
  41.  
  42.     public Integer getCountAthlete() {  
  43.         return this.countAthlete;  
  44.     }  
  45.  
  46.     public void setCountAthlete(Integer countAthlete) {  
  47.         this.countAthlete = countAthlete;  
  48.     }  
  49.  
  50.     public Integer getCountTeam() {  
  51.         return this.countTeam;  
  52.     }  
  53.  
  54.     public void setCountTeam(Integer countTeam) {  
  55.         this.countTeam = countTeam;  
  56.     }  
  57.  
  58.     public long getEveId() {  
  59.         return this.eveId;  
  60.     }  
  61.  
  62.     public void setEveId(long eveId) {  
  63.         this.eveId = eveId;  
  64.     }  
  65.  
  66.     public String getRace() {  
  67.         return this.race;  
  68.     }  
  69.  
  70.     public void setRace(String race) {  
  71.         this.race = race;  
  72.     }  
  73.  
  74.     public long getUserId() {  
  75.         return this.userId;  
  76.     }  
  77.  
  78.     public void setUserId(long userId) {  
  79.         this.userId = userId;  
  80.     }  
  81.  
  82.     public long getId() {  
  83.         return this.id;  
  84.     }  
  85.  
  86.     public void setId(long id) {  
  87.         this.id = id;  
  88.     }  
  89.  
  90.     public boolean equals(Object other) {  
  91.         if ((this == other))  
  92.             return true;  
  93.         if ((other == null))  
  94.             return false;  
  95.         if (!(other instanceof CountViewId))  
  96.             return false;  
  97.         CountViewId castOther = (CountViewId) other;  
  98.  
  99.         return ((this.getCountAthlete() == castOther.getCountAthlete()) || (this 
  100.                 .getCountAthlete() != null 
  101.                 && castOther.getCountAthlete() != null && this 
  102.                 .getCountAthlete().equals(castOther.getCountAthlete())))  
  103.                 && ((this.getCountTeam() == castOther.getCountTeam()) || (this 
  104.                         .getCountTeam() != null 
  105.                         && castOther.getCountTeam() != null && this 
  106.                         .getCountTeam().equals(castOther.getCountTeam())))  
  107.                 && (this.getEveId() == castOther.getEveId())  
  108.                 && ((this.getRace() == castOther.getRace()) || (this.getRace() != null 
  109.                         && castOther.getRace() != null && this.getRace()  
  110.                         .equals(castOther.getRace())))  
  111.                 && (this.getUserId() == castOther.getUserId())  
  112.                 && (this.getId() == castOther.getId());  
  113.     }  
  114.  
  115.     public int hashCode() {  
  116.         int result = 17;  
  117.  
  118.         result = 37 
  119.                 * result  
  120.                 + (getCountAthlete() == null ? 0 : this.getCountAthlete()  
  121.                         .hashCode());  
  122.         result = 37 * result  
  123.                 + (getCountTeam() == null ? 0 : this.getCountTeam().hashCode());  
  124.         result = 37 * result + (intthis.getEveId();  
  125.         result = 37 * result  
  126.                 + (getRace() == null ? 0 : this.getRace().hashCode());  
  127.         result = 37 * result + (intthis.getUserId();  
  128.         result = 37 * result + (intthis.getId();  
  129.         return result;  
  130.     }  
  131.  

我们知道,视图是没有主键的,视图就是一张虚拟表。hibernate对没有主键的表逆向生成的时候,是会生成

CountView.java

CountViewId.java

两个类的,XxxId.java这个类里面的属性才是我们需要的数据.

③映射文件如下

CountView.hbm.xml

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
  4. <!--   
  5.     Mapping file autogenerated by MyEclipse Persistence Tools  
  6. --> 
  7. <hibernate-mapping> 
  8.     <class name="com.yaxing.entity.CountView" table="countView" schema="dbo" catalog="sportSys"> 
  9.         <composite-id name="id" class="com.yaxing.entity.CountViewId"> 
  10.             <key-property name="countAthlete" type="integer"> 
  11.                 <column name="countAthlete" /> 
  12.             </key-property> 
  13.             <key-property name="countTeam" type="integer"> 
  14.                 <column name="countTeam" /> 
  15.             </key-property> 
  16.             <key-property name="eveId" type="long"> 
  17.                 <column name="eve_id" precision="18" scale="0" /> 
  18.             </key-property> 
  19.             <key-property name="race" type="string"> 
  20.                 <column name="race" length="50" /> 
  21.             </key-property> 
  22.             <key-property name="userId" type="long"> 
  23.                 <column name="userId" precision="18" scale="0" /> 
  24.             </key-property> 
  25.             <key-property name="id" type="long"> 
  26.                 <column name="id" precision="18" scale="0" /> 
  27.             </key-property> 
  28.         </composite-id> 
  29.     </class> 
  30. </hibernate-mapping> 

④CountViewDaoImpl .java如下:

 

  1. package com.yaxing.daoImpl;  
  2.  
  3. import java.util.List;  
  4.  
  5. import org.hibernate.Query;  
  6. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  7.  
  8. import com.yaxing.dao.CountViewDao;  
  9. import com.yaxing.entity.CountView;  
  10. import com.yaxing.util.PageModel;  
  11.  
  12. public class CountViewDaoImpl extends HibernateDaoSupport implements CountViewDao {  
  13.  
  14.         @Override 
  15.     public List<CountView> listCountView() {  
  16.         // TODO Auto-generated method stub  
  17.         return this.getHibernateTemplate().find("select id.countAthlete,id.countTeam,id.race,id.eveId from CountView");  
  18.     }  
  19.  
  20.     }  

可以看到第17行:

 

  1. return this.getHibernateTemplate().find("select id.countAthlete,id.countTeam,id.race,id.eveId from CountView"); 

字段是id.countAthlete这种,因为CountView.java的11行

 

  1. private CountViewId id;   

我们这只有一个返回所有的List集合

⑤Action中如下:

 

  1. public String listCountView() throws Exception {  
  2.         try {  
  3.             List allValue = this.countViewService.listCountView();  
  4.             listCountView = new ArrayList();  
  5.             Iterator it = allValue.iterator();  
  6.             while (it.hasNext()) {  
  7.                 Object[] all = (Object[]) it.next();  
  8.                 CountViewId countViewId = new CountViewId();  
  9.                 countViewId.setCountAthlete((Integer) all[0]);  
  10.                 countViewId.setCountTeam((Integer) all[1]);  
  11.                 countViewId.setRace((String) all[2]);  
  12.                 countViewId.setEveId( (Long)all[3]);  
  13.                   
  14.                 listCountView.add(countViewId);   
  15.             }  
  16.  
  17.         } catch (Exception e) {  
  18.             e.printStackTrace();  
  19.             return INPUT;  
  20.         }  
  21.         return SUCCESS;  
  22.  
  23.     } 

第3行,如果直接写成

 

  1. listCountView = this.countViewService.listCountView(); 

这个页面查询的是没有结果的!

是因为:Hibernate 映射视图会生成联合主键.在查询时,如果联合主键里有一项值为null,则整个结果返回null。然而,我们的查询中不可避免的存在值为null的情况,这种情况下该怎么办呢?

解决办法:需要做4-11行代码的转换才行!

给 listCountView 迭代的添加内容!

本文出自 “幽灵柯南的技术blog” 博客,请务必保留此出处http://enetq.blog.51cto.com/479739/845699