How to represent a Set and Map Java Objects in datasets

Hello @Moses_Mutesasira, @Agaba_Derrick_Junior, @mherman22
I have been writing tests for the testcalculated services
And this ResultCalculation.java entity has two columns that I’m not yet sure how to represent them in datasets
one as a Set

    @CollectionTable(name = "test_operations", joinColumns = @JoinColumn(name = "result_calculation_id", referencedColumnName = "id"))
    @Column(name = "test_id")
    private Set<Test> test;

and the other a Map

    @CollectionTable(name = "test_result_map", joinColumns = @JoinColumn(name = "result_calculation_id", referencedColumnName = "id"))
    @MapKeyColumn(name = "test_id")
    @Column(name = "result_id")
    private Map<Integer, Integer> testResultMap;

I thought of designing them like this:

        <!-- ElementCollection: Set<testId> -->
    <test_operations result_calculation_id="1" test_id="401" />
    <test_operations result_calculation_id="2" test_id="402" />

And like this:

 <!-- ElementCollection: Map<testId, resultId> -->
    <test_result_map result_calculation_id="1" test_id="401" result_id="301" />
    <test_result_map result_calculation_id="2" test_id="402" result_id="302" />

Though the compiler throws an Exceptionorg.dbunit.dataset.NoPrimaryKeyException: thus suggesting that test_operations and test_result_map must have an id column, making them look like this:

   <!-- ElementCollection: Set<testId, resultId> -->
    <test_operations id="1" result_calculation_id="1" test_id="401" />
  • Which then makes me think that we should make them Java objects (entities) and use the @JoinColumn(name = "id") annotation to map them to the ResultCalculation entity

Please, I’m requesting for your ideas here, how can I create a data set for the ResultCalculation table?

@ Ariho-Seth
Don’t convert them into full entities unless you’re running into limitations beyond dataset generation.
Use your original modeling and XML representation — it’s semantically correct.

Please @Agaba_Derrick_Junior, may you help me visualise one entry that compiles successfully, because I tried different options, and most of them were generating Exceptions.
Thanks

@Ariho-Seth Have you taken a look at the coresponding liquibase scripts ?
It should give you an idea of how those objects should be represented in the DB/dataset

Thanks, @Moses_Mutesasira this has helped. Please check out my draft here for review and any suggested adjustments

1 Like

I reviewed the @ElementCollection mappings and the Liquibase scripts. It seems these tables rely on composite keys rather than an id column. Could the NoPrimaryKeyException be due to DBUnit not detecting composite keys correctly?

I just want to confirm that we should keep the current domain modeling intact and align the dataset strictly with the DB schema.

1 Like

The datasets idealy simulates the actual DB in the testing enviroment hence the dataset schema should be a replica of the db schema other wise the Integration Tests would fail

Thank you so much for the clarification. I have done the codebase setup and now I have a good understanding of the repo and how it is structured.