-->
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: Lazy instantiation question
PostPosted: Thu Dec 23, 2004 2:24 pm 
Newbie

Joined: Wed Dec 22, 2004 11:36 am
Posts: 11
I'm new to Hibernate and am trying to understand what is happening under the hood with my object constructors. Here is my scenario:

2 objects - ServiceRequest and ServiceMethod. Each one sets a timestamp in the constructor when the object is created. ServiceRequest contains a reference to ServiceMethod (private field with get/set methods). What I want is the following: only one session.save() call using the ServiceRequest object, ServiceMethod is not created until it is needed (so that it's timestamp is an acurate reflection of the order of events).

Here's the order I do things...
* create ServiceRequest
* save ServiceRequest (ServiceMethod is null)
* ServiceRequest.getServiceMethod
* oh it's null - create ServiceMethod and ServiceRequest.setServiceMethod
* save ServiceRequest (ServiceMethod is not null)

I have this working, but I don't like that I have to explicitly do that null handling. I tried having getServiceMethod do the null check and create the ServiceMethod but Hibernate appeared to be calling this when saving the ServiceRequest the first time which meant that the timestamp on the object was not in the right sequence and that the object was written to the database before I wanted it to be (because I want to use cascade:save-or-update to make my save calls more bulletproof).

I like that Hibernate encourages me to think of these timestamp issues in the object layer (instead of the database layer where I usually punt it to) since they are object layer concerns. Is there a better design I can use to work with Hibernate and either tell it to create the object but not write it to the db (e.g. if certain fields are null despite the save-or-update call) or maybe setup the mapping better to handle this situation.

thx

Hibernate version:
2.1.7

Mapping documents:
Code:
<class name="testservice.ServiceMethod" table="method">
      <id name="id" type="integer" unsaved-value="0">
         <generator class="identity"/>
      </id>
      <property name="requestFilename" column="request_file"/>
      <property name="responseFilename" column="response_file"/>
      <property name="createMillis" column="create_dt"/>
      <property name="modifiedMillis" column="mod_dt"/>
   </class>

<class name="testservice.ServiceRequest" table="request">
      <id name="id" type="integer" unsaved-value="0">
         <generator class="identity"/>
      </id>
      <property name="ipAddress" column="ip_address"/>
      <property name="username" column="username"/>
      <property name="createMillis" column="create_dt"/>
      <property name="modifiedMillis" column="mod_dt"/>
      <many-to-one name="client" class="testservice.ServiceClient" column="client_id" not-null="true"/>
      <many-to-one name="method" class="testservice.ServiceMethod" column="method_id" cascade="save-update"/>
   </class>


Code between sessionFactory.openSession() and session.close():
Code:
sess.saveOrUpdate(request);
sess.flush();


Full stack trace of any exception that occurs:
n/a

Name and version of the database you are using:
MySQL 3.23.x[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 23, 2004 5:12 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
add
Code:
access="field"
to the
Code:
<many-to-one name="method" ...


HTH
Ernst


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.