gpt4 book ai didi

java - 从 Ebean sql 查询获取 Map

转载 作者:行者123 更新时间:2023-12-01 14:16:19 24 4
gpt4 key购买 nike

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

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

1254          1380 
1254 1389
1258 1259
1259 1258
1380 1254

我认为这是一张 map ,对吧?

这是我的功能:

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< capture#481 of ?, com.avaje.ebean.SqlRow> [必需:java.util.Map< java.lang.Long, java.lang。长>]

如何让这个功能发挥作用?我可以制作一个列表吗?

最佳答案

findMap()方法SqlQuery不返回Map<Long,Long> ,它返回 Map<?, SqlRow> 。请参阅ebean API here .

尝试返回 List<SqlRow>相反,使用 findList() ,然后使用 SqlRow 中包含的方法访问两个 Long值。

这是the API doc对于 SqlRow .

关于java - 从 Ebean sql 查询获取 Map<Long, Long>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18078330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com