5

我正在用 Ebean 构建一个 Play2 应用程序。我创建了一个服务类,其中包含通过 id 列表获取场所的方法:

public static List<Venue> getVenuesForIds(List<Long> list){          
    ArrayList<Venue> venues = new ArrayList<Venue>();
    String sql = "select c.id, c.name from Company c where in (:ids)";
    List<SqlRow> sqlRows =
                Ebean.createSqlQuery(sql).setParameter("ids", list).findList();        
    for(SqlRow row : sqlRows) {
        venues.add(new Venue(row.getLong("id"), row.getString("name")));
    }        
    return venues;
}

但我得到:

[PersistenceException: Query threw SQLException:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'in (201639091,201637666)' at line 1 Query was: select c.id, c.name from Company c where in (?,?) ] 

我已经阅读了http://www.avaje.org/ebean/introquery.html但可能错过了正确的语法。我想在原始 sql 中执行此操作。我错过了什么?

4

2 回答 2

3

您不需要执行这种“复杂”的查询,如果您Finder<I,T>Venue模型中使用 common(一次)就足够了:

@Entity
@Table(name = "Company")
public class Venue extends Model {

    @Id
    public Long id;
    public String name;
    // other fields

    public static Finder<Long, Venue> find
            = new Finder<Long, Venue>(Long.class, Venue.class);
}

因此,您可以使用...方法中的单行代码执行相同的任务:

 public static List<Venue> getVenuesForIds(List<Long> ids){          
     return Venue.find.select("id,name").where().idIn(ids).findList();
 }

或类似的表达方式:

 public static List<Venue> getVenuesForIds(List<Long> ids){          
     return Venue.find.select("id,name").where().in("id",ids).findList();
 }
于 2012-12-10T18:02:02.903 回答
3

您的要求似乎不正确。

关于什么 :

 "select c.id, c.name from Company c where c.id in (:ids)";
于 2012-12-10T15:16:45.807 回答