How to merge data from different tables into one entity with Kotlin and JPA?

I’m new to the Kotlin world and I’ve been trying to figure out what would be the best way to represent the data fetched from 3 different tables in the database (2 out of 3 at a time), ideally as an entity with flat hierarchy, using JPA (or anything else that makes sense, really).

Table structure:

asset

asset_id INT other…

ownership

ownership_id INT owner_id INT owned_id INT owner_type VARCHAR owned_type VARCHAR other…

owner_business

owner_business_id INT other…

owner_individual

owner_individual_id INT other…

Logic:

An asset is linked to the ownership table based on asset.asset_id <-> ownership.owned_id reference, then the ownership.owner_id can reference either owner_business.owner_business_id or owner_individual.owner_individual_id and needs to be determined based on ownership.owner_type (either ‘BUSINESS’ or ‘INDIVIDUAL’).

One asset can belong to many owner_business and/or owner_individual entities, which would be represented by multiple records in the ownership table with a common owned_id.

Finally, this query logic should only show results where ownership.owned_type equals ‘ASSET’.

Goal:

JSON structure representing the owner data as a single object to be retrieved/updated via an API:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"assetId":111,
"otherFromAssetTable":"...",
"owners":[
{
"ownershipId":222,
"ownerId":333,
"ownedId":111,
"ownerType":"BUSINESS",
"otherFromOwnerBusinessTable":"..."
},
{
"ownershipId":223,
"ownerId":334,
"ownedId":111,
"ownerType":"INDIVIDUAL",
"otherFromOwnerIndividualTable":"..."
}
]
}
</code>
<code>{ "assetId":111, "otherFromAssetTable":"...", "owners":[ { "ownershipId":222, "ownerId":333, "ownedId":111, "ownerType":"BUSINESS", "otherFromOwnerBusinessTable":"..." }, { "ownershipId":223, "ownerId":334, "ownedId":111, "ownerType":"INDIVIDUAL", "otherFromOwnerIndividualTable":"..." } ] } </code>
{
   "assetId":111,
   "otherFromAssetTable":"...",
   "owners":[
      {
         "ownershipId":222,
         "ownerId":333,
         "ownedId":111,
         "ownerType":"BUSINESS",
         "otherFromOwnerBusinessTable":"..."
      },
      {
         "ownershipId":223,
         "ownerId":334,
         "ownedId":111,
         "ownerType":"INDIVIDUAL",
         "otherFromOwnerIndividualTable":"..."
      }
   ]
}

I did some research on this and tried experimenting with @Entity class inheritance, different @Inheritance strategies, secondary tables, mapped superclasses, etc. with everything ending up either not (fully) working or not giving me the results I need. I’m aware now that ultimately I should probably transform the JPA entity into a DTO and only then serve it as a nicely formatted API response, but otherwise I feel clueless and I’m reaching peak frustration over this.

Can someone think of a reasonable way to solve this?

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