spring-data-jpa Spring JPA需要派生查询的成员声明

x0fgdtte  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(143)

我尝试在Spring应用程序中添加派生查询,以便从SQL数据库中返回具有匹配电子邮件的用户,但我遇到了以下错误(抱歉提供外部链接,这是我的第一个Stack Overflow问题):
Error Image Here
下面是我的代码,我认为这些文件可能对这很重要:

存储库.kt

package com.companyname.productname.repository

import com.companyname.productname.entity.User
import org.springframework.stereotype.Repository
import org.springframework.data.jpa.repository.JpaRepository
import java.util.List

@Repository
interface UserRepository: JpaRepository<User, Long> {
    List<User> findByEmail(String email);
}

服务.kt

package com.companyname.productname.service

import com.companyname.productname.entity.User
import com.companyname.productname.repository.UserRepository
import org.springframework.stereotype.Service
import java.util.Optional;

@Service
class UserService(
  val userRepository: UserRepository
) {
  fun findAllUsers(): List<User> {
    return userRepository.findAll()
  }
  fun findOneUser(id: Long): Optional<User> {
    return userRepository.findById(id)
  }
  fun findByEmail(email: String): Optional<User> {
    return userRepository.findByEmail(email)
  }
  fun createUser(user: User) {
    userRepository.save(user)
  }
  fun updateUser(existingID: Long, info: User) {
    val userCurrent = findOneUser(existingID).get()
    val userUpdate = User()
    userUpdate.id = existingID
    userUpdate.firstName = if(info.firstName != "") info.firstName else userCurrent.firstName
    userUpdate.lastName = if(info.lastName != "") info.lastName else userCurrent.lastName
    userUpdate.email = if(info.email != "") info.email else userCurrent.email
    userUpdate.admin = info.admin
    userUpdate.enabled = info.enabled
    userRepository.save(userUpdate)
  }
  fun deleteUser(id: Long) {
    userRepository.deleteById(id)
  }
}

实体.kt

package com.companyname.productname.entity

import javax.persistence.*

@Entity
@Table(name = "users")
class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long = 0L

    @Column(name = "first_name")
    var firstName: String = ""

    @Column(name = "last_name")
    var lastName: String = ""

    @Column(name = "email")
    var email: String = ""

    @Column(name = "security_role")
    var admin: Boolean = false

    @Column(name = "enabled")
    var enabled: Boolean = true
}

控制器.kt

package com.companyname.productname.controller

import com.companyname.productname.entity.User
import com.companyname.productname.service.UserService
import org.springframework.web.bind.annotation.*
import java.util.Optional;

@RestController
class DemoController {
  @GetMapping("/hello-world")
  fun helloWorld(): String {
    return "Hello World!"
  }
}

@RestController
class UserController(
  val userService: UserService
) {
  @GetMapping("/users")
  fun findAllUsers(): List<User> {
    return userService.findAllUsers()
  }
  @GetMapping("/users/{id}")
  fun findOneUser(@PathVariable id: Long): Optional<User> {
    return userService.findOneUser(id)
  }
  @GetMapping("/users/getEmail/{email}")
  fun findByEmail(@PathVariable email: String): Optional<User> {
    return userService.findByEmail(email)
  }
  @PostMapping("/users")
  fun createUser(@RequestBody newUser: User) {
    userService.createUser(newUser)
  }
  @PutMapping("/users/{id}")
  fun updateUser(@PathVariable id: Long, @RequestBody newInfo: User) {
    userService.updateUser(id, newInfo)
  }
  @DeleteMapping("/users/{id}")
  fun deleteUser(@PathVariable id: Long) {
    userService.deleteUser(id)
  }
}

如果这是显而易见的事情,我很抱歉,我是Spring的新手,我所看到的所有文档都让我相信这应该是可行的。到目前为止,除了findByEmail之外,我所实现的其他请求都是可行的。提前感谢您的帮助!

lo8azlld

lo8azlld1#

UserRepository中的findByEmail方法的语法不是有效的Kotlin,它是Java。签名应该如下所示:

fun findByEmail(email: String): List<User>

对了,还有一个问题:您正尝试从UserService.findByEmail返回List<User>,而方法返回类型为Optional<User>

相关问题