-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Mapping lazy many-to-one view
PostPosted: Wed Mar 05, 2008 7:13 am 
Newbie

Joined: Fri Nov 16, 2007 10:42 am
Posts: 6
I'm trying to solve a (quite unconventional) lazy many-to-one mapping. I found a lot of similar posts but none addressed my requirements completely.

I'd like to provide Class Request with a lazy many-to-one relationship to an immutable, read-only entity Cert. Cert however is not a real table but mapped by a custom SQL loader query.

The two classes are linked by the request's property requestNumber. This property is not a foreign key, since it's an optional property on the request. A Cert object is created asynchronous for each request.

Now, when I'm retrieving a list of Request objects, Hibernate executes the custom SQL loader query for each request in order to load an associated Cert object. Since I mapped the many-to-one association as lazy I'd expect Hibernate would only load a Certobject when it is accessed.

Is there anything I need to adjust in the mapping or will Hibernate always do the lookup on the many-to-one association regardless of the lazy-setting?

Hibernate version:
3.2.5

Mapping documents:

Class Request:
Code:
<class name="Request" table="idstorerequest">
      <id name="id">
         <generator class="sequence">
            <param name="sequence">request_id_seq</param>
         </generator>
      </id>
      <version name="version" access="field" type="long" column="hibernate_version" />
      <property name="..." />
        ...
      <property name="requestNumber" type="long" unique="true" />
      <many-to-one name="cert" column="requestNumber"  class="Cert"
not-null="false" insert="false" update="false" foreign-key="none"  not-found="ignore" lazy="proxy"/>
   </class>


Class Cert:
Code:
   
<class name="Cert" mutable="false" lazy="true">
      <subselect> expensive SQL </subselect>
      <id name="requestNumber" column="requestnumber"  />
      <property ... />
     
     <loader query-ref="loadCert" /> <!-- expensive SQL loader query -->
</class>


Name and version of the database you are using:
PostgreSQL 8.2


Top
 Profile  
 
 Post subject: Solved
PostPosted: Mon Mar 10, 2008 3:42 pm 
Newbie

Joined: Fri Nov 16, 2007 10:42 am
Posts: 6
Alright I solved this one myself, partly it's alreay in the FAQs as well (see http://www.hibernate.org/162.html).

I had to remove not-found="ignore" on the many-to-one association. This will make the associaton lazy. If I would allow null values for the association (by setting not-found="ignore") Hibernate wouldn't be able to return a proxy instead of the real class. The downside is that Hibernate throws an exception if no Cert object can be found. For my use-case this is tolerable, though.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.