HSQLDB:找不到列

时间:2015-10-26 20:57:19

标签: java sql left-join hsqldb functional-testing

我正在使用HSQLDB为我的应用程序编写一些功能测试。我的应用程序生成以下SQL请求:

SELECT 
CLIENT.account_id, 
CLIENT.client_code, 
SHIPPINGADDRESS10A9.account_id, 
SHIPPINGADDRESS10A9.address_code
FROM CLIENT 
LEFT JOIN ADDRESS SHIPPINGADDRESS10A9 
    ON SHIPPINGADDRESS10A9.address_code = CLIENT.shipping_address_code AND (SHIPPINGADDRESS10A9.account_id = 4) 
WHERE CLIENT.account_id = 4

此请求有效并适用于我的代码(JDBC + MySQL)。当我尝试使用HSQLDB执行我的功能测试时,我得到以下异常:

resultSet.findColumn("CLIENT.account_id"); //Works
resultSet.findColumn("SHIPPINGADDRESS10A9.account_id"); //Exception

Caused by: java.sql.SQLException: Column not found: SHIPPINGADDRESS10A9.account_id
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCResultSet.findColumn(Unknown Source)
    at com.inventis.bw.data.DataBaseUtil.loadBean(DataBaseUtil.java:419)
    ... 33 more
Caused by: org.hsqldb.HsqlException: Column not found: SHIPPINGADDRESS10A9.account_id
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    ... 36 more

知道我在这里做错了什么?

修改 看起来问题来自表别名。

logger.debug("address account id => " + resultSet.findColumn("ADDRESS.account_id")); // Works
logger.debug("address account id => " + resultSet.findColumn("SHIPPINGADDRESS10A9.account_id")); //Exception

使用表名访问列值可以很好地工作,但如果我使用表别名,则抛出异常。有什么想法吗?

感谢您的帮助。

3 个答案:

答案 0 :(得分:5)

我想你的问题是两列具有相同的别名“account_id”,这会引起混淆。尝试给他们不同的名字:

SELECT CLIENT.account_id as client_account_id,
       CLIENT.client_code, 
       SHIPPINGADDRESS10A9.account_id as shipping_account_id
       SHIPPINGADDRESS10A9.address_code

好奇,你能写这样的查询吗?

SELECT c.account_id as client_account_id,
       c.client_code, 
       a.account_id as shipping_account_id
       a.address_code
FROM CLIENT c LEFT JOIN
     ADDRESS a 
     ON a.address_code = c.shipping_address_code AND 
        a.account_id = c.account_id
WHERE c.account_id = 4;

这应该是等效的,并且明确表示您不需要在account_id中两次返回select

答案 1 :(得分:0)

我喜欢Gordon Linoff的回答。我只想补充一点,你需要在连接字符串“; get_column_name = false”中指定__如果你希望它在HSQLDB中工作

答案 2 :(得分:-1)

您应该尝试此SQL请求以正确声明连接:

SELECT 
C.account_id, 
C.client_code, 
S.account_id, 
S.address_code
FROM CLIENT C,ADDRESS S
WHERE S.address_code = C.shipping_address_code AND C.account_id = 4

你会尝试这个

resultSet.findColumn("C.account_id"); 
resultSet.findColumn("S.account_id");