У меня тут какие-то проблемы.Попытка создать 10 фармплотов для моего пользователя.Вот класс данных Famplot и класс пользовательских данных:
@Entity
@IdClass(FarmPlotPK::class)
data class FarmPlot (
@Id
val slotnr: Int? = -1,
@Id
@ManyToOne
@JoinColumn(name = "username", nullable = false)
var user: User,
val plantedDate: LocalDate? = null,
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "name")
var plant: Plant? = null
){}
@Entity
@Table(name="user")
data class User (
@Id
val username: String = "",
@OneToMany(mappedBy = "user")
val farmPlots: MutableSet<FarmPlot> = HashSet()
)
С моим контроллером я пытаюсь создать 10 фармплотов с помощью этого метода в FarmPlotController:
@PostMapping("/farmPlots/create/{username}")
fun createNewFarmPlot(@PathVariable(value = "username") username:String) {
val user:User = userRepository.findById(username).get()
for (i in 0..9) {
val farmPlot = FarmPlot(slotnr = i, user = user)
println(farmPlot.toString())
// Outprint is FarmPlot(slotnr=0, user=User(username=test2, farmPlots=[]), plantedDate=null, plant=null)
farmPlotRepository.save(farmPlot)
}
}
Но при попыткедобавив farmplot для пользователя, я получаю это сообщение об ошибке:
"message": "Failed to convert property value of type 'java.lang.String' to required type 'com.merchantsofrome.server.model.User' for property 'user'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.merchantsofrome.server.model.User' for property 'user': no matching editors or conversion strategy found"
FarmPlotRepository.kt
import com.merchantsofrome.server.model.FarmPlot
import com.merchantsofrome.server.model.IdClass.FarmPlotPK
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface FarmPlotRepository : JpaRepository<FarmPlot, FarmPlotPK>
FarmPlotPK.kt
@IdClass(FarmPlotPK::class)
class FarmPlotPK : Serializable {
var slotnr: Int? = -1
var user: User? = null
init{
this.slotnr=slotnr
this.user=user
}
// The override of equals, see the rules mentioned above for creating a composite primary key
override fun equals(other: Any?): Boolean {
if (other is FarmPlotPK) {
val farmPlotPK = other as FarmPlotPK?
if (this.user!!.username == farmPlotPK!!.user!!.username && this.slotnr == farmPlotPK.slotnr) {
return true
}
} else {
return false
}
return false
}
// The override of hashCode, see the rules mentioned above for creating a composite primary key
override fun hashCode(): Int {
return super.hashCode()
}
}