2

I have two tables in my DB, T1 and T2. One column of T1 is a foreign key that references T2. I need only a part of the columns of T1 and T2 for my processing, thus I've created a DTO T1T2 that contains only the necessary columns. To map the DTO to these two tables with Hibernate, I used subselect attribute of the class element.

<hibernate-mapping>
<class
    name="com.xconnect.cdrrecorder.processing.dto.IngressNumRuleVoipProfile"
    table="numbermodificationrules"
    subselect="select ... from T1 left join T2 on id1=id2 where ...">

    <cache usage="read-only"/>

...

</class>
</hibernate-mapping>

I noticed that when i need to select for an object, Hibernate converts the request to two selects (one into the other).

Is there a better way to do this? What do you think about performances?

Thanks

manash
  • 6,985
  • 12
  • 65
  • 125

1 Answers1

4

If you are creating a DTO, better to fill the DTO from the query as in an example here. Only create the entity if there are backing tables.

If the DTO is used very often you can write a view in the DB and create a simple DTO entity to refer that. Since view is pre-compiled in the DB, it will be faster.

ManuPK
  • 11,623
  • 10
  • 57
  • 76
  • Thanks for your answer. In this [link](http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.html) (section 5.1.1), the use of subselect is explained. My question is in which cases we need to use subselect instead of other ways for querying? – manash Oct 23 '11 at 11:49
  • As told in the link, _Sometimes you want to use a view, but you cannot create one in the database (i.e. with a legacy schema). In this case, you can map an immutable and read-only entity to a given SQL subselect expression_. If you can, go with the view. – ManuPK Oct 23 '11 at 11:58