java - Spring Boot JPQL 不适用于特定条件

标签 java spring spring-boot jpa jpql

我在 Spring Boot 应用程序中面临与 JPQL 相关的问题。我面临问题“参数索引无效!您似乎声明的查询方法参数太少!” 。无法通过用户名和客户端代码获取记录。请检查我下面的 Spring Boot 应用程序代码片段。

Bean 类 UserClients.Java。

@Entity
@Table(name = "usersclients")
public class UserClients implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long ID;

    @JsonBackReference
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "userName", referencedColumnName = "userName")
    private Users user;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "clientCode", referencedColumnName = "code")
    private Clients client;
}

存储库类UserClientsRepository

@Repository
public interface UserClientsRepository extends CrudRepository<UserClients, Long> {

@Async
@Query(value = "from UserClients userCli join userCli.user user  join userCli.client client  where user.userName= ?0 and client.clientCode= ?1", nativeQuery = true)
UserClients fetchRecordByUserNameClient(String userName,String clientCode);

}

服务类UserClientsService

@Service
public class UserClientsService {

    @Autowired
    private UserClientsRepository userClientsRepository;


    public UserClients fetchRecordByUserNameClient(String username, String clientCode) {
        return userClientsRepository.fetchRecordByUserNameClient(username, clientCode);
    }

}

Controller 类AuthenticationController

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/token")
public class AuthenticationController {


    @Autowired
    private UserClientsService userClientsService;


    @RequestMapping(value = "/android-generate-token", method = RequestMethod.POST)
    public ApiResponse<AuthToken> loginActivity(@RequestBody LoginUserDto loginUser) {
        try {
            final UserClients userClients= userClientsService.fetchRecordByUserNameClient(loginUser.getUsername(), 
                    loginUser.getClient());
            if(userClients == null) {
                return new ApiResponse<>(401, "failed", null);
            }
            return new ApiResponse<>(200, "success", new AuthToken(token, user.getUserName()));
        } catch (AuthenticationException e) {
            return new ApiResponse<>(401, e.getMessage(), null);
        }
    }

}

最佳答案

您的查询有误。

1) 您设置了native = true,这意味着您要使用SQL。但查询看起来像 HQL

2)您应该使用命名参数。

此外,我不确定您想通过@Async 实现什么目标。如果不返回 Future 对象,此查询将永远不会异步运行。

所以你的查询应该是这样的:

@Repository
public interface UserClientsRepository extends CrudRepository<UserClients, Long> {

    @Async
    @Query("select userCli from UserClients userCli join userCli.user user join userCli.client client "+ 
           "where user.userName= :userName and client.clientCode= :clientCode")
    Future<UserClients> fetchRecordByUserNameClient(String userName,String clientCode);

}

关于java - Spring Boot JPQL 不适用于特定条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56443899/

相关文章:

java - 为什么 spring 应用程序在 tomcat 和 jetty 服务器上工作,而在 jboss 服务器上以 404 响应?

java - os.s.boot.SpringApplication : Application run failed

java - 在 gradle 中,如何禁用单个项目的 findbugs 或 checkstyle 插件

java - 在没有 ODBC 的情况下从 Java 操作 Access 数据库

java - 确保调用的方法在 Spring @Transactional 边界内

java - 所有任务完成后spring boot应用程序不退出

java - 如何在 Spring Boot 测试中连接到内存中的 HSQLDB 以进行查询

spring-boot - 如何确保只有一个消费者实际消费已发布的消息?

java - MaxTenuringThreshold - 它究竟是如何工作的?

java - spring boot : java. lang.NoSuchMethodError : javax. servlet.http.HttpServletRequest.getHttpServletMapping()Ljavax/servlet/http/HttpServletMapping;