cain.kim
01/08/2024, 10:58 AM"Can't init value outside the transaction"
error message?
my intention was to take the entity Lecture with join Educator entity like
return Lecture.findById(id)?.load(Lecture::educator) ?: throw Exception("Lecture not found")
@Transactional
annotation is applied to above sentence function and then if i want to use result of above query outside of transactional function. error occurs.
So, i treated this issue with this way, define LectureAggregate
,
data class LectureAggregate(
val id: Long,
val createdAt: LocalDateTime,
val updatedAt: LocalDateTime,
val name: String,
val status: LectureStatus,
val registrationCode: String,
val educator: EducatorAggregate
) {
data class EducatorAggregate(
val id: Long,
val createdAt: LocalDateTime,
val updatedAt: LocalDateTime,
val name: String
)
constructor(lecture: Lecture) : this(
id = lecture.id.value,
createdAt = lecture.createdAt,
updatedAt = lecture.updatedAt,
name = lecture.name,
status = lecture.status,
registrationCode = lecture.registrationCode,
educator = EducatorAggregate(
id = lecture.educator.id.value,
createdAt = lecture.educator.createdAt,
updatedAt = lecture.educator.updatedAt,
name = lecture.educator.name
)
)
}
and assign the result of query to LectureAggregate
. In this way, i can avoid the error and can use LectureAggregate freely outside of transaction function. But i think it is temporary solution.
anyone has better solution for this problem?cain.kim
01/17/2024, 2:13 PM"Can't init value outside the transaction"
error keeps occuring.
why keepLoadedReferencesOutOfTransaction
options doesn’t work???
for infornation, in the test code i manually connect with databse config option with keepLoadedReferencesOutOfTransaction
, it works