Reputation: 407
I am currently using Kotlin 1.2 and Spring Boot 2.0 M7 with Spring Data JPA. In this project I am using a custom base repository instead of JPARepository or PagingAndSortingRepository (really doesn't matter)
Here is the base interface
@NoRepositoryBean
interface BaseRepository<T, ID : Serializable> : Repository<T, ID> {
fun <S : T> save(entity: S): S
fun findOne(id: ID): T?
fun findAll(): List<T>
fun count(): Long
}
and here is the actual repository
interface ArticleRepository : BaseRepository<Article, Int> {
}
and finally here is the Article data class
@Entity
@Table(name = "article")
@Cacheable
data class Article (
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
var id: Int? = null,
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
var member: Member? = null,
@Column(name = "title", nullable = false, length = 200)
var title: String = "",
@Column(name = "content", nullable = false, length = 65535)
var content: String = "",
@Column(name = "last_modified", nullable = false, length = 19)
var lastModified: LocalDateTime = LocalDateTime.now(),
@Column(name = "deleted", nullable = false)
var deleted: Boolean = false,
@Column(name = "effective_start", length = 19)
var effectiveStart: LocalDateTime? = null,
@Column(name = "effective_end", length = 19)
var effectiveEnd: LocalDateTime? = null,
@Version
@Column(name = "version", nullable = false)
var version: Int = 0
): Serializable {
constructor() : this(null)
constructor(member: Member, title: String, content: String, lastModified: LocalDateTime, deleted: Boolean) : this(null, member, title, content, lastModified, deleted)
}
But upon start up, I get this weird error
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.lang.Object com.nokia.srandu.oms.corrviewer.db.repo.BaseRepository.findOne(java.io.Serializable)! No property findOne found for type Article!
.
.
.
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findOne found for type Article!
I think this is related to https://jira.spring.io/browse/DATACMNS-1223 but what can I do as a work around for this? Transferring the interface to Java did not help either.
Upvotes: 0
Views: 2210
Reputation: 83081
The method name conventions for repositories have changed in Spring Data 2.0:
fun findOne(…): T?
-> fun findById(…): Optional<T>
If you would like to continue to get a simple nullable type instead of an Optional
, declare an additional or alternate query method fun getById(…): T?
and it should work as expected.
Upvotes: 2