select new A (字段,............) 
直接转换成希望得到的类型 
网上有人用这样的hql解决过类型问题 
可是我用没有成功,有什么要注意的点吗 

解决方案 »

  1.   

    不太懂~基本上都是直接同过类对象进行操作"from A a"~用List接值~然后再转类型~
      

  2.   

    这个跟泛型有什么关系?
    举个例子:
    select new Student(name,age) from Student只查询Student的name与age属性,然后将每条记录封装成一个Student对象。语句里的new Student(name,age)要求Student这个类必须有一个类似如下的构造方法:public Student(String name,int age) {}
      

  3.   

    是的,类和构造方法都有的,不过没走到那sql就抛错了
    2008-10-24 14:19:51,312 ERROR [jp.co.bsd.lawyer.interceptor.ExceptionInterceptor] - org.hibernate.exception.SQLGrammarException: could not execute query
    javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:630)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:75)
    at jp.co.bsd.lawyer.dao.base.BaseDao.execute(BaseDao.java:235)
    at jp.co.bsd.lawyer.dao.TrnCaseDAO.caseInfoDown(TrnCaseDAO.java:937)
    at jp.co.bsd.lawyer.gj.service.imp.TrnCaseServiceImp.caseInfoDown(TrnCaseServiceImp.java:1386)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy26.caseInfoDown(Unknown Source)
      

  4.   


    你用的是Query 还是 SQLQuery?
      

  5.   

    上面的说得错了
    用的是Query 
      

  6.   

    这个就是DTO需要学一个dto的类 类似于 javabean5楼的说的对。
    可以看看 主要是 在select new DTO(name,password)--这个new 后边要写全路径select new org.com.DtoClass(name,password) from table anew org.com.DtoClass(name,password) --- 这个参数要 是 a的 属性 不能写构造器里面的否则报错。而且要和构造器里面的属性 一一对象 否则也会报错
      

  7.   

    select new jp.co.dto.AAADto(
    我写的就没有通过别人写得就通过了,不过谁也不知道原因
      

  8.   

    哦,这个叫DTO啊,谢谢,我查到了,相关的内容了DTO是数据传输对象,负责数据的传输,主要用于页面显示。本项目中用到的功能有:(1).如果某个对象有很多字段,而需要的只是部分几个,则从数据库读取时直接提取那几个字段通过构造函数封装到一个DTO中。(2)如果页面需要多个类中的某一些数据,而那些类中的字段又特别多,则可以将需要的数据从类中读出来封装到DTO中。避免读取不必要的数据,可以加快传输的速度。代码解释如下: 域模型:类A,类BPublic class A{Private Long aid;Private String userName;Private String password;Private int sex;Private Date createdDate; Public void setAid(Long aid){This.aid=aid}Public Long getAid(){Return this.aid;}以下的get set方法省略……………… } Public class B{Private Long bid;Private String userName;Private String password;Private int sex;Private Date createdDate;Private A aa; Public void setBid(Long bid){This.bid=bid}Public Long getBid(){Return this.bid;}以下的get set方法省略……………… }  类DTO Public class TestDTO{ //以下是根据页面需要的数据字段集合,可以是一个类中的部分字段,也可以是几个类中的部分字段集合Private Long aid;Private Long bid;Private String userName;Private String password; //若页面需要类A的用户名和密码Public TestDTO(String username,String password){This.userName=username;This.password=password;} //若页面需要类A的ID、用户名和类B的密码Public TestDTO(Long aid,String username,String password ){This.aid=aid;This.username=username;This.password=password;} Public void setAid(Long aid){This.aid=aid}Public Long getAid(){Return this.aid;}以下的get set方法省略……………… }         具体实现: 持久层:DAO接口:Public interface testDAO { //根据类A的ID获得A的用户名和密码封装到一个DTO中 public TestDTO getDaoByAid(Long aid); //根据类B的ID获得A的ID、用户名和类B的密码封装成DTO集合 public List  getDaoListByAid(Long bid); } DAO实现:public class testDAOImpl extends HibernateDaoSupport implements testDAO { public List  getDaoListByAid(Long bid){// ……为DTO的包路径String hql=”select new …… TestDTO(b.aa.aid,b.aa.userName,b.password)from B b where b. bid=?”List list=This.getHibernateTemplate().find(hql,new Object[]{bid});Return list; //如果是取得一个数据,可处理后在返回} } 业务层: public List  getDaoListByAid(Long bid){//testDAO自己注入return testDAO.getDaoListByAid(bid);} 表示层的View:<%If(list!=null&&!list.isEmpty())Iterator itr=list. Iterator();While(itr.hasNext()){TestDAO testDAO=(TestDAO)itr.next();Long aid=testDAO.getAid();String ausername=testDAO.getUserName();String bpassword=testDAO.getPassword();}%>
      

  9.   

    "select new com.shhicom.jfdh.gift.bean.GiftLimit(l.limitId , p.fdEnumCnet, l.limitValue) FROM JfLimit l ,JfParameter p where 1 =1 and l.limitTime = p.fdEnumId");