2

JDBC url に ALIAS_COLUMN_NAME=TRUE を追加すると、h2 で列名に「エイリアス」を使用できるようになります。つまり、次のようにできます。

resultSet.getString("p.first_name")

p が何らかのテーブルのエイリアスである場合。このコードで示されているように、これは私にとっては機能していないようです:

package com.example;

import junit.framework.TestCase;
import org.apache.commons.dbcp.BasicDataSource;
import org.h2.Driver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

/**
 * H2 Spring Test
 */
public class H2SelectTest extends TestCase {
    public void testQuery() throws Exception {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(Driver.class.getName());
        dataSource.setUrl("jdbc:h2:mem:test;ALIAS_COLUMN_NAME=TRUE");
        dataSource.setUsername("sa");
        dataSource.setPassword("");
        JdbcTemplate template = new JdbcTemplate(dataSource);
        template.afterPropertiesSet();
        template.execute("create table people(id int auto_increment, first_name varchar);");
        SimpleJdbcInsert insert = new SimpleJdbcInsert(template).withTableName("people");
        insert.setGeneratedKeyName("id");
        insert.compile();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("first_name", "Bob");
        insert.execute(map);
        template.query("select p.first_name from people p", new RowCallbackHandler() {
            public void processRow(ResultSet rs) throws SQLException {
                String name = rs.getString("p.first_name");
            }
        });
    }
}

次のエラーが発生します。

Caused by: org.h2.jdbc.JdbcSQLException: Column "p.first_name" not found [42122-168]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
    at org.h2.message.DbException.get(DbException.java:169)
    at org.h2.message.DbException.get(DbException.java:146)
    at org.h2.jdbc.JdbcResultSet.getColumnIndex(JdbcResultSet.java:2918)
    at org.h2.jdbc.JdbcResultSet.get(JdbcResultSet.java:2979)
    at org.h2.jdbc.JdbcResultSet.getString(JdbcResultSet.java:291)
    at org.apache.commons.dbcp.DelegatingResultSet.getString(DelegatingResultSet.java:225)
    at com.example.H2SelectTest$1.processRow(H2SelectTest.java:35)
    at org.springframework.jdbc.core.JdbcTemplate$RowCallbackHandlerResultSetExtractor.extractData(JdbcTemplate.java:1482)
    at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:446)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:396)
    ... 19 more
4

1 に答える 1

3

探している答えは次のとおりです。

これらのドキュメントによると、ALIAS_COLUMN_NAMEはテーブルエイリアスではなく列名用であることが判明しました:

 /**
  * System property <code>h2.aliasColumnName</code>.<br />
  * When enabled, aliased columns (as in SELECT ID AS I FROM TEST) return the
  * alias (I in this case) in ResultSetMetaData.getColumnName() and 'null' in
  * getTableName(). If disabled, the real column name (ID in this case) and
  * table name is returned. This setting only affects the default mode.
  **/

また、テーブル エイリアスを使用した結果セットのクエリは、MySQL でのみサポートされており、JDBC 仕様ではサポートされていないことがわかります。H2 で完全なテーブル名を使用して列を明確にすることができます。別の方法を見つける必要があります (たとえば、エイリアスを作成せず、完全なテーブル名を使用します)。

他に助けが必要な場合は、私を通り越して、メモをドロップしてください。

于 2012-10-18T18:48:06.997 に答える