开发者

Database view for multiple one to many relationships

开发者 https://www.devze.com 2023-01-17 11:09 出处:网络
What is the best way to design a view for a Java/Hibernate Application for the following scenario: There is Entity A that has one to many relation with Entity B, Entity C and Entity D.

What is the best way to design a view for a Java/Hibernate Application for the following scenario:

There is Entity A that has one to many relation with Entity B, Entity C and Entity D.

There is a need to show all the relations of En开发者_如何学运维tity A in a single Table in the UI.

Does it make sense to create a database view and map that with hibernate.

or

Have all the logic in Java and populate a POJO with the results from multiple queries done via Hibernate ?

In the first case, if views are used, then is such a view Possible ? I have been unable to find info on creating views with a structure such as Entity Id, Component Id, Component Type ( where Component Id and Component Type would have values from Entity B,C,D).

Is there something that I am doing fundamentally wrong ?


SELECT a.Id AS AID, b.Id AS BID, c.Id AS CID
FROM EntityA a
LEFT JOIN EntityB b ON a.Id = b.aId
LEFT JOIN EntityC c ON a.Id = b.aId

You could make that a view but it's pretty much the same thing as executing that query each time unless your database can optimize it in some way (pre-compute it, etc.). I'm no expert on hibernate but this is a rather simple query. I can't imagine any problems.

0

精彩评论

暂无评论...
验证码 换一张
取 消