Hibernate操纵视图详解

简介:

Hibernate操纵视图

①有如下视图:

 

图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 迭代的添加内容!






 本文转自 w156445045 51CTO博客,原文链接:http://blog.51cto.com/enetq/845699,如需转载请自行联系原作者

相关文章
|
30天前
|
XML JSON Java
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
本文介绍了如何使用IntelliJ IDEA和Maven搭建一个整合了Struts2、Spring4、Hibernate4的J2EE项目,并配置了项目目录结构、web.xml、welcome.jsp以及多个JSP页面,用于刷新和学习传统的SSH框架。
30 0
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
|
缓存 Java 数据库连接
hibernate5(8)操纵对象入门[3]操控对象封装方法
<div class="markdown_views"> <p>为什么要说是“封装方法”呢?因为它帮我们封装好了底层的增删改查操作,直接调用相应方法即可灵活地操作我们数据库数据。它们由Session接口提供,下面我们通过实例一一分析这些方法。</p> <h2 id="1save方法">1.save方法</h2> <p><font color="blue">Session
2019 0
|
缓存 Java 数据库连接
hibernate5(6)操纵对象入门[1]Session缓存
<div class="markdown_views"> <h1 id="java对象在jvm中的存活条件">java对象在JVM中的存活条件</h1> <p>在java中,我们使用<code>User user = new User();</code>来创建一个java对象时,JVM会为其分配一块内存空间,此时,这个对象被变量“user”引用,那么它就会一直存在于内存中,而
1422 0
|
缓存 Java 数据库连接
hibernate5(7)操纵对象入门[2]四大对象状态分析
<div class="markdown_views"> <h1 id="状态类型">状态类型</h1> <p>在hibernate中,java对象的声明周期对应有4种状态:</p> <table> <thead><tr> <th>状态</th> <th>说明</th> </tr></thead> <tbody> <tr> <td>瞬时(Transient)</td>
1424 0
|
4月前
|
SQL 缓存 Java
框架分析(9)-Hibernate
框架分析(9)-Hibernate
|
24天前
|
SQL Java 数据库连接
Hibernate 是一款开源 ORM(对象关系映射)框架,封装了 JDBC,允许以面向对象的方式操作数据库,简化了数据访问层的开发。
Hibernate 是一款开源 ORM(对象关系映射)框架,封装了 JDBC,允许以面向对象的方式操作数据库,简化了数据访问层的开发。通过映射机制,它可以自动处理对象与数据库表之间的转换,支持主流数据库,提高了代码的可移植性和可维护性。其核心接口包括 SessionFactory、Session 和 Transaction 等,通过它们可以执行数据库的 CRUD 操作。配置方面,需在项目中引入 Hibernate 及数据库驱动依赖,并创建 `hibernate.cfg.xml` 配置文件来设置数据库连接和 Hibernate 行为参数。
28 1
|
14天前
|
数据库 Java 数据库连接
Struts 2 与 Hibernate 的完美邂逅:如何无缝集成两大框架,轻松玩转高效 CRUD 操作?
【8月更文挑战第31天】本文通过具体示例介绍了如何在 Struts 2 中整合 Hibernate,实现基本的 CRUD 操作。首先创建 Maven 项目并添加相关依赖,接着配置 Hibernate 并定义实体类及其映射文件。然后创建 DAO 接口及实现类处理数据库操作,再通过 Struts 2 的 Action 类处理用户请求。最后配置 `struts.xml` 文件并创建 JSP 页面展示用户列表及编辑表单。此示例展示了如何配置和使用这两个框架,使代码更加模块化和可维护。
24 0
|
2月前
|
SQL Java 数据库连接
Java面试题:简述ORM框架(如Hibernate、MyBatis)的工作原理及其优缺点。
Java面试题:简述ORM框架(如Hibernate、MyBatis)的工作原理及其优缺点。
45 0
|
3月前
|
Java 数据库连接 数据库
探索JPA生态:Hibernate与其他ORM框架的对比分析
【6月更文挑战第25天】**JPA标准下的Hibernate是流行的ORM实现,提供丰富功能如二级缓存和延迟加载,但其学习曲线较陡,性能优化复杂。相比如MyBatis,Hibernate的JPQL更面向对象,MyBatis则接近SQL。选择ORM需考虑项目需求和个人偏好。**
52 0
|
3月前
|
Java 数据库连接
杨老师课堂之JavaEE三大框架Hibernate入门第一课
杨老师课堂之JavaEE三大框架Hibernate入门第一课
23 0