Hibernate 多表查询处理,涉及主键,外键关联,类型转换

本文探讨了Hibernate中HQL查询的常见问题,特别是Integer类型的字段在HQL中的使用限制及解决办法。通过调整持久化类的属性类型和配置文件中的映射设置,可以有效避免类型转换错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看下下面的错误!

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,不过感觉没啥用,呵呵,自己试验下。。。。
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值