Hibernate本机查询失败,但SQL查询有效

时间:2019-07-24 20:36:51

标签: mysql hibernate

我使用休眠状态查询

@Query(value = "BEGIN SET @rank = 1; CREATE TEMPORARY TABLE IF NOT EXISTS results "
        + "SELECT User.userId, User.username, @rank FROM User WHERE User.username = :searchString ;"

        + "SET @rank = 2; INSERT INTO results (SELECT User.userId, User.username, @rank FROM Following "
        + "JOIN User ON Following.followsId = User.userId " + "JOIN UserProfile ON UserProfile.userProfileId = User.userProfileId "
        + "WHERE Following.userId = :userId AND User.username LIKE :searchString "
        + "ORDER BY UserProfile.followsCount DESC); "

        + "SET @rank = 3; INSERT INTO results (SELECT User.userId, User.username, @rank FROM User "
        + "JOIN UserProfile ON UserProfile.userProfileId = User.userProfileId "
        + "WHERE User.username LIKE :searchString ORDER BY UserProfile.followsCount DESC); "

        + "SELECT User.* FROM results JOIN User ON User.userId = results.userId ORDER BY @rank DESC LIMIT 50; "
        + "DROP TEMPORARY TABLE results; END;", nativeQuery = true)
List<User> findLike(@Param("searchString") String searchString, @Param("userId") Long userId);

当我运行它时,被告知无法提取结果集(通过JUNIT)并在控制台中

  

16:32:24.554 [main]错误o.h.e.jdbc.spi.SqlExceptionHelper-您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以在'SET @rank = 1附近使用正确的语法;如果不存在,则创建临时表结果SELECT User.userId,'在第1行

我打开了hibernate.show_sql = true,甚至运行了使用mysqlworkbench登录到控制台的查询,并且该查询有效。

请注意,此查询在定义如下的接口中运行:

public interface IUserJpaDao extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> 

有什么想法为什么它可以在MySqlWorkbench而不是休眠模式下工作?

0 个答案:

没有答案