我声明了我想要存储在数据库中的所有内容,并提供了数据类名用户。因此,不需要在数据类中定义存储库内容详细信息。但是,当我运行项目时,我会得到一个运行时异常,属性引用没有找到异常。
在这里,问题
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property existByUserName found for type User!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:90) ~[spring-data-commons-2.5.5.jar:2.5.5]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:437) ~[spring-data-commons-2.5.5.jar:2.5.5]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:413) ~[spring-data-commons-2.5.5.jar:2.5.5]
at org.springframework.data.mapping.PropertyPath.lambda$from$0(PropertyPath.java:366) ~[spring-data-commons-2.5.5.jar:2.5.5]
at java.base/java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:330) ~[na:na]
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:348) ~[spring-data-commons-2.5.5.jar:2.5.5]
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:331) ~[spring-data-commons-2.5.5.jar:2.5.5]
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:81) ~[spring-data-commons-2.5.5.jar:2.5.5]
at org.springframework.data.repository.query.parser.PartTree$OrPart.lambda$new$0(PartTree.java:249) ~[spring-data-commons-2.5.5.jar:2.5.5]
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:948) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[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:250) ~[spring-data-commons-2.5.5.jar:2.5.5]
at org.springframework.data.repository.query.parser.PartTree$Predicate.lambda$new$0(PartTree.java:383) ~[spring-data-commons-2.5.5.jar:2.5.5]
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:948) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[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:384) ~[spring-data-commons-2.5.5.jar:2.5.5]
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:92) ~[spring-data-commons-2.5.5.jar:2.5.5]
at org.springframework.data.mongodb.repository.query.PartTreeMongoQuery.<init>(PartTreeMongoQuery.java:67) ~[spring-data-mongodb-3.2.5.jar:3.2.5]
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory$MongoQueryLookupStrategy.resolveQuery(MongoRepositoryFactory.java:207) ~[spring-data-mongodb-3.2.5.jar:3.2.5]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:102) ~[spring-data-commons-2.5.5.jar:2.5.5]
... 66 common frames omitted
Disconnected from the target VM, address: '127.0.0.1:53978', transport:
“插座”
User.kt
package com.nilmani.mychat.model
import org.jetbrains.annotations.NotNull
import org.springframework.data.mongodb.core.mapping.Document
import java.time.Instant
import java.time.LocalDate
@Document
data class User(
var id:String="",
var userName:String="",
var password:String="",
var email:String="",
var createdAt:LocalDate=LocalDate.now(),
var updatedAt:LocalDate= LocalDate.now(),
var active:Boolean=false,
@NotNull
var userProfile:Profile,
@NotNull
var role:Set<Role> = HashSet()
)
UserRepository.kt
package com.nilmani.mychat.repository
import com.nilmani.mychat.model.User
import org.springframework.data.mongodb.repository.MongoRepository
import java.util.*
interface UserRepository : MongoRepository<User,String> {
fun findByUserName(userName:String):Optional<User>
fun existByUserName(userName:String): Boolean
fun existByEmail(email:String): Boolean
}
userService.kt
package com.nilmani.mychat.service
import com.nilmani.mychat.model.Role
import com.nilmani.mychat.model.User
import com.nilmani.mychat.repository.UserRepository
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
@Service
class UserService {
val log : Logger = LoggerFactory.getLogger(UserService::class.java)
@Autowired
private lateinit var userRepository: UserRepository
fun register(user:User,role:Role):User{
log.info("Regestering user {}",user.userName)
if (userRepository.existByUserName(user.userName)){
log.warn("UserName already Exist", user.userName)
}
if(userRepository.existByEmail(user.email)){
log.warn("This email already Registred",user.email)
}
user.active = true
user.password = user.password
user.role = user.role/**it auto automatically get the user type from role due to some conflict set role provided by user*/
return userRepository.save(user)
}
}
UserAuthController.kt
package com.nilmani.mychat.controller
import com.nilmani.mychat.ezception.BadRequestException
import com.nilmani.mychat.ezception.EmailAlreadyExistException
import com.nilmani.mychat.ezception.UserNameAlreadyExistException
import com.nilmani.mychat.model.ApiResponse
import com.nilmani.mychat.model.Profile
import com.nilmani.mychat.model.Role
import com.nilmani.mychat.model.User
import com.nilmani.mychat.payload.Signup
import com.nilmani.mychat.service.UserService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ModelAttribute
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.servlet.support.ServletUriComponentsBuilder
import java.net.URI
@RestController
class UserAuthController {
@Autowired
private lateinit var userService: UserService
val log:Logger=LoggerFactory.getLogger(UserAuthController::class.java)
@PostMapping(value = ["/users"], produces = [MediaType.APPLICATION_JSON_VALUE])
fun createUser(@ModelAttribute request:Signup):ResponseEntity<*>{
log.info("create User",request.username)
val user:User = User(
userName = request.username,
email = request.email,
password = request.password,
userProfile = Profile(
displayName = request.name,
profilePictureUrl = request.profilePicUrl,
)
)
try {
userService.register(user, Role.USER)
}catch (e:UserNameAlreadyExistException){
throw BadRequestException(e.message)
}catch (e:EmailAlreadyExistException){
throw BadRequestException(e.message)
}
val location : URI = ServletUriComponentsBuilder
.fromCurrentContextPath().path("users/{username}")
.buildAndExpand(user.userName).toUri()
return ResponseEntity
.created(location)
.body(ApiResponse(true,"User Registered successfully"))
}
}
这类问题产生的原因是什么,
发布于 2021-09-30 22:06:58
这个问题是为了查询,在用户存储库中定义的。因为springBoot没有预定义的查询existByUserName(用户名:字符串),所以在本例中要定义解决问题的自定义查询。
像这种类型的自定义查询
UserRepository.kt
package com.nilmani.mychat.repository
import com.nilmani.mychat.model.User
import org.springframework.data.mongodb.repository.MongoRepository
import org.springframework.data.mongodb.repository.Query
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.RequestParam
import java.util.*
interface UserRepository : MongoRepository<User, String> {
fun findByUserName(userName: String): Optional<User>
@Transactional
@Query("SELECT u from user WHERE u.userName =userName")
fun existByUserName(@RequestParam("userName") userName: String): Boolean?
@Transactional
@Query("SELECT u from user WHERE u.email =email")
fun existByEmail(@RequestParam("email") email: String): Boolean?
}
https://stackoverflow.com/questions/69400836
复制相似问题