https://kotlinlang.org logo
#spring
Title
# spring
s

Sheheryar Umair

02/26/2024, 12:49 PM
Hey. This is my entity code
Copy code
@Entity
class OrderFile {
    @Id
    @Column(nullable = false, updatable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Int? = null

    @Column
    var fileUrl: String? = null

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "order_id")
    var order: Order? = null

}

@Entity
@Table(name = "orders")
class Order {

    @Id
    @Column(nullable = false, updatable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Int? = null

    @Column
    var userId: Int? = null

    @Column
    var serviceId: Int? = null

    @OneToMany(mappedBy = "order", cascade = [CascadeType.ALL], orphanRemoval = true)
    var orderFiles: MutableSet<OrderFile>? = null

}
My service layer code is like this
Copy code
fun createOrder(order: Order): OrderDTO {
    val createdOrder=orderRepository.save(order)
    return createdOrder.toOrderDTO()
}
This code saves the fileURL in OrderFile Table but the order field in the OrderFile Table remains null. The required behavior I want is that when I save this the order ID should also be reflected in the orderfield in my OrderFile Table Any help in this regard would be appreciated!
k

kqr

02/26/2024, 1:01 PM
this is not related to kotlin or spring... did you set order in orderfile?
s

Sheheryar Umair

02/26/2024, 1:02 PM
In service Code or in entity code?
Just for the reference, I havent made any OrderFile repository file.
k

kqr

02/26/2024, 1:04 PM
orderFile.order = order
s

Sheheryar Umair

02/26/2024, 1:07 PM
I have updated my service layer code to
Copy code
fun createOrder(order: Order): OrderDTO {
    order.orderFiles?.forEach { it.order = order }
    val createdOrder=orderRepository.save(order)
    return createdOrder.toOrderDTO()
}
The issue is that my DB is being correctly updated but now I am getting security issue.
Copy code
Caused by: org.springframework.security.access.AccessDeniedException: Access Denied
k

kqr

02/26/2024, 1:09 PM
🤷