Spring Data JPA OrderBy不起作用[小写字母] [大写字母]类型参数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Data JPA OrderBy不起作用[小写字母] [大写字母]类型参数相关的知识,希望对你有一定的参考价值。

嗨〜我在使用Spring Data JPA时遇到了麻烦。这个麻烦github网址:https://github.com/moregorenine/toyproject/tree/jpa_self_join_save/tree-structure-ztree

开发环境spring-boot-starter-parent:2.0.6.RELEASE + JPA + H2 spring data jpa参考:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/

一切正常。除了一个。

不工作参数类型:[小写字母] [大写字母] ... ex)tId

如果将{tId}更改为{tempId}是好工作!但我想用{tId}

Z tree service.Java

good work! no problem exmaple!!!

(O) zTrees = zTreeRepository.findAllByOrderById();
(O) zTrees = zTreeRepository.findAllByOrderByName();
(O) zTrees = zTreeRepository.findAllByOrderByIsParent();

don't work! only this type exmaple!!! why~

(X) zTrees = zTreeRepository.findAllByOrderByTId();

tId类型参数可以创建,从弹簧数据JPA插入。但'OrderBy'方法不支持[小写字母] [大写字母]类型参数吗?

Z tree.Java

package com.github.moregorenine.ztree.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.validation.constraints.NotBlank;

@Entity
public class ZTree {
    @Id
    @GeneratedValue
    private Long id; // 본인 id
    private String tId; // Ztree 내장 id
    @Transient
    private String parentTId; // Ztree 내장 pId
    @Transient
    private Long level; // lvl : 같은 그룹내 계층
    @NotBlank(message = "Name을 작성해주세요.")
    private String name;
    private String isParent;
    private String url; // 메뉴에 연결할 url
    private String useYn; // 메뉴 사용여부
    @ManyToOne(cascade={CascadeType.ALL})
    @JoinColumn(name="pId")
    private ZTree parent;
    @OneToMany(mappedBy="parent", cascade={CascadeType.ALL})
    private Set<ZTree> children = new HashSet<ZTree>();
}

Z tree service.Java

@Service
public class ZTreeService {

    @Autowired
    private ZTreeRepository zTreeRepository;

    public List<ZTree> getAllZTrees() {
        List<ZTree> zTrees = new ArrayList<>();
        zTrees = zTreeRepository.findAllByOrderByTId();
        return zTrees;
    }
}

H2数据库

enter image description here

构建失败消息

org.springframework.beans.factory.UnsatisfiedDependencyException:
    Error creating bean with name 'ZTreeController': Unsatisfied dependency expressed through field 'zTreeService';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
    Error creating bean with name 'ZTreeService': Unsatisfied dependency expressed through field 'zTreeRepository';
nested exception is org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'ZTreeRepository': Invocation of init method failed;
nested exception is java.lang.IllegalArgumentException:
    Failed to create query for method public abstract java.util.List com.github.moregorenine.ztree.repository.ZTreeRepository.findAllByOrderByTId()! Unable to locate Attribute  with the the given name [TId] on this ManagedType [com.github.moregorenine.ztree.domain.ZTree]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:370) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1336) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:548) ~[spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at com.github.moregorenine.TreeStructureZtreeApplication.main(TreeStructureZtreeApplication.java:10) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_152]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_152]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_152]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_152]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.6.RELEASE.jar:2.0.6.RELEASE]
答案

我使用'排序'而不是'OrderByTid'

zTrees = zTreeRepository.findAll(new Sort(Sort.Direction.ASC, "tId"));

以上是关于Spring Data JPA OrderBy不起作用[小写字母] [大写字母]类型参数的主要内容,如果未能解决你的问题,请参考以下文章

Spring -data-jpa ,存储库类不起作用

Spring Data JPA主键违规约束不起作用

Spring Data JPA 查询不起作用,列不存在

Spring Data JPA - 查询日期减去 2 天不起作用

使用 Spring Boot 和 Spring Data JPA 批量插入不起作用

Spring Data JPA自定义查询限制功能不起作用[重复]