开发者

[N]Hibernate: view-like fetching properties of associated class

开发者 https://www.devze.com 2023-01-02 16:28 出处:网络
(Felt quite helpless in formulating an appropriate title...) Is it possible to define a mapping for a class such that some properties are fetched from another table using a join query?

(Felt quite helpless in formulating an appropriate title...)

Is it possible to define a mapping for a class such that some properties are fetched from another table using a join query?

In my C# app I display a list of "A" objects, along with some properties of their associated "B" objects and properties of B's associated "C" objects:

A.Name    B.Name    B.SomeValue    C.Name
Foo       Bar       123            HelloWorld
Bar       Hello     432            World
...

To clarify: A has an FK to B, B has an FK to C. (Such as, e.g. BankAccount -> Person -> Company).

If I extend class A by properties "BName", "BSomeValue" and "CName", can I make [N]Hibernate issue a query like

select A.Name, B.Name as BName, B.SomeValue as BSomeValue, C.Name as CName
from A
join B on A.B_ID = B.ID
join C on B.C_ID = C.ID

and map all of the results of these query onto A's properties?


Background:

I have tried two approaches to load these properties from the database (using NHibernate): A fast approach and a clean approach. My eventual question is how to do a fast & clean approach.

Fast approach:

  • Define a view in the database which joins A, B, C and provides all these fields.
  • In the A class, define properties "BName", "BSomeValue", "CName"
  • Define a hibernate mapping between A and the View, whereas the needed B and C properties are mapped with update="false" insert="false" and do a开发者_JAVA百科ctually stem from B and C tables, but Hibernate is not aware of that since it uses the view.

This way, the listing only loads one object per "A" record, which is quite fast. If the code tries to access the actual associated property, "A.B", I issue another HQL query to get B, set the property and update the faked BName and BSomeValue properties as well.

Clean approach:

  • There is no view.
  • Class A is mapped to table A, B to B, C to C.
  • When loading the list of A, I do a double left-join-fetch to get B and C as well: from A a left join fetch a.B left join fetch a.B.C
  • B.Name, B.SomeValue and C.Name are accessed through the eagerly loaded associations.

The disadvantage of this approach is that it gets slower and takes more memory, since it needs to created and map 3 objects per "A" record: An A, B, and C object each.

Fast and clean approach:

I feel somehow uncomfortable using a database view that hides a join and treat that in NHibernate as if it was a table. So I would like to do something like:

  • Have no views in the database.
  • Declare properties "BName", "BSomeValue", "CName" in class "A".
  • Define the mapping for A such that NHibernate fetches A and these properties together using a join SQL query as a database view would do.
  • The mapping should still allow for defining lazy many-to-one associations for getting A.B.C

My questions:

  • Is this possible?
  • Is it [un]artful?
  • Is there a better way?


Your "clean" approach is closest to how it should be (the others aren't even worth trying)

Before going the join fetch way, you should see what happens without it. Depending on the usage, you might find that's good enough.

You might also want to read about batch-size and caching in the docs.

And, above all, you shouldn't try to microoptimize before finding actual problems. When you say "The disadvantage of this approach is that it gets slower and takes more memory", I don't think you've actually measured it.

0

精彩评论

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