java - spring-data-jpa 的查询注解

标签 java spring spring-boot spring-data-jpa thymeleaf

大家好, 我想知道使用 Spring Data Jpa 的 custum 查询有什么问题。 如果我使用

UtilisateurRepositorie.findAll()

,工作正常

我的存储库:

@Repository
public interface UtilisateurRepositorie extends JpaRepository <Utilisateur, Long> {       
    @Query ("select u.idUtilisateur, u.login, u.mail, u.nom, u.prenom,u.poste.nomposte, u.idChantier.nomChantier from Utilisateur u ")
    List<Utilisateur> selectAllUser();
}

在我的 Controller 中

 @Autowired
    private UtilisateurRepositorie utilisateurRepositorie;

     @GetMapping("/userlist")
        public String listerUtilisateur(@RequestParam (name = "page", defaultValue = "0") int page , Model model) {

            List<Utilisateur> lstUser= utilisateurRepositorie.selectAllUser();

            model.addAttribute("listeusers", lstUser);

            return "utilisateur";

        }

我使用 thymeleaf 来表达我的观点:

<div>
                        <table class="table table-responsive table-bordered">
                            <thead>
                            <tr>
                                <th data-breakpoints="xs">Login</th>
                                <th>Nom</th>
                                <th>Prenom</th>
                                <th data-breakpoints="xs">mail</th>
                                <th data-breakpoints="xs">Poste</th>
                                <th data-breakpoints="xs">Chantier</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr th:each="listeuser:${listeusers}">
                                <td th:text="${listeuser.getLogin()}"></td>
                                <td th:text="${listeuser.nom}"></td>
                                <td th:text="${listeuser.prenom}"></td>
                                <td th:text="${listeuser.mail}"></td>
                                <td th:text="${listeuser.poste.nomposte}"></td>
                                <td th:text="${listeuser.idChantier.nomChantier}"></td>
                            </tr>
                            </tbody>
                        </table>
                    </div>

编译后,出现以下错误:

org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method getLogin() cannot be found on java.lang.Object[] type
    at org.springframework.expression.spel.ast.MethodReference.findAccessorForMethod(MethodReference.java:211) ~[spring-expression-4.3.6.RELEASE.jar:4.3.6.RELEASE]...

谢谢

最佳答案

那是因为您正在尝试转换 List<Object[]>List<Utilisateur> .

当您执行此类查询时

@Query ("select u.idUtilisateur, u.login, u.mail, u.nom, u.prenom,u.poste.nomposte, u.idChantier.nomChantier from Utilisateur u ")
List<Utilisateur> selectAllUser();

它返回List<Object[]> ,因为您选择了多个字段。

您应该将查询重写为 select u from Utilisateur u如果你想返回List<Utilisateur> .

否则,需要解析Object[]逐个元素。

关于java - spring-data-jpa 的查询注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49975580/

相关文章:

java - 如何正确配置Log4J?

java - 任务 ':generateContractTests' 执行失败。 Spring Cloud 合约

spring - spring-mvc 中的 Autowiring 对象是线程安全的吗?

java - Spring Integration 关于如何组合多个变量的两条路径

java - 在我通过电子邮件发送给自己后,android 中文件上的 md5 校验和与 md5 不匹配

java - 持久化数据的 DBUnit 问题

java - Docker/Marathon 上的 Spring Boot 数组环境变量

spring - ContextLoader.getCurrentWebApplicationContext() 总是返回 null

java - Android语音识别服务空指针异常

Spring Security总是返回403禁止,访问被拒绝