Home > Enterprise >  Spring JPA Expecting Member Declaration for Derived Query
Spring JPA Expecting Member Declaration for Derived Query

Time:07-29

I'm trying to add a derived query to my Spring application to return users from my SQL database with a matching email, but I've been running into the following error (sorry for the external link, this is my 1st Stack Overflow question):

Error Image Here

Here's my code for the files that I think could conceivably matter for this:

repository.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);
}

service.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)
  }
}

entity.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
}

controller.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)
  }
}

My apologies if this is something obvious, I'm new to Spring and all the documentation I've seen has led me to believe this should be working. Other requests I've implemented so far other than findByEmail have worked so far. Thanks in advance for any help!

CodePudding user response:

The syntax of the findByEmail method in the UserRepository is not valid Kotlin, it's Java. The signature should look like this:

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

By the way, there is yet another problem: you are trying to return List<User> from UserService.findByEmail while the method return type is Optional<User>.

  • Related