当前位置: 首页 > 知识库问答 >
问题:

没有找到类型属性的属性类型

宋华美
2023-03-14

您好,我正在建立一个动物园微服务,包括动物、员工、客户和价格表。我的动物微服务可以工作,但在我的员工微服务中,我遇到了一个错误,即没有为类型“EmployeeModel”找到属性“name”。在问这个问题之前,我已经在网上搜索了几个小时,还有一些类似的问题。我在模型中没有“名字”employee_name,我只是感到困惑,不知道如何修复它。任何指导/建议/信息将不胜感激:)第一次发布,所以我希望我的代码进入灰色框

员工模型


    package com.example.employee.model;
    
    import lombok.Getter;
    import lombok.Setter;
    import org.springframework.lang.NonNull;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "employee")
    public class EmployeeModel {
    
        @Id
        @Column(name = "employeeID", nullable = false)
        @Getter
        @Setter
        @NonNull
        private Long employeeID;
    
        @Column(name = "employee_name", nullable = false)
        @Getter
        @Setter
        @NonNull
        private String employee_name;
    
        @Column(name = "employee_salary", nullable = false)
        @Getter
        @Setter
        @NonNull
        private Long employee_salary;
    
        @Column(name = "gender", nullable = false)
        @Getter
        @Setter
        @NonNull
        private String gender;
    
        public EmployeeModel() {
            super();
        }
    
        @Override
        public String toString() {
            return "EmployeeModel{" +
                    "employeeID=" + employeeID +
                    ", employee_name='" + employee_name + '\'' +
                    ", employee_salary=" + employee_salary +
                    ", gender='" + gender + '\'' +
                    '}';
        }
    }

雇员安置


    package com.example.employee.repository;
    
    import com.example.employee.model.EmployeeModel;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.stereotype.Repository;
    
    import java.util.List;
    
    @Repository
    public interface EmployeeRepository extends JpaRepository<EmployeeModel, Long> {
        List<EmployeeModel> findByName(String employee_name);
    
        List<EmployeeModel> findByGender(String gender);
    }

员工服务


    package com.example.employee.service;
    
    import com.example.employee.model.EmployeeModel;
    import com.example.employee.repository.EmployeeRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    import java.util.Optional;
    
    @Service
    public class EmployeeService {
    
        @Autowired
        private EmployeeRepository employeeRepository;
    
        public EmployeeService(EmployeeRepository employeeRepository){
            super();
            this.employeeRepository = employeeRepository;
        }
    
        public EmployeeRepository getEmployeeRepository() {
            return employeeRepository;
        }
    
        public void setEmployeeRepository(EmployeeRepository employeeRepository) {
            this.employeeRepository = employeeRepository;
        }
    
        public List<EmployeeModel> findAll(){
            return employeeRepository.findAll();
        }
    
        public Optional<EmployeeModel> findById(Long employeeID){
            return employeeRepository.findById(employeeID);
        }
    
        public List<EmployeeModel> findByName(String employee_name){
            return employeeRepository.findByName(employee_name);
        }
    
        public List<EmployeeModel> findByGender(String gender){
            return employeeRepository.findByGender(gender);
        }
    
        public EmployeeModel addEmployee(EmployeeModel employeeModel){
            return employeeRepository.save(employeeModel);
        }
    
        public void deleteEmployee(Long employeeID){
            employeeRepository.deleteById(employeeID);
        }
    
    }

员工控制员


    package com.example.employee.controller;
    
    import com.example.employee.model.EmployeeModel;
    import com.example.employee.service.EmployeeService;
    import org.springframework.web.bind.annotation.*;
    
    import java.security.PublicKey;
    import java.util.List;
    import java.util.Optional;
    
    @RestController
    @RequestMapping("employee")
    public class EmployeeController {
    
        private EmployeeService employeeService;
    
        public EmployeeController(EmployeeService employeeService) {
            super();
            this.employeeService = employeeService;
        }
    
        public EmployeeService getEmployeeService() {
            return employeeService;
        }
    
        public void setEmployeeService(EmployeeService employeeService) {
            this.employeeService = employeeService;
        }
    
        @PostMapping("/add")
        public EmployeeModel addEmployee(@RequestBody EmployeeModel employeeModel){
            return employeeService.addEmployee(employeeModel);
        }
    
        @GetMapping("/all")
        public List<EmployeeModel> findAll(){
            return employeeService.findAll();
        }
    
        @GetMapping("/ID{employeeID}")
        public Optional<EmployeeModel> findById(@PathVariable("employeeID") Long employeeID){
            return employeeService.findById(employeeID);
        }
    
        @GetMapping("/name{employee_name}")
        public List<EmployeeModel> findByName(@PathVariable("employee_name") String employee_name){
            return employeeService.findByName(employee_name);
        }
    
        @GetMapping("/gender{gender}")
        public List<EmployeeModel> findByGender(@PathVariable("gender") String gender){
            return employeeService.findByGender(gender);
        }
    
        @DeleteMapping("/deleteEmployee")
        public void deleteEmployee(Long employeeID){
            employeeService.deleteEmployee(employeeID);
        }
    
    }

error logs
{
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.employee.repository.EmployeeRepository.name(java.lang.String)! No property 'name' found for type 'EmployeeModel'
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:96) ~[spring-data-jpa-2.7.1.jar:2.7.1]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:119) ~[spring-data-jpa-2.7.1.jar:2.7.1]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:259) ~[spring-data-jpa-2.7.1.jar:2.7.1]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:93) ~[spring-data-jpa-2.7.1.jar:2.7.1]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:103) ~[spring-data-commons-2.7.1.jar:2.7.1]
    ... 71 common frames omitted
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property 'name' found for type 'EmployeeModel'
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:91) ~[spring-data-commons-2.7.1.jar:2.7.1]
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:438) ~[spring-data-commons-2.7.1.jar:2.7.1]
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:414) ~[spring-data-commons-2.7.1.jar:2.7.1]
    at org.springframework.data.mapping.PropertyPath.lambda$from$0(PropertyPath.java:367) ~[spring-data-commons-2.7.1.jar:2.7.1]
    at java.base/java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:330) ~[na:na]
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:349) ~[spring-data-commons-2.7.1.jar:2.7.1]
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:332) ~[spring-data-commons-2.7.1.jar:2.7.1]
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:81) ~[spring-data-commons-2.7.1.jar:2.7.1]
    at org.springframework.data.repository.query.parser.PartTree$OrPart.lambda$new$0(PartTree.java:250) ~[spring-data-commons-2.7.1.jar:2.7.1]
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179) ~[na:na]
    at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:992) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[na:na]
    at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) ~[na:na]
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:251) ~[spring-data-commons-2.7.1.jar:2.7.1]
    at org.springframework.data.repository.query.parser.PartTree$Predicate.lambda$new$0(PartTree.java:384) ~[spring-data-commons-2.7.1.jar:2.7.1]
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179) ~[na:na]
    at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:992) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[na:na]
    at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) ~[na:na]
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:385) ~[spring-data-commons-2.7.1.jar:2.7.1]
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:93) ~[spring-data-commons-2.7.1.jar:2.7.1]
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:89) ~[spring-data-jpa-2.7.1.jar:2.7.1]
    ... 75 common frames omitted
}

共有1个答案

有宏邈
2023-03-14

雇员仓库中将方法的名称更改为findBy雇员名称而不是findByName

   package com.example.employee.repository;
    
    import com.example.employee.model.EmployeeModel;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.stereotype.Repository;
    
    import java.util.List;
    
    @Repository
    public interface EmployeeRepository extends JpaRepository<EmployeeModel, Long> {
        List<EmployeeModel> findByEmployeeName(String employee_name);
    
        List<EmployeeModel> findByGender(String gender);
    }
 类似资料:
  • 我已经搜索了很多页,但没有找到答案,所以我粘贴了整个代码。我正在测试测试类,并得到这样的错误:“起因:org.springframework.beans.factory.BeanCreation异常:创建名为“userRepository”的bean时出错:初始化方法调用失败;嵌套异常java.lang.IllegalArgument异常:未能为方法公共抽象创建查询org.home.mysyste

  • 我有以下实体: 然后我创建我的存储库: 但我一开始就收到: 未找到MyTable类型的属性getCountOf 我到底做错了什么?

  • 我正在尝试将ReactiveQuerydslPredicateExecitor与R2dbcRepository一起用于我的spring-data-r2dbc应用程序以及r2dbc-mssql客户端版本0.8.1。释放 我从2.2.7升级了spring boot版本。发布至2.3.0。版本和1.0.0的spring-data-r2dbc版本。发布至1.1.0。释放。我正在使用query dsl版本4

  • 我最近开始使用ReactiveCouchbase Repository(sping-data-Couchbase-3.0.0。M2,Spring-引导-启动器-父-2.0.0。M2)在我们的一个项目中。 我从这里引用了未发布的文档并设置了项目,但我得到了以下错误。 以下是关于设置我的项目的相关信息。 @Maven @爪哇 现在,我确实经历了这个和这个,但在这里,我没有在DummyRepositor

  • 我不知道为什么Spring不喜欢我的代码: 我有: : : < code > entryserviceimpl . Java : 此代码给我一个错误: org.springframework.beans.factory。BeanCreationException:创建名为“entryDao”的bean时出错:调用init方法失败;嵌套异常是org.springframework.data.map。

  • 问题内容: 我收到以下错误: 类型对象“ datetime.datetime”没有属性“ datetime” 在下一行: 有人知道错误的原因吗? 我导入日期时间是否有帮助 谢谢 问题答案: 日期时间是一个允许处理日期,时间和日期时间(所有都是数据类型)的模块。这意味着它既是顶级模块,又是该模块中的一种类型。这很令人困惑。 您的错误可能是基于模块的混乱命名,而您或您正在使用的模块已经导入了。 但是,