1

当我尝试在表上选择其中一列是外键时出现此错误:

select count(*) from cards where username = 'name';

我是通过 java jdbc 做的,所以我认为我的映射有问题,因为通过 MySql 命令行的相同查询工作得很好:

<many-to-one name="users" class="table" update="false" insert="false" fetch="select">
        <column name="username" length="45" not-null="true" />
</many-to-one>

2013-03-02 12:19:03,660  INFO [http-bio-8080-exec-5] (NullableType.java:203) - could not read column value from result set: username; Column 'username' not found.
2013-03-02 12:19:03,663  WARN [http-bio-8080-exec-5] (JDBCExceptionReporter.java:100) - SQL Error: 0, SQLState: S0022
2013-03-02 12:19:03,664 ERROR [http-bio-8080-exec-5] (JDBCExceptionReporter.java:101) - Column 'username' not found.

我发现了这个:

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html#d0e13696

第 16.1.2 和 16.1.3 节可能是我问题的答案,但我无法弄清楚......

4

3 回答 3

2

The error message states:

could not read column value from result set: username; Column 'username' not found.

The result set for select count(*) from ... does not have a column named username. It only consists of one column, the count.

于 2013-03-02T10:49:50.590 回答
0

获取字段不应该使用“Select *”。
应该使用“从用户名='名称'的卡片中选择计数(用户名);” 表达。

于 2013-03-02T19:41:25.327 回答
0

尝试执行

 select count(*) from cards where `cards`.username='name';
于 2013-03-02T10:52:25.647 回答