无法修复 JOOQ 查询中的“未知表”异常
Posted
技术标签:
【中文标题】无法修复 JOOQ 查询中的“未知表”异常【英文标题】:Can not fix "Unknown table" exception from JOOQ query 【发布时间】:2014-06-02 09:24:53 【问题描述】:我无法从我知道存在并且我知道其格式的数据库中获取数据。
在下面的代码 sn-p 中,“if conn != null”只是一个测试,用于验证数据库名、表名等是否都正确,并且它们确实会验证。
下面的最后一行是产生异常的原因
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();
这是输出
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
.................
为了完整起见,这里是 JOOQ 生成的网络类文件的一部分
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);
..........
基于“未知表”异常,我认为连接到错误的数据库或错误的服务器有问题,但控制台输出对于 JDBC 查询是正确的。
任何想法都值得赞赏,也许其他原因可能是根本原因,或者 DSLContext 无效(但我认为这会产生不同的异常)。
【问题讨论】:
【参考方案1】:答案很简单,我没有包含 .from() 方法
Result<Record2<Integer, String>> result = dslRemote.select( nR.NETWORKID, nR.NAME )
.from(nR)
.fetch();
这就是为什么表是未知的,我从来没有把 from 方法放进去。
【讨论】:
是的,这肯定有帮助:-)以上是关于无法修复 JOOQ 查询中的“未知表”异常的主要内容,如果未能解决你的问题,请参考以下文章