简体   繁体   English

无法修复JOOQ查询中的“未知表”异常

[英]Can not fix “Unknown table” exception from JOOQ query

I am having trouble getting data from a database I know exists and I know the format of. 我在从已知存在且知道格式的数据库中获取数据时遇到麻烦。

In the code snippet below the "if conn != null" is just a test to verify the database name, table name, etc are all correct, and they DO verify. 在“ if conn!= null”下面的代码片段中,这只是一个测试,以验证数据库名称,表名称等是否正确,并且它们也进行了验证。

The last line below is what generates the exception 下面的最后一行是生成异常的原因

public static HashMap<Integer, String> getNetworkMapFromRemote(DSLContext dslRemote, Connection conn, Logger logger) {
    HashMap<Integer,String> remoteMap = new HashMap<Integer, String>();
    // conn is only used for test purposes
    if (conn != null) {
        // test to be sure database is ok
        try
        {
            ResultSet rs = conn.createStatement().executeQuery("SELECT networkid, name FROM network");
            while (rs.next()) {
                System.out.println("TEST: nwid " + rs.getString(1) + " name " + rs.getString(2));
            }
            rs.close();
        }
        catch ( SQLException se )
        {
            logger.trace("getNetworksForDevices SqlException: " + se.toString());
        }
    }
    // ----------- JOOQ problem section ------------------------
    Network nR = Network.NETWORK.as("network");
    // THE FOLLOWING LINE GENERATES THE UNKNOWN TABLE 
    Result<Record2<Integer, String>> result      = dslRemote.select( nR.NETWORKID, nR.NAME ).fetch();

This is the output 这是输出

TEST: nwid 1 name Network 1

org.jooq.exception.DataAccessException: SQL [select `network`.`NetworkId`, `network`.`Name` from dual]; Unknown table 'network' in field list
    at org.jooq.impl.Utils.translate(Utils.java:1288)
    at org.jooq.impl.DefaultExecuteContext.sqlException(DefaultExecuteContext.java:495)
    at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:327)
    at org.jooq.impl.AbstractResultQuery.fetch(AbstractResultQuery.java:330)
    at org.jooq.impl.SelectImpl.fetch(SelectImpl.java:2256)
    at com.nvi.kpiserver.remote.KpiCollectorUtil.getNetworkMapFromRemote(KpiCollectorUtil.java:328)
    at com.nvi.kpiserver.remote.KpiCollectorUtilTest.testUpdateKpiNetworksForRemoteIntravue(KpiCollectorUtilTest.java:61)
    .................
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'network' in field list
    .................

For the sake of completness here is part of the JOOQ generated class file for Network 为了完整起见,这是JOOQ为Network生成的类文件的一部分

package com.wbcnvi.intravue.generated.tables;
@javax.annotation.Generated(value    = { "http://www.jooq.org", "3.3.1" },
                            comments = "This class is generated by jOOQ")
@java.lang.SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Network extends org.jooq.impl.TableImpl<com.wbcnvi.intravue.generated.tables.records.NetworkRecord> {
    private static final long serialVersionUID = 1729023198;
    public static final com.wbcnvi.intravue.generated.tables.Network NETWORK = new com.wbcnvi.intravue.generated.tables.Network();
    @Override
    public java.lang.Class<com.wbcnvi.intravue.generated.tables.records.NetworkRecord> getRecordType() {
        return com.wbcnvi.intravue.generated.tables.records.NetworkRecord.class;
    }
    public final org.jooq.TableField<com.wbcnvi.intravue.generated.tables.records.NetworkRecord, java.lang.Integer> NWID = createField("NwId", org.jooq.impl.SQLDataType.INTEGER.nullable(false), this, "");
    public final org.jooq.TableField<com.wbcnvi.intravue.generated.tables.records.NetworkRecord, java.lang.Integer> NETWORKID = createField("NetworkId", org.jooq.impl.SQLDataType.INTEGER.nullable(false).defaulted(true), this, "");
    public final org.jooq.TableField<com.wbcnvi.intravue.generated.tables.records.NetworkRecord, java.lang.String> NAME = createField("Name", org.jooq.impl.SQLDataType.CHAR.length(40).nullable(false).defaulted(true), this, "");
    public final org.jooq.TableField<com.wbcnvi.intravue.generated.tables.records.NetworkRecord, java.lang.Integer> USECOUNT = createField("UseCount", org.jooq.impl.SQLDataType.INTEGER.nullable(false).defaulted(true), this, "");
    public final org.jooq.TableField<com.wbcnvi.intravue.generated.tables.records.NetworkRecord, java.lang.Integer> NETGROUP = createField("NetGroup", org.jooq.impl.SQLDataType.INTEGER.nullable(false).defaulted(true), this, "");
    public final org.jooq.TableField<com.wbcnvi.intravue.generated.tables.records.NetworkRecord, java.lang.String> AGENT = createField("Agent", org.jooq.impl.SQLDataType.CHAR.length(16), this, "");

    public Network() {
        this("network", null);
    }
    public Network(java.lang.String alias) {
        this(alias, com.wbcnvi.intravue.generated.tables.Network.NETWORK);
    }
    ..........

Based on the "unknown table" exception I thought there was a problem connected to the wrong database or wrong server, but the console output is correct for a JDBC query. 基于“未知表”异常,我认为连接到错误的数据库或错误的服务器存在问题,但是控制台输出对于JDBC查询是正确的。

Any thoughts are appreciated, perhaps something else can be the root cause or the DSLContext is not valid (but I would think that would generate a different exception). 任何想法都值得赞赏,也许其他原因可能是根本原因,或者DSLContext无效(但我认为这会产生不同的异常)。

The answer ends up being simple, I did not include the .from() method 答案最终很简单,我没有包含.from()方法

Result<Record2<Integer, String>> result = dslRemote.select( nR.NETWORKID, nR.NAME )
    .from(nR)
    .fetch();

That is why the table was unknown, I never put the from method in. 这就是为什么表未知的原因,我从来没有将from方法放入其中。

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

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