通过java查询

时间:2016-08-12 11:42:59

标签: java postgresql querying

我有两个表:harvested_record和harvested_record_simple_keys有一对一关系。

harvested_record

id|harvested_record_simple_keys_id|a lot of columns



harvested_record_simple_keys

id| a lot of columns

我想进行一个查询,我需要在那里加入两个表。结果我会有一张桌子:

joined_table
id(harvested_record)| (harvested_record_simple_keys)|a lot of columns.

不幸的是我得到了一个例外:nested exception is java.sql.SQLSyntaxErrorException: Column name 'ID' matches more than one result column.

我已经明白这是因为在加入之后我会有两列' id'。有没有人可以帮我解决问题?

P.S。 SQL语句(适用于IDEA控制台):

SELECT * FROM ( SELECT TMP_ORDERED.*, ROW_NUMBER() OVER () AS ROW_NUMBER FROM (SELECT * FROM harvested_record hr JOIN harvested_record_simple_keys hrsk ON hrsk.id = hr.harvested_record_simple_keys_id WHERE import_conf_id = ? ) AS TMP_ORDERED) AS TMP_SUB WHERE TMP_SUB.ROW_NUMBER <= 2 ORDER BY import_conf_id ASC, record_id ASC;

Java代码(假设错误在这里):

JdbcPagingItemReader<HarvestedRecord> reader = new JdbcPagingItemReader<HarvestedRecord>();
SqlPagingQueryProviderFactoryBean pqpf = new SqlPagingQueryProviderFactoryBean();
pqpf.setDataSource(dataSource);
pqpf.setSelectClause("SELECT *");
pqpf.setFromClause("FROM harvested_record hr JOIN harvested_record_simple_keys hrsk ON hrsk.id = hr.harvested_record_simple_keys_id");

String whereClause = "WHERE import_conf_id = :configId";
if (from!=null) {
    fromStamp = new Timestamp(from.getTime());
    whereClause += " AND updated >= :from";
}
if (to!=null) {
    toStamp = new Timestamp(to.getTime());
    whereClause += " AND updated <= :to";
}

if (configId != null) {
    pqpf.setWhereClause(whereClause);
}

pqpf.setSortKeys(ImmutableMap.of("import_conf_id",
        Order.ASCENDING, "record_id", Order.ASCENDING));
reader.setRowMapper(harvestedRecordRowMapper);
reader.setPageSize(PAGE_SIZE);
reader.setQueryProvider(pqpf.getObject());
reader.setDataSource(dataSource);
if (configId != null) {
    Map<String, Object> parameterValues = new HashMap<String, Object>();
    parameterValues.put("configId", configId);
    parameterValues.put("from", fromStamp );
    parameterValues.put("to", toStamp);
    reader.setParameterValues(parameterValues);
}
reader.afterPropertiesSet();

提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果在select语句中命名列而不是使用'SELECT *',则可以省略其中一个表中的ID,因为它总是等于另一个表中的id。