先看下下面的错误!
public class WseAddress implements java.io.Serializable {
/**
*Feb 20, 2007
* Zhou JianGuo
* 小白
* 中国电信上海技术研究院
* MSN:zhuojianguo_leo@hotmail.com
*/
// Fields
private Integer addressId;
private String addressBriefEn;
private String addressBriefChn;
private Integer roadId;
private String addressNumber;
private String nearRoadId;
private Integer districtId;
private Set wseLocationAddressPhones = new HashSet(0);
是不是Integer型的就不能放在hql里使用,locationid也是
"select addressId from WseAddress" 这样写就提示Integer ,String转换出错!
org.hibernate.HibernateException: incompatible column types: integer, string
at org.hibernate.impl.AbstractScrollableResults.throwInvalidColumnTypeException(AbstractScrollableResults.java:243)
"select addressBriefEn from WseAddress" 这样写就对了
为什么上面会出现类型转换出错,发现是有2个地方
1。持久类
把持久类里的addressId对应的是Integer改为int
2。配置文件.hbm.xml
去掉addressId那个配置里的type="java.lang.Integer",默认,记住,是默认,让hibernate自己去认,不然久会出现Integer,String转换出错额。。。
查询:
有2种方法用于存放笛卡儿积
1. String query = "select new com.gis.hibernate.entity.WseLocationAddressPhoneView ( l.locationId,a.addressBriefChn,a.addressNumber,l.locationNameChn,l.type,l.popularity,l.featuredKeyWordsChn,l.editorsRecommendation,p.fixedPhone) from WseLocationAddressPhone wap,WseLocation l,WsePhone p ,WseAddress a where wap.wseLocation.locationId=l.locationId and wap.wseAddress.addressId=a.addressId and wap.wsePhone.phoneId=p.phoneId";
再建一个视图类,用于存放数据集
public WseLocationAddressPhoneView(
Integer locationId,
String addressBriefChn,
String addressNumber,
String locationNameChn,
Integer type,
Long popularity,
String featuredKeyWordsChn,
Integer editorsRecommendation,
String fixedPhone
)
{
System.out.println("locationId="+locationId.intValue());
this.locationId=locationId;
this.address_brief_chn=addressBriefChn;
this.address_number=addressNumber;
this.location_name_chn=locationNameChn;
this.type=type;
this.popularity=popularity;
this.featured_key_words_chn=featuredKeyWordsChn;
this.editors_recommendation=editorsRecommendation;
this.fixed_phone=fixedPhone;
}
2。查询的时候用List保存上面的视图类对象
String query = "select l.locationId,a.addressBriefChn,a.addressNumber,l.locationNameChn,l.type,l.popularity,l.featuredKeyWordsChn,l.editorsRecommendation,p.fixedPhone from WseLocationAddressPhone wap,WseLocation l,WsePhone p ,WseAddress a where wap.wseLocation.locationId=l.locationId and wap.wseAddress.addressId=a.addressId and wap.wsePhone.phoneId=p.phoneId";
Session session=(Session) new BaseHibernateDAO().getSession();
Transaction tx=null;
try{
tx=session.beginTransaction();
Query query1= session.createQuery(query);
System.out.println("query1="+query1);
query1.setFirstResult(page.getStartRow());
query1.setMaxResults(page.getPageSize());
ScrollableResults srs =query1.scroll();
while(srs.next()){
//String str=new String(srs.getString(0).getBytes("ISO-8859-1"),"GBK");
System.out.println("srs.getInteger(0)="+srs.getInteger(0));
list.add(new WseLocationAddressPhoneView(
srs.getInteger(0),
srs.getString(1),
srs.getString(2),
srs.getString(3),
srs.getInteger(4),
srs.getLong(5),
srs.getString(6),
srs.getInteger(7),
srs.getString(8))
);
System.out.println("完成");
}
}catch(Exception e)
{
if(tx!=null)
{
tx.rollback();
e.printStackTrace();
}
}finally{
session.close();
}
当然你再做一个持久类对应的临时表也可以,写个.hbm.xml,不过感觉没啥用,呵呵,自己试验下。。。。