知道美河 | 上传资料 | VIP申请 | 精品课程 | 资料搜索 | 问题反馈 | 会员手册 | 积分消费 | 积分充值 | 帐号保护
美河学习学习在线赞助VIP

美河学习在线(主站) eimhe.com

 找回密码
 建立账号
查看: 7623|回复: 10

[求助] 一个hibernate连级模糊查询的问题

[复制链接]
发表于 2007-1-24 16:27:45 | 显示全部楼层 |阅读模式
public static void main(String[] args) {
                // TODO Auto-generated method stub
                 Session session = HibernateSessionFactory.currentSession();
                 Transaction tx = session.beginTransaction();
                 
                 String key="shanghai";
                 Object[] obj=null;
                 Account a = null;
                 Hotel h = null;
                 
                 Query query = session.createQuery("select a from Account as a join a.hotel where a.name like '%"+key+"%'");
                 List list = query.list();
                 for(int i =0;i<list.size();i++)
                {
                  obj  = (Object[])list.get(i);
                  a = (Account)obj[0];
                  h = (Hotel)obj[1];
                        System.out.println(a.getName()+h.getHotelUid()+" ");
                }
                 tx.commit();
               
                  HibernateSessionFactory.closeSession();
       
                }
实在搞不懂为什么会报java.lang.classcastException
发表于 2007-1-24 16:54:19 | 显示全部楼层
当试图将对象强制转换为不是实例的子类时,抛出该异常。
报错说的是哪行??
 楼主| 发表于 2007-1-24 17:01:48 | 显示全部楼层
public static void main(String[] args) {
                // TODO Auto-generated method stub
                 Session session = HibernateSessionFactory.currentSession();
                 Transaction tx = session.beginTransaction();
                 
                 String key="shang";
                // Object[] obj;
                 Account a ;
                 Hotel h ;
                 
                 Query query = session.createQuery("select a from Account as a join a.hotel where a.name like '%"+key+"%'");
                 List list = query.list();
                 
                 for(int i =0;i<list.size();i++)
                {
                 // h  = (Hotel)list.get(i);
                  a = (Account)list.get(i);
                 // a = (Account)obj[1];
                  //h = (Hotel)obj[2];
                        System.out.println(a.getName()+" ");
                }
                 tx.commit();
               
                  HibernateSessionFactory.closeSession();
       
                }

编译通过

public static void main(String[] args) {
                // TODO Auto-generated method stub
                 Session session = HibernateSessionFactory.currentSession();
                 Transaction tx = session.beginTransaction();
                 
                 String key="shang";
                // Object[] obj;
                 Account a ;
                 Hotel h ;
                 
                 Query query = session.createQuery("select a from Account as a join a.hotel where a.name like '%"+key+"%'");
                 List list = query.list();
                 
                 for(int i =0;i<list.size();i++)
                {
                  h  = (Hotel)list.get(i);
                 // a = (Account)list.get(i);
                 // a = (Account)obj[1];
                  //h = (Hotel)obj[2];
                        System.out.println(h.getHotelUid()+" ");
                }
                 tx.commit();
               
                  HibernateSessionFactory.closeSession();
       
                }
出现java.lang.ClassCastException异常
发表于 2007-1-24 17:03:23 | 显示全部楼层
Query query = session.createQuery("select a from Account as a join a.hotel where a.name like '%"+key+"%'");
这里面的select a 里面的a是什么啊。???如果是上面的变量的话这么写就是错的

你这样查出来的是一列。列名叫a
然后你再把这列每个值都变成2个类型,肯定异常啊

[ 本帖最后由 adamed 于 2007-1-24 17:05 编辑 ]
 楼主| 发表于 2007-1-24 17:04:37 | 显示全部楼层
发表于 2007-1-24 17:06:31 | 显示全部楼层
 楼主| 发表于 2007-1-24 17:13:06 | 显示全部楼层
那应该怎么解决呢.,表a中的 hoteluid是表h的pk,我想查出表a中的name和hoteluid...
 楼主| 发表于 2007-1-24 17:15:17 | 显示全部楼层
public static void main(String[] args) {
                // TODO Auto-generated method stub
                 Session session = HibernateSessionFactory.currentSession();
                 Transaction tx = session.beginTransaction();
                 
                 Account a = null;
                 Hotel h =null;
                 Query query = session.createQuery("from Account a join a.hotel");
                 List list = query.list();
                 Object[] obj=null;
                 for(int i =0;i<list.size();i++)
                {
                        obj = (Object[])list.get(i);
                        a =(Account)obj[0];
                        h =(Hotel)obj[1];
                        System.out.println(a.getName()+"属于"+h.getHotelUid()+" ");
                }
               
                 // HibernateSessionFactory.closeSession();
       
                }

}

这个是能查出来的..实在不解啊..
发表于 2007-1-24 17:33:34 | 显示全部楼层
这个是select *当然可以了。。有问题短信息给我吧。。。
发表于 2007-1-24 20:18:11 | 显示全部楼层
原帖由 adamed 于 2007-1-24 17:03 发表
Query query = session.createQuery("select a from Account as a join a.hotel where a.name like '%"+key+"%'");
这里面的select a 里面的a是什么啊。???如果是上面的变量的话这么写 ...


这里的a是把Account 重命名的。。
发表于 2007-1-24 20:20:15 | 显示全部楼层
原帖由 wotiancai 于 2007-1-24 17:15 发表
public static void main(String[] args) {
                // TODO Auto-generated method stub
                 Session session = HibernateSessionFactory.currentSession();
                 Transaction tx = session.beginTransaction();
                 ...

实行的是连接查询。。。。然后将list进行。。for 得到想要的数据。。。
您需要登录后才可以回帖 登录 | 建立账号

本版积分规则

 
QQ在线咨询

QQ|小黑屋|手机版|Archiver|美河学习在线 ( 浙网备33020302000026号 )

GMT+8, 2025-5-7 15:46

Powered by Discuz!

© 2001-2025 eimhe.com.

快速回复 返回顶部 返回列表