I have a Voucher – POJO mapped to two tables. The first mapping assigns an entity name “voucherA” and maps the POJO to TableA. The second mapping uses “voucherB” as entity name and maps the POJO to TableB.
Now i have also a customer POJO mapped to TableC. This POJO references vouchers in a list.
<list name="vouchers" table="TableC_vouchers">
<key column="pid"/>
<list-index column="position" base="0"/>
<!-- how to do that right -->
<many-to-many column="voucher_id" entity-name="voucherB"/>
</list>
How do i properly map a list of many-to-many associations from customers to vouchers so that if a customer POJO is persisted, the Voucher entities are persisted to TableB if they don’t exist there, instead of TableA? Can this be done? If not, what would a workaround look like so that vouchers used by customers are persisted to tableB? (TableA contains only available Vouchers, not the used ones)
Your core model seems wrong. Your
Voucherentity presumably has many attributes – do ALL of them change after it’s used by aCustomer? I doubt that. And yet, you’re duplicating them in your A and B tables which means your schema is not normalized.“Available” voucher and “used” voucher are not (or should not be) the same entity. I would instead suggest that you create a new entity for
UsedVoucherthat would link to bothVoucheras many-to-one andCustomeras many-to-one and contain only “changed” properties ofVoucher(if any). So,Your “many-to-many” on
Customerwill become “one-to-many” (collections of Vouchers used by this customer) IF you need it as maintainable property; otherwise it’s easily retrievable via query.You can’t physically delete from
Voucherstable under this scenario, though (unless Voucher in question was never used). You’ll have to do a logical delete instead.