今天遇到个问题,2张表关联查询,要显示出另一张表ID的中文名称。本来直接HIBERNATE 多刷新一次就可以带出来,需要要求直接写出来,没办法 改进代码
表结构 WCSC_CSC
create table WCSC_CSC ( COMPCODE VARCHAR2(32) not null, CSC_ID NUMBER not null, NAME VARCHAR2(100) not null, LOGO_URL VARCHAR2(200), CSS_STYLE VARCHAR2(64), WELCOME VARCHAR2(200), OTHER1 VARCHAR2(100), OTHER2 VARCHAR2(100) )
表WCSC_QUEUE
create table WCSC_QUEUE ( CSC_ID NUMBER not null, QUEUE_ID NUMBER not null, QUEUE_NAME VARCHAR2(100), QUEUE_LENGTH NUMBER default 0 not null, ACD_TYPE CHAR(1) default 0 not null )
2张表的关系是,WCSC_CSC 一 对WCSC_QUEUE表的多 关联字段是 CSC_ID
需求是查询出 WCSC_QUEUE 所有字段已经和WCSC_CSC表关联的 NAME
SQL 可以这样写:
select a.*, b.name from WCSC_CSC b, WCSC_QUEUE a where a.csc_id = b.csc_id and b.compcode = '0071'
2张表我是单项WCSC_QUEUE 多对WCSC_CSC 一关联
WCSC_CSC 表
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="com.ideal.csc.manager.entity"> <class name="Csc" table="WCSC_CSC" > <meta attribute="sync-DAO">false</meta> <cache usage="read-write"/> <id name="id" column="CSC_ID" type="java.lang.Long"> <generator class="sequence"> <param name="sequence">SEQ_WCSC_CSC_ID</param> </generator> </id> <property name="name" column="NAME" type="string" not-null="false" length="100" /> <property name="logurl" column="LOGO_URL" type="string" not-null="false" length="200" /> <property name="cssstyle" column="CSS_STYLE" type="string" not-null="false" length="64" /> <property name="welcome" column="WELCOME" type="string" not-null="false" length="200" /> <property name="other1" column="OTHER1" type="string" not-null="false" length="100" /> <property name="other2" column="OTHER2" type="string" not-null="false" length="100" /> <property name="compcode" column="COMPCODE" type="string" not-null="true" length="32" /> </class> </hibernate-mapping>
WCSC_QUEUE 表
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="com.ideal.csc.manager.entity"> <class name="Queue" table="WCSC_QUEUE" > <meta attribute="sync-DAO">false</meta> <cache usage="read-write"/> <id name="id" column="QUEUE_ID" type="java.lang.Long"> <generator class="sequence"> <param name="sequence">SEQ_WCSC_QUEUE_ID</param> </generator> </id> <property name="queueName" column="QUEUE_NAME" type="string" not-null="false" length="100" /> <property name="queueLength" column="QUEUE_LENGTH" type="java.lang.Long" not-null="true" /> <property name="acdType" column="ACD_TYPE" type="string" not-null="true" length="2" /> <many-to-one name="csc" column="CSC_ID" class="com.ideal.csc.manager.entity.Csc" not-null="true" > </many-to-one> </class> </hibernate-mapping>
这里用了单向多对一
<many-to-one name="csc" column="CSC_ID" class="com.ideal.csc.manager.entity.Csc" not-null="true" > </many-to-one>
查询出结果可以 可是赋值的时候 返回一个对象,Queue 但是结果时需要 Queue对象和 CSC 对象的NAME值
想了一下在Queue 对象中在定义个NAME 字段是为了取SQL 值赋值到Queue 对象,这样 返回页面 就直接用Queue对象来取值了
Queue.java
package com.ideal.csc.manager.entity; import com.ideal.csc.manager.entity.base.BaseQueue; /*********************************************************************** * * Queue.java * @copyright Copyright: 2009-2012 * @creator 周辉<br/> * @create-time Sep 29, 2009 3:06:35 PM * @revision $Id: * ***********************************************************************/ public class Queue extends BaseQueue { private static final long serialVersionUID = 1L; public Queue(){ super(); } public Queue(java.lang.Long id){ super(id); } private java.lang.String acdType2; public java.lang.String getAcdType2() { String type=getAcdType(); if(null!=type){ if("0".equals(type)){ acdType2="随机"; } else if("1".equals(type)){ acdType2="最少时间"; } else if("2".equals(type)){ acdType2="最少次数"; } else { acdType2="客服等级"; } } return acdType2; } public void setAcdType2(java.lang.String acdType2) { this.acdType2 = acdType2; } private java.lang.String cscname; public java.lang.String getCscname() { return cscname; } public void setCscname(java.lang.String cscname) { this.cscname = cscname; } }
定义一个cscname 设置 get set 方法
Queue类继承BaseQueue 类,其中BaseQueue类是表 WCSC_QUEUE 对应字段的get set 方法
好了,最后写DAO 层方法
hql
public Pagination getQueue(String compcode, int pageNo, int pageSize) { Finder f1 = Finder.create(" from Queue bean"); f1.append(" inner join bean.csc csc "); f1.append(" where csc.compcode=:compcode") .setParam("compcode", compcode); return find2(f1, pageNo, pageSize); }
带分页的find2 代码
protected Pagination find2(Finder finder, int pageNo, int pageSize) { int totalCount = countQueryResult(finder); Pagination p = new Pagination(pageNo, pageSize, totalCount); if (totalCount < 1) { p.setList(new ArrayList()); return p; } Query query = getSession().createQuery(finder.getOrigHql()); finder.setParamsToQuery(query); query.setFirstResult(p.getFirstResult()); query.setMaxResults(p.getPageSize()); List<Queue> list2=new ArrayList<Queue>(); Iterator iterator1 = query.list().iterator(); while (iterator1.hasNext()) { Object[] o = (Object[]) iterator1.next(); Queue queue = (Queue) o[0]; Csc csc = (Csc) o[1]; queue.setCscname(csc.getName()); list2.add(queue); } p.setList(list2); return p; }
说明
Query query = getSession().createQuery(finder.getOrigHql());
执行查询语句
Iterator iterator1 = query.list().iterator();
返回结果LIST 对2个对象进行组合成一个LIST queue 对象
因为是2个对象关联 返回结果 会返回2个对象集合,我们需要把第2个对象的NAME 字段取出 赋值到第一个对象的
cscname 字段中,于是有了下面代码
Queue queue = (Queue) o[0]; Csc csc = (Csc) o[1]; queue.setCscname(csc.getName());
最后将新的对象重新装袋LIST 中返回,这样新对象中就有NAME 字段值了
页面中我们就直接可以用queue.id ,queue.cscname ,queue.queueName 显示了。