2012-12-13 87 views
1

比方說,我有以下代碼:然後如何使用原始sql查詢在eBean中使用findIterate?

String sql = "select c.id, c.name from Company c left join Tag t on c.id=t.company_id where c.state='PUBLISHED'"; 

List<SqlRow> sqlRows = 
       Ebean.createSqlQuery(sql).setMaxRows(4000000).findList(); 
for(SqlRow row : sqlRows) { 
    // Do cool stuff 
} 

findList()將加載一切都交給內存這是不是我真的想在這種情況下。

我看到Query<T>findIterate()Ebean.createSqlQuery(sql)回報SqlQuery並沒有findIterate()。那麼,我該如何做到這一點?

回答

2

我想RFTM已經到位了,或者需要5分鐘的時間,然後再回到問題。 這是我做的:

服務類:

String sql = "Select c.id, c.name from Company c left join Tag t on c.id=t.company_id where c.state='PUBLISHED'";  

RawSql rawSql = RawSqlBuilder 
      .parse(sql) 
      .columnMapping("c.name", "word") 
      .columnMappingIgnore("c.id") 
      .create(); 

Query<SearchWord> query = Ebean.find(SearchWord.class); 
QueryIterator<SearchWord> iterator = query 
      .setMaxRows(4000000) 
      .setRawSql(rawSql) 
      .findIterate(); 
try { 
    while (iterator.hasNext()) { 
     iterator.next() 
     // do magic stuff in here 
    } 
} finally { 
    iterator.close(); 
} 

搜索內容

@Entity 
public class SearchWord { 

    private String word; 
    private String type; 

    public SearchWord() { 
    } 

    public String getWord() { 
     return word; 
    } 

    public void setWord(String word) { 
     this.word = word; 
    } 

    public String getType() { 
     return type; 
    } 

    public void setType(String type) { 
     this.type = type; 
    } 
} 
相關問題