从Java / Spring Project执行时,SQL语句会导致错误

时间:2018-01-26 13:44:31

标签: java sql spring postgresql inner-join

在我目前的春季项目中,我已经为我的UserDetailsS​​ervice bean配置了我的spring-security xml以及这个SQL语句:

<bean id="userDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
    <property name="dataSource" ref="dataSource"></property>
    <property name="usersByUsernameQuery" value="select login, senha, enabled from usuario where login = ?"></property>
    <property name="authoritiesByUsernameQuery" value="SELECT t1.username, t2.authority FROM (SELECT u.login as username, c.nome as credencial FROM usuario u, usuario_credencial uc, credencial c WHERE u.id = uc.usuario_id and c.id = uc.credenciais_id) as t1 INNER JOIN (SELECT c.nome as credencial, a.nome as authority FROM credencial c, credencial_autorizacao ca, autorizacao a WHERE c.id = ca.credencial_id and a.id = ca.autorizacoes_id) as t2 ON t1.credencial = t2.credencial WHERE t1.username = ?;"></property>
</bean>

SQL句子:

SELECT t1.username, t2.authority
FROM (
    SELECT u.login as username, c.nome as credencial
    FROM usuario u, usuario_credencial uc, credencial c
    WHERE u.id = uc.usuario_id and c.id = uc.credenciais_id
    ) as t1
        INNER JOIN
    (
    SELECT c.nome as credencial, a.nome as authority
    FROM credencial c, credencial_autorizacao ca, autorizacao a
    WHERE c.id = ca.credencial_id and a.id = ca.autorizacoes_id
    ) as t2
        ON t1.credencial = t2.credencial
WHERE t1.username = ?;
当我在tomcat上部署并运行应用程序时,

导致此错误(下面的文本来自postgresql日志文件):

2018-01-26 07:53:41.644 -03 [20491] postgres@mydata ERROR:  syntax error at or near "INNER" at character 160
2018-01-26 07:53:41.644 -03 [20491] postgres@mydata STATEMENT:  (SELECT u.login as username, c.nome as credencial FROM usuario u, usuario_credencial uc, credencial c WHERE u.id = uc.usuario_id and c.id = uc.credenciais_id) INNER JOIN (SELECT c.nome as credencial, a.nome as authority FROM credencial c, credencial_autorizacao ca, autorizacao a WHERE c.id = ca.credencial_id and a.id = ca.autorizacoes_id)
2018-01-26 07:54:52.882 -03 [20491] postgres@mydata ERROR:  syntax error at or near "as" at character 160
2018-01-26 07:54:52.882 -03 [20491] postgres@mydata STATEMENT:  (SELECT u.login as username, c.nome as credencial FROM usuario u, usuario_credencial uc, credencial c WHERE u.id = uc.usuario_id and c.id = uc.credenciais_id) as t1 INNER JOIN (SELECT c.nome as credencial, a.nome as authority FROM credencial c, credencial_autorizacao ca, autorizacao a WHERE c.id = ca.credencial_id and a.id = ca.autorizacoes_id) as t2
2018-01-26 07:55:14.275 -03 [20491] postgres@mydata ERROR:  syntax error at or near "INNER" at character 158
2018-01-26 07:55:14.275 -03 [20491] postgres@mydata STATEMENT:  SELECT u.login as username, c.nome as credencial FROM usuario u, usuario_credencial uc, credencial c WHERE u.id = uc.usuario_id and c.id = uc.credenciais_id INNER JOIN SELECT c.nome as credencial, a.nome as authority FROM credencial c, credencial_autorizacao ca, autorizacao a WHERE c.id = ca.credencial_id and a.id = ca.autorizacoes_id
2018-01-26 07:56:55.362 -03 [20520] postgres@mydata ERROR:  syntax error at or near "as" at character 160
2018-01-26 07:56:55.362 -03 [20520] postgres@mydata STATEMENT:  (SELECT u.login as username, c.nome as credencial FROM usuario u, usuario_credencial uc, credencial c WHERE u.id = uc.usuario_id and c.id = uc.credenciais_id) as t1 INNER JOIN (SELECT c.nome as credencial, a.nome as authority FROM credencial c, credencial_autorizacao ca, autorizacao a WHERE c.id = ca.credencial_id and a.id = ca.autorizacoes_id) as t2 ON t1.credencial = t2.credencial

任何人都可以告诉我一个导致此错误的原因吗?

1 个答案:

答案 0 :(得分:0)

提出一个更简单的查询:

SELECT u.login as username, a.nome as authority
FROM usuario u
JOIN usuario_credencial uc     ON u.id = uc.usuario_id
JOIN credencial c              ON c.id = uc.credenciais_id
JOIN credencial_autorizacao ca ON c.id = ca.credencial_id
JOIN autorizacao a             ON a.id = ca.autorizacoes_id
WHERE u.login = ?;