Home > Software engineering >  Spring boot "Operation is not supported for read-only collection" when updating a record i
Spring boot "Operation is not supported for read-only collection" when updating a record i

Time:10-02

I am fairly new to Spring Boot and unable to find out why my CRUD repository seems to keep throwing an exception saying "Operation is not supported for read-only collection" when sending a request to a Put endpoint. All other repositories seem to work just fine. Here's my code:

User.kt

package com.karbal.tutortek.entities

import com.karbal.tutortek.dto.userDTO.UserPostDTO
import java.sql.Date
import javax.persistence.*

@Entity
@Table(name = "users")
data class User(
    @Id
    @Column(name = "id", nullable = false)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_generator")
    @SequenceGenerator(name = "user_generator", sequenceName = "user_seq", allocationSize = 1)
    var id: Long? = null,

    @Column(name = "firstName", nullable = false)
    var firstName: String = "",

    @Column(name = "lastName", nullable = false)
    var lastName: String = "",

    @Column(name = "birthDate", nullable = false)
    var birthDate: Date = Date(System.currentTimeMillis()),

    @Column(name = "rating", nullable = false)
    var rating: Float = 0.0F,

    @OneToMany(mappedBy = "user")
    var payments: List<Payment> = listOf(),

    @OneToMany(mappedBy = "user")
    var topics: List<Topic> = listOf()
){
    constructor(userPostDTO: UserPostDTO) : this(
        null,
        userPostDTO.firstName,
        userPostDTO.lastName,
        userPostDTO.birthDate,
        userPostDTO.rating
    )

    fun copy(user: User){
        firstName = user.firstName
        lastName = user.lastName
        birthDate = user.birthDate
        rating = user.rating
        payments = user.payments
        topics = user.topics
    }
}

UserController.kt

package com.karbal.tutortek.controllers

import com.karbal.tutortek.dto.userDTO.UserGetDTO
import com.karbal.tutortek.dto.userDTO.UserPostDTO
import com.karbal.tutortek.entities.User
import com.karbal.tutortek.services.UserService
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*
import org.springframework.web.server.ResponseStatusException
import java.util.*

@RestController
class UserController(val userService: UserService) {

    @PostMapping("/users/add")
    fun addUser(@RequestBody userDTO: UserPostDTO): UserGetDTO {
        val user = User(userDTO)
        return UserGetDTO(userService.saveUser(user))
    }

    @DeleteMapping("/users/{id}")
    fun deleteUser(@PathVariable id: Long){
        val user = userService.getUser(id)
        if(user.isEmpty) throw ResponseStatusException(HttpStatus.NOT_FOUND, "User not found")
        userService.deleteUser(id)
    }

    @GetMapping("/users/all")
    fun getAllUsers() = userService.getAllUsers().map { u -> UserGetDTO(u) }

    @GetMapping("/users/{id}")
    fun getUser(@PathVariable id: Long): UserGetDTO {
        val user = userService.getUser(id)
        if(user.isEmpty) throw ResponseStatusException(HttpStatus.NOT_FOUND, "User not found")
        return UserGetDTO(user.get())
    }

    @PutMapping("/users/{id}")
    fun updateUser(@PathVariable id: Long, @RequestBody userDTO: UserPostDTO){
        val user = User(userDTO)
        val userInDatabase = userService.getUser(id)
        if(userInDatabase.isEmpty) throw ResponseStatusException(HttpStatus.NOT_FOUND, "User not found")
        val extractedUser = userInDatabase.get()
        extractedUser.copy(user)
        userService.saveUser(extractedUser)
    }
}

UserService.kt

package com.karbal.tutortek.services

import com.karbal.tutortek.entities.User
import org.springframework.stereotype.Service
import com.karbal.tutortek.repositories.UserRepository

@Service
class UserService(val database: UserRepository) {

    fun getAllUsers(): List<User> = database.getAllUsers()

    fun saveUser(user: User) = database.save(user)

    fun deleteUser(id: Long) = database.deleteById(id)

    fun getUser(id: Long) = database.findById(id)
}

UserRepository.kt

package com.karbal.tutortek.repositories

import com.karbal.tutortek.entities.User
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository

@Repository
interface UserRepository : CrudRepository<User, Long> {
    @Query("SELECT * FROM users", nativeQuery = true)
    fun getAllUsers(): List<User>
}

UserPostDTO.kt

package com.karbal.tutortek.dto.userDTO

import java.sql.Date

data class UserPostDTO(
    var firstName: String,
    var lastName: String,
    var rating: Float,
    var birthDate: Date
)

The JSON that I send:

{
    "firstName": "Thomas",
    "lastName": "Thompson",
    "rating": 4.7,
    "birthDate": "2000-02-03"
}

Post works fine. Put works fine on other entities in my code. But here it always responds with 500 and a message "Operation is not supported for read-only collection". Any ideas why this could be happening?

CodePudding user response:

Just solved this after a couple of hours. Changed lists in User class to mutable lists and now it works fine.

  • Related