2011-08-08 3 views
0

이 내 쿼리는 여러 테이블에서 결과를 얻으려고합니다.여러 결과 집합을 인쇄하려고합니다. 그러나 목록에만 개체를 ​​반환합니다

SQLQuery query = session.createSQLQuery("select t.id as ID, 
                t.companyname as COMPANYNAME, 
                e.fullname as FULLNAME, 
                e.empid as EMPID, 
                ca.dateallocated as DATEALLOCATED 
              from bw_tempclientdetails t, 
                bw_employee_details e, 
                bw_clientallocation ca 
              where e.empid=ca.empid and          
                ca.companyname=t.companyname "); 

그러나 query.list는 문자열 표현으로 변환 할 수없는 개체 만 반환합니다. 어떤 해결책?

+0

어떤 개체입니까? – ssedano

+0

지금까지 내가 아는 한, Java의 모든 Object는 toString() 메소드 –

답변

0

POJO를 만들 수 있습니다. 약간의 클래스와 같은 :

/** 
* Dto. 
*/ 
class MyClass { 
    int id; 
    String companyName; 
    String fullName; 
    int empId; 
    Date dateAllocated; 

    public MyClass() { 
    } 

    public MyClass(int id, String companyName, String fullName, int empId, Date dateAllocated) { 
     this.id = id; 
     this.companyName = companyName; 
     this.fullName = fullName; 
     this.empId = empId; 
     this.dateAllocated = dateAllocated; 
    } 
    // Rest of code 
} 

이렇게하면 Hibernate은 행 당 하나의 객체가 반환 인스턴스화 할 수 있습니다. 그 결과에 대한 전체 목록.

일부 요소가 nullable이면 개체 (대신 Integer)를 사용하는 것이 좋습니다.

의 인스턴스를 생성하기위한 목적 :

select new package.MyClass(colA, colB...) 

우리의 예를

SQLQuery query = session.createSQLQuery("select new a.b.MyPojo(t.id as ID, 
               t.companyname as COMPANYNAME, 
               e.fullname as FULLNAME, 
               e.empid as EMPID, 
               ca.dateallocated as DATEALLOCATED) 
             from bw_tempclientdetails t, 
               bw_employee_details e, 
               bw_clientallocation ca 
             where e.empid=ca.empid and          
               ca.companyname=t.companyname "); 

에서 행당 하나 개의 요소와 List<MyPojo>을 반환합니다. 너가 원하는게 그거야?

우도.

+0

을 가지고 있습니다. 잠시 후 모든 결과 값을 가져 오는 방법을 설명해주십시오. 나는 최대 절전 모드 상태가 아닙니다. –

+0

안녕하세요, 귀하의 솔루션을 시도했지만이 예외가 발생합니다. 원인 : com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : SQL 구문에 오류가 있습니다. '.bluewhale.model.TestPojo (t.id는 ID, t.companyname은 COMPANYNAME, e.fullname은'at line1 ') 근처에서 사용할 올바른 구문에 대해서는 MySQL 서버 버전에 해당하는 설명서를 확인하십시오.하지만 SQL을 실행할 때 혼자 MySQL의 껍질에 그것은 result.what 잘못 수 있습니다 보여줍니다? 매우 힘든 결과 값을 설정하려면 ...? –

관련 문제