Micronaut Data Many to Many mapping does not persist

I have 2 entities – a Question and a TestPaper. The mapping between both should be many to many given that a test paper can have many questions and a question can belong in many test papers.

`@Serdeable
@MappedEntity
data class Question(
@Id @GeneratedValue(Type.AUTO) val id: Long? = null,
var department: String,
var courseId: Long,
var questionText: String,
var questionType: String,
var mediaUri: String?,

@Relation(value = MANY_TO_MANY, cascade = [ALL])
@JoinTable(
    name = "test_paper_question",
    joinColumns = [JoinColumn(name = "question_id")],
    inverseJoinColumns = [JoinColumn(name = "test_paper_id")]
)
@JsonIgnore
var testPapers: MutableList<TestPaper> = mutableListOf()

)`

@Serdeable @MappedEntity data class TestPaper( @Id @GeneratedValue(GeneratedValue.Type.AUTO) val id: Long? = null, var department: String, var courseId: Long, @Relation(value = MANY_TO_MANY, mappedBy = "testPapers") var questions: MutableList<Question> = mutableListOf() )

This how I add a question to a test paper and also retrieve it

`@Singleton
open class TestPaperService(
private val testPaperRepository: TestPaperRepository,
private val questionRepository: QuestionRepository
) {
@Transactional
open fun addQuestionToTestPaper(testPaperId: Long, questionId: Long): TestPaper {
val testPaper = testPaperRepository.findById(testPaperId).orElseThrow {
RuntimeException(“TestPaper not found with id: $testPaperId”)
}
val question = questionRepository.findById(questionId).orElseThrow {
RuntimeException(“Question not found with id: $questionId”)
}

    testPaper.questions.add(question)
    question.testPapers.add(testPaper)

    testPaperRepository.update(testPaper)
    questionRepository.update(question)

    return testPaper
}

@Transactional(readOnly = true)
open fun getQuestionsForTestPaper(testPaperId: Long): List<Question> {
    val testPaper = testPaperRepository.findById(testPaperId).orElseThrow {
        RuntimeException("TestPaper not found with id: $testPaperId")
    }
    return testPaper.questions
}

}`

I have written this test including print statements to see what the operations are doing –
@Test fun test addQuestionToTestPaper adds a question successfully`() {

    val question = Question(department = "Computer Science", courseId = 1, questionText = "What is calculus?", questionType = "Multiple Choice", mediaUri = null, testPapers = mutableListOf())
    val savedQuestion = questionRepository.save(question)

    val testPaper = TestPaper(department = "Computer Science", courseId = 1, questions = mutableListOf())
    val savedTestPaper = testPaperRepository.save(testPaper)

    val updatedTestPaper = testPaperService.addQuestionToTestPaper(savedTestPaper.id!!, savedQuestion.id!!)
    val retrieved = testPaperService.getQuestionsForTestPaper(savedTestPaper.id!!)
    val retrievedAfterUpdate = testPaperRepository.findById(3L)

    println("saved: $savedTestPaper")
    println("updated: $updatedTestPaper")
    println("retrieved: $retrievedAfterUpdate")

    assertTrue(retrieved.any { it.id == savedQuestion.id })
    assertTrue(savedQuestion.testPapers.any { it.id == savedTestPaper.id })
}`

Here are the logs of the operations
01:04:24.142 [Test worker] DEBUG io.micronaut.data.query - Executing Query: SELECT test_paper_."id",test_paper_."department",test_paper_."course_id" FROM "test_paper" test_paper_ WHERE (test_paper_."id" = ?) 01:04:24.147 [Test worker] DEBUG io.micronaut.data.query - Executing Query: SELECT question_."id",question_."department",question_."course_id",question_."question_text",question_."question_type",question_."media_uri" FROM "question" question_ WHERE (question_."id" = ?) 01:04:24.150 [Test worker] DEBUG io.micronaut.data.query - Executing SQL query: UPDATE "test_paper" SET "department"=?,"course_id"=? WHERE ("id" = ?) 01:04:24.152 [Test worker] DEBUG io.micronaut.data.query - Executing SQL query: UPDATE "question" SET "department"=?,"course_id"=?,"question_text"=?,"question_type"=?,"media_uri"=? WHERE ("id" = ?) 01:04:24.158 [Test worker] DEBUG io.micronaut.data.query - Executing SQL query: UPDATE "test_paper" SET "department"=?,"course_id"=? WHERE ("id" = ?) 01:04:24.162 [Test worker] DEBUG io.micronaut.data.query - Executing SQL query: INSERT INTO "test_paper_question" ("question_id","test_paper_id") VALUES (?,?) 01:04:24.167 [Test worker] DEBUG io.micronaut.data.query - Executing Query: SELECT test_paper_."id",test_paper_."department",test_paper_."course_id" FROM "test_paper" test_paper_ WHERE (test_paper_."id" = ?) 01:04:24.168 [Test worker] DEBUG io.micronaut.data.query - Executing Query: SELECT test_paper_."id",test_paper_."department",test_paper_."course_id" FROM "test_paper" test_paper_ WHERE (test_paper_."id" = ?) saved: TestPaper(id=3, department='Computer Science', courseId=1, questions=0) updated: TestPaper(id=3, department='Computer Science', courseId=1, questions=1) retrieved: Optional[TestPaper(id=3, department='Computer Science', courseId=1, questions=0)] 01:04:24.175 [Test worker] DEBUG i.m.t.j.DataSourceTransactionManager - Rolling back JDBC transaction on Connection [HikariProxyConnection@193383192 wrapping org.testcontainers.jdbc.ConnectionWrapper@3bb9ca38] 01:04:24.176 [Test worker] DEBUG i.m.t.j.DataSourceTransactionManager - Setting JDBC Connection [HikariProxyConnection@193383192 wrapping org.testcontainers.jdbc.ConnectionWrapper@3bb9ca38] auto-commit [true]

I tried using JPA and now Micronaut’s own Data and JDBC (I know it is not an ORM and more of a data mapper) but I get the same results using both. I even tried to switch to a One to Many where a test paper can have many questions but I simply am not able to get questions to map to test papers.

New contributor

Keshav Vel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật