开发者

NHibernate Inner Join Fetch Only Some Columns?

开发者 https://www.devze.com 2023-01-19 13:48 出处:网络
I\'m developing an application with nHibernate and MySQL. I have a HQL command that runs every second, and in this command I do a \"Inner Join Fetch\", like this:

I'm developing an application with nHibernate and MySQL.

I have a HQL command that runs every second, and in this command I do a "Inner Join Fetch", like this:

"from开发者_开发百科 Order o inner join fetch o.Customer order by o.Date"

It's work fine but It fill all properties of "Customer", and I have a lot of columns in DataBase (almost 40 columns). I need only some columns like Name, Address and Telephone to show in my presentation layer.

Is there any way to get only some properties doing a Fecth Join or another way to improve performace?

Thanks...

Cheers


You could make a secondary read-only mapping that only fetches part of the columns into a Dto (Data Transfer Object):

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Your.NameSpace" assembly="Your.Assembly">
  <class name="OrderDto" table="[Order]" schema-action="none" lazy="false" mutable="false">
  <id name="Id">
    <generator class="native"/>
  </id>
  <many-to-one name="CustomerDto" class="CustomerDto"/>
  <!-- Other properties-->
  </class>
</hibernate-mapping>

This way - you get full control over what gets pulled out of the DB. There is a flip-side though - make sure that it is marked as mutable="false" or you could potentially save it without the full data.

And if you really want to get crazy - you make the OrderDto implement the same interface as the normal Order - and then when asking for a property not already loaded - you fetch the full order. This adds a fair bit of complexity, but you get an extreme amount of flexibility as well as being able to use the Dto in place of the real object with all the performance benefit of the Dto. (This is also known as the Proxy pattern whereas the first bit is sometimes called a PresentationModel as it is tailored for presentation without the normal business-logic.)


If you are looking for some properties only, don't use join fetch, because that's not what it's for.

Instead, select the properties you want and (optionally) hydrate a DTO with them. You don't even need an explicit join:

select o, o.Customer.Name, o.Customer.Address
from Order o
order by o.Date

That returns a tuple (object[]) for each row, where the 1st element is the order, the 2nd is the customer name, etc.


You could also create a database view of just those columns and table joins you want. Then tie nhibernate to the view.

0

精彩评论

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