在Kotlin Spring中获得BeanCreationException,我在做什么错?

时间:2018-12-13 21:11:07

标签: spring spring-boot kotlin

d.mp3

我正在制作电影的原始回购协议,但我不知道为什么它总是告诉我我做错了什么,这是我的课程:

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

实体:

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

1 个答案:

答案 0 :(得分:0)

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

@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而不是单个字段。在当前的实现中,如果您在影片中添加或删除字段,则必须更新存储库方法的方法签名。而且,随着电影实体的成长,参数列表将变得非常长...