【问题标题】:Getting a BeanCreationException in Kotlin Spring, what am I doing wrong?在 Kotlin Spring 中获取 BeanCreationException,我做错了什么?
【发布时间】:2018-12-13 21:11:07
【问题描述】:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'movieRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract boolean no.kristiania.soj.groupexam.movie.MovieRepositoryCustom.update(long,java.lang.String,java.lang.String,java.lang.String,java.lang.String,int,java.time.ZonedDateTime)! No property update found for type MovieEntity!

Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract boolean no.kristiania.soj.groupexam.movie.MovieRepositoryCustom.update(long,java.lang.String,java.lang.String,java.lang.String,java.lang.String,int,java.time.ZonedDateTime)! No property update found for type MovieEntity!

我正在为一部电影做一个粗略的回购,但我不知道为什么它一直告诉我我做错了什么,这些是我的课程:

也许我忽略了一些东西,但我一直在重写和重构以试图找出我的错误,但无济于事。

实体:

@Entity
class MovieEntity(
    @get:NotBlank
    var title: String,

    @get:NotBlank
    var director: String,

    @get:NotBlank
    var description: String,

    @get:NotBlank
    var info: String,

    @get:NotNull
    var rating: Int,

    @get:NotNull
    var releaseDate: ZonedDateTime,

    @get:Id
    @get:GeneratedValue
    var id: Long? = null
    )

自定义存储库

@Repository
interface MovieRepository : CrudRepository<MovieEntity, Long>, 
MovieRepositoryCustom

@Transactional
interface MovieRepositoryCustom {
fun createMovie(
        title: String,
        director: String,
        description: String,
        info: String,
        rating: Int,
        releaseDate: ZonedDateTime
) : Long

fun update(
        id: Long,
        title: String,
        director: String,
        description: String,
        info: String,
        rating: Int,
        releaseDate: ZonedDateTime
) : Boolean
}

@Repository
@Transactional
class MovieRepositoryImplementation : MovieRepositoryCustom {
@Autowired
private lateinit var entityManager: EntityManager

override fun createMovie(
        title: String,
        director: String,
        description: String,
        info: String,
        rating: Int,
        releaseDate: ZonedDateTime
): Long {
    val movie = MovieEntity(title, director, description, info, rating, releaseDate)
    entityManager.persist(movie)
    return movie.id!!
}

override fun update(
        id: Long,
        title: String,
        director: String,
        description: String,
        info: String,
        rating: Int,
        releaseDate: ZonedDateTime
) : Boolean {
    val movie = entityManager.find(MovieEntity::class.java, id) ?: return false

    movie.title = title
    movie.director = director
    movie.description = description
    movie.info = info
    movie.rating = rating
    movie.releaseDate = releaseDate

    return true
   }
}

然后我尝试运行测试以检查它是否有效,但它在开始时给了我错误:

@RunWith(SpringRunner::class)
@SpringBootTest(classes = [(MovieApplication::class)],
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class RestTest {
@LocalServerPort
var port = 0

@Before
@After
fun clean() {
    RestAssured.baseURI = "http://localhost"
    RestAssured.port = port
    RestAssured.basePath = "/movie"
    RestAssured.enableLoggingOfRequestAndResponseIfValidationFails()

    val list = RestAssured.given().accept(ContentType.JSON).get()
            .then()
            .statusCode(200)
            .extract()
            .`as`(Array<MovieDTO>::class.java)
            .toList()

    list.stream().forEach {
        RestAssured.given().pathParam("id", it.id)
                .delete("/{id}")
                .then()
                .statusCode(204)
    }
    RestAssured.given().get()
            .then()
            .statusCode(200)
            .body("size()", CoreMatchers.equalTo(0))
}

@Test
fun testCreateMovie() {
    val title = "title"
    val director = "director"
    val description = "description"
    val info = "info"
    val rating = 4
    val releaseDate = ZonedDateTime.now()

    val dto = MovieDTO(title, director, description, info, rating, releaseDate)

    RestAssured.given().accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
            .get()
            .then()
            .statusCode(200)
            .body("size()", CoreMatchers.equalTo(0))

    val id = RestAssured.given().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
            .body(dto)
            .post()
            .then()
            .statusCode(201)
            .extract().asString()

    RestAssured.given().accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
            .get()
            .then()
            .statusCode(200)
            .body("size()", CoreMatchers.equalTo(1))
    RestAssured.given().accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
            .pathParam("id", id)
            .get("/{id}")
            .then()
            .statusCode(200)
            .body("title", CoreMatchers.equalTo(title))
            .body("director", CoreMatchers.equalTo(director))
            .body("description", CoreMatchers.equalTo(description))
            .body("info", CoreMatchers.equalTo(info))
            .body("rating", CoreMatchers.equalTo(rating))
            .body("releaseDate", CoreMatchers.equalTo(releaseDate))
            .body("id", CoreMatchers.equalTo(id))
}

【问题讨论】:

    标签: spring spring-boot kotlin


    【解决方案1】:

    为您的存储库实现尝试此测试:

    @SpringBootTest
    @RunWith(SpringRunner::class)
    class MovieRepositoryImplementationTest {
    
        @Autowired
        private lateinit var movieRepository: MovieRepositoryImplementation
    
        @Test
        fun createAndUpdateAMovie() {
            val id = movieRepository.createMovie("title", "dir", "desc", "info", 5, ZonedDateTime.now())
            val wasUpdated = movieRepository.update(id, "title", "dir", "desc", "info", 5, ZonedDateTime.now())
    
            assertTrue(wasUpdated)
        }
    }
    

    我还建议重构您的 createMovieupdateMovie 方法以接受 MovieEntity 而不是单个字段。在您当前的实现中,如果您在影片中添加或删除字段,则必须更新存储库方法的方法签名。此外,随着电影实体的增长,参数列表会变得非常长......

    【讨论】:

    • 谢谢,帮了大忙!
    猜你喜欢
    • 2020-01-11
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多