【问题标题】:Get a Map<Long, Long> from Ebean sql query从 Ebean sql 查询中获取 Map<Long, Long>
【发布时间】:2013-08-06 10:59:45
【问题描述】:

我正在尝试实现一个应该返回 Map 的函数,Long 值是两个不同系统的 ID。我正在使用 Java Playframework 2。

假设我的数据库中有 100 个系统。查询查找与系统之间是否存在关系,sql 中的输出是这样的:

1254          1380 
1254          1389 
1258          1259 
1259          1258 
1380          1254

我认为这是一张地图,对吧?

这是我的功能:

public static Map<Long, Long> show_all_system_relations_between_systems() {
    List <Infoobjectrelationtype> typeIdList = Infoobjectrelationtype.find.where().ilike("designation","is_a").findList();
    Long typeId = typeIdList.get(0).infoobjectrelationtype_id; 

    List <Infoobject> ioList = Infoobject.find.where().ilike("designation","SYSTEM").findList();
    Long systemId = ioList.get(0).infoobjectId;

    SqlQuery query = Ebean.createSqlQuery("select distinct ir1.infoobject_id, ir2.infoobject_id from infoobjectrelation ir1, infoobjectrelation ir2 where ir1.related_infoobject_id = ir2.related_infoobject_id and ir1.related_infoobject_id !=" + systemId + " and ir1.infoobject_id != ir2.infoobject_id and ir1.infoobject_id in (select infoobject_id from infoobjectrelation where infoobjectrelationtype_id =" +typeId+ " and related_infoobject_id =" +systemId+ ") and ir2.infoobject_id in (select infoobject_id from infoobjectrelation where infoobjectrelationtype_id =" +typeId+ " and related_infoobject_id =" + systemId +") order by ir1.infoobject_id");
    Map<Long, Long> rows = query.findMap();

    return rows;
}

错误信息:

不兼容的类型 [找到:java.util.Map [必需:java.util.Map]

我怎样才能使这个功能起作用?我可以把它列出来吗?

【问题讨论】:

    标签: java sql playframework ebean


    【解决方案1】:

    SqlQueryfindMap() 方法不返回Map&lt;Long,Long&gt;,它返回Map&lt;?, SqlRow&gt;。请参阅ebean API here

    尝试返回List&lt;SqlRow&gt;,使用findList(),然后使用SqlRow 中包含的方法访问两个Long 值。

    这里是the API doc 代表SqlRow

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-03
      • 1970-01-01
      • 2018-04-07
      • 2018-11-16
      • 2013-11-24
      相关资源
      最近更新 更多