2012-03-27 69 views
1

的Util測試案例中獲取數據:很奇怪的行爲,同時從DB

@Test 
     public void validate() { 
      Map<String,Object> params = new HashMap<String,Object>(); 
      params.put("code", "23102"); 
      List<ZipCodes> list = (List<ZipCodes>)this.genericDAO.findByNamedQuery("zipCodesList.findByCode",params); 
      if(list!=null){ 
       for(ZipCodes zipCodes: list) 
       { 
        System.out.println(zipCodes.getCounty()); 
        System.out.println(zipCodes.getStateCode()); 
       } 
      } 
     } 

數據庫選擇查詢,它的數據:

SELECT * FROM zip_codes其代碼= '23102'

CODE STATE_CODE  COUNTY 
23102 VA GOOCHLAND 
23102 VA HANOVER 
23102 VA LOUISA 

hql GENRATED是:

select zipcodes0_.code as code11_, zipcodes0_.city as city11_, zipcodes0_.county as county11_, zipcodes0_.state_code as state4_11_ from zip_codes zipcodes0_ where zipcodes0_.code='23102' 

Genrated data has all county with same name。

不知道會出錯嗎?

能幫助我嗎?

郵編類

@Entity 
@Table(name = "zip_codes") 
@NamedQueries(value = { 
     @NamedQuery(name = "zipCodesList.findByCode", query = "select a from ZipCodes a where a.code=:code") 
     }) 
public class ZipCodes implements Serializable{ 

    private static final long serialVersionUID = 1L; 

    @Id 
    @Column(name = "code") 
    private String code; 

    @Column(name = "state_code") 
    private String stateCode; 

    @Column(name = "county") 
    private String county; 

    @Column(name = "city") 
    private String city; 

    public String getCode() { 
     return code; 
    } 

    public void setCode(String code) { 
     this.code = code; 
    } 

    public String getStateCode() { 
     return stateCode; 
    } 

    public void setStateCode(String stateCode) { 
     this.stateCode = stateCode; 
    } 

    public String getCounty() { 
     return county; 
    } 

    public void setCounty(String county) { 
     this.county = county; 
    } 

    public String getCity() { 
     return city; 
    } 

    public void setCity(String city) { 
     this.city = city; 
    } 

} 

我在哪裏丟失無法趕上?

+0

爲什麼不嘗試直接使用** HQL **,然後使用** SQL **(用於調試目的)? – ManuPK 2012-03-27 13:33:21

回答

0

用作id的字段是問題。這不是獨一無二的。和數據預先填充在數據庫中。所以第一場比賽就回來了。

我已經使用組合鍵證明了身份。因此問題解決了。

public class ZipCodesPK implements Serializable { 

    private String code; 

    private String county; 


    public ZipCodesPK() { 
    } 

    public ZipCodesPK(String code, String county) { 
     super(); 
     this.code = code; 
     this.county = county; 
    } 

    public String getCode() { 
     return code; 
    } 

    public void setCode(String code) { 
     this.code = code; 
    } 

    public String getCounty() { 
     return county; 
    } 

    public void setCounty(String county) { 
     this.county = county; 
    } 

}