简体   繁体   English

JOOQ使用Record7封装来自不同表的字段

[英]JOOQ use Record7 to encapsulate fields from different tables

I'm using JOOQ to retrieve records from 2 tables, i created custom object NearByPlace of returned columns, i read about Record7 and see that it's much better to use it rather than creating new object as below, i tried it but it failed in compilation saying that can't convert from Org.JOOQ.Record7 to record of below aliases, any idea of how to do that? 我正在使用JOOQ从2个表中检索记录,我创建了返回列的自定义对象NearByPlace ,阅读了Record7 ,发现使用它比如下创建新对象要好得多,我尝试了一下,但是编译失败说不能从Org.JOOQ.Record7转换为以下别名的记录,怎么做呢?

public List<NearByPlace> getNearBy(NearByRequestWrapper wrapper) {

return db().select(
    PLACE.NAME.as("placeName"),
    PLACE.TYPE.as("placeType"),
    PLACE.LAT.as("lat"),
    PLACE.LON.as("lon"),
    EVENT.NAME.as("eventName"),
    DSL.field("earth_distance(ll_to_earth(" + wrapper.lat() + "," + wrapper.lon() + "),ll_to_earth(place.lat, place.lon))* 0.000621371192").as("distanceToReach"),
    DSL.field(EVENT.TYPE).as("eventType"))
    .from(PLACE)
    .leftJoin(EVENT)
    .on(PLACE.ID.eq(EVENT.LOCATION_ID))
    .where(PLACE.ID.eq(1))
    .fetchInto(NearByPlace.class);

}

if you want to work with jOOQ's native RecordX types, simply invoke fetch() rather than fetchInto(Class) . 如果要使用jOOQ的本机RecordX类型,只需调用fetch()而不是fetchInto(Class)

in this case, that should yield an org.jooq.Result<Record7<...>> 在这种情况下,应产生org.jooq.Result<Record7<...>>

UPDATE UPDATE

updated sample code as requested. 根据要求更新了示例代码。

(note: this example assumes what the type parameters for Record7 should be based on the names of columns and what i think they mean. if i've guessed wrong, compilation will fail, but this should be a straightforward problem to fix.) (注意:此示例假定Record7的类型参数应基于列的名称以及我认为它们的含义。如果我猜错了,编译将失败,但这应该是一个直接解决的问题。)

        final org.joog.Result<Record7<String,String,Double,Double,String,String,String>> result = db().select(
                    PLACE.NAME.as("placeName"),
                    PLACE.TYPE.as("placeType"),
                    PLACE.LAT.as("lat"),
                    PLACE.LON.as("lon"),
                    EVENT.NAME.as("eventName"),
                    DSL.field("earth_distance(ll_to_earth(" + wrapper.lat() + "," + wrapper.lon() + "),ll_to_earth(place.lat, place.lon))* 0.000621371192").as("distanceToReach"),
                    DSL.field(EVENT.TYPE).as("eventType"))
                .from(PLACE)
                    .leftJoin(EVENT)
                        .on(PLACE.ID.eq(EVENT.LOCATION_ID))
                .where(PLACE.ID.eq(1))
                .fetch();

        return result;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM