【问题标题】:No property lastname found for type User (JPA)未找到类型 User (JPA) 的属性 lastname
【发布时间】:2016-08-29 05:47:29
【问题描述】:

用户.java:

package com.hodor.booking.jpa.domain;
import javax.persistence.Entity;
import java.util.Date;

@Entity
public class User extends AbstractPersistable {

    private String firstName;

    private String lastName;

    private String email;

    private Date birthday;

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getEmail() {
        return email;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

UserRepository.java:

package com.hodor.booking.jpa.repository;

import com.hodor.booking.jpa.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * For Spring Data JPA query methods see:
 * http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods
 */
public interface UserRepository extends JpaRepository<User, Long> {

    public User findByEmail(String email);
    public User findByLastname(String lastName);
}

UserService.java:

package com.hodor.booking.service;

import com.hodor.booking.jpa.domain.User;
import com.hodor.booking.jpa.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public User saveUser(User user) {
        Assert.notNull(user);
        return userRepository.saveAndFlush(user);
    }

    public List<User> findAll() {
        return userRepository.findAll();
    }

    public User findOne(Long id) {
        Assert.notNull(id);
        return userRepository.findOne(id);
    }

    public User findByEmail(String email) {
        Assert.hasLength(email);
        return userRepository.findByEmail(email);
    }

    public User findByLastname(String lastName) {
        Assert.hasLength(lastName);
        return userRepository.findByLastname(lastName);
    }
}

用户控制器:

package com.hodor.booking.controller;

import com.hodor.booking.jpa.domain.User;
import com.hodor.booking.service.UserService;
import com.wordnik.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/v1/users")
@Api(value = "users", description = "User resource endpoint")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public List<User> index() {
        log.debug("Getting all users");
        return userService.findAll();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User get(@PathVariable("id") Long id) {
        return userService.findOne(id);
    }

    @RequestMapping(value = "/{lastName}", method = RequestMethod.GET)
    public User get(@PathVariable("lastName") String lastName) {
        return userService.findByLastname(lastName);
    }
}

堆栈跟踪:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.hodor.booking.jpa.repository.UserRepository com.hodor.booking.service.UserService.userRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property lastname found for type User!

问题:

我添加了 findByLastname 方法,但找不到问题(我是新手)。

【问题讨论】:

  • 您是否尝试使用@Column JPA 注释来注释您的用户实例字段??
  • 谢谢.. 我会在代码中的哪个位置执行此操作?
  • 在User类中...注释实例属性...示例:@Column private String firstName;
  • 你的属性是lastName,但错误是关于lastname。检查 getter/setter

标签: java spring spring-mvc jpa


【解决方案1】:

我从未使用过 Spring...但是阅读了关于org.springframework.data.jpa.repository.JpaRepositorydocs,我建议在UserRepository 界面中将您的方法重命名为:findByLastnamefindByLastName...

似乎此功能通过读取和解析从JpaRepository 扩展的给定接口处定义的方法来生成 JPA 查询,并通过说“findByLastname”,Spring 期望在用户实体中找到一个属性:@987654327 @ 而不是lastName

【讨论】:

  • 像往常一样打错字。谢谢老兄。
猜你喜欢
  • 2018-09-24
  • 2019-01-05
  • 2013-11-04
  • 1970-01-01
  • 2021-02-16
  • 2012-02-29
  • 2016-07-15
  • 2020-07-15
  • 2019-03-07
相关资源
最近更新 更多