-->
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.  [ 8 posts ] 
Author Message
 Post subject: Composite-element of a map is not passed to Interceptor
PostPosted: Fri May 07, 2004 3:59 pm 
Newbie

Joined: Sun Apr 04, 2004 10:27 am
Posts: 4
Location: Atlanta, Georgia
I'm having trouble with the Interceptor on a composite-element in a map. I have the following mapping:

Code:
<hibernate-mapping>
  <class
    name="com.termnetinc.domain.merchant.MerchantLocationBaseImpl"
    table="MERCHANT_LOCATION"
    optimistic-lock="version"
  >
    <id
      name="merchantId"
      type="long"
      column="MERCHANT_ID"
    >
      <generator class="sequence">
        <param name="sequence">merchant_location_sequence</param>
      </generator>
    </id>
    <version
      column="tuple_version"
      name="persistentVersion"
      type="long"
      unsaved-value="undefined"
    />
    <map
      name="bankAccountsPersistent"
      table="MERCHANT_BANK_ACCOUNT"
      lazy="false"
      inverse="false"
      cascade="all"
    >
      <key column="MERCHANT_ID"/>
      <index-many-to-many
        column="TYPE_ID"
        class="com.termnetinc.domain.merchant.BankAccountTypeBaseImpl"
      />
      <composite-element
        class="com.termnetinc.domain.merchant.BankAccountBaseImpl"
      >
        <property
          name="routingNumber"
          type="long"
          column="ROUTING_NUMBER"
          not-null="true"
          length="9"
        />
        <property
          name="accountNumber"
          type="java.lang.String"
          column="ACCOUNT_NUMBER"
          not-null="true"
          length="17"
        />
      </composite-element>
    </map>


BankAccountBaseImpl.java is the class in question:
Code:
public class BankAccountBaseImpl
  implements BankAccount
{
 
  private static final int HASH_CODE_INITIAL = 31;
  private static final int HASH_CODE_MULTIPLIER = 7;
 
  private String accountNumber;
  private long routingNumber;
 
  public String getAccountNumber()
  {
    return this.accountNumber;
  }

  public long getBankAccountId()
  {
    return this.bankAccountId;
  }

  public long getRoutingNumber()
  {
    return this.routingNumber;
  }
 
  public boolean equals(final Object compare)
  {
    if(!(compare instanceof BankAccountBaseImpl))
    {
      return false;
    }
    BankAccountBaseImpl compareImpl = (BankAccountBaseImpl) compare;
    EqualsBuilder builder = new EqualsBuilder();
    builder.append(this.accountNumber, compareImpl.accountNumber);
    builder.append(this.routingNumber, compareImpl.routingNumber);
    return builder.isEquals();
  }

  public int hashCode()
  {
    HashCodeBuilder builder = new HashCodeBuilder(HASH_CODE_INITIAL, HASH_CODE_MULTIPLIER);
    builder.append(this.accountNumber);
    builder.append(this.routingNumber);
    return builder.toHashCode();
  }

  public String toString()
  {
    ToStringBuilder builder = this.getStringBuilderUtils().getObjectToStringBuilder(this);
    builder.append("accountNumber", this.accountNumber);
    builder.append("routingNumber", this.routingNumber);
    return builder.toString();
  }
 
  public void setAccountNumber(final String accountNumber)
  {
    this.accountNumber = accountNumber;
  }
 
  public void setRoutingNumber(final Long routingNumber)
    throws IllegalStateException
  {
    this.routingNumber = routingNumber.longValue();
  }

}


I have the following Interceptor defined:
Code:
public class ObjectInterceptor implements Interceptor
{
 
  public ObjectInterceptor()
  {
  }
 
  public int[] findDirty
  (   final Object obj
    , final Serializable serializable
    , final Object[] obj2
    , final Object[] obj3
    , final String[] str
    , final Type[] type
  )
  {
    // do nothing
    return null;
  }

  public Object instantiate
  (   final Class clazz
    , final Serializable serializable
  ) throws CallbackException
  {
    return null;
  }

  public Boolean isUnsaved(final Object obj)
  {
    // do nothing
    return null;
  }

  public void onDelete
  (   final Object obj
    , final Serializable serializable
    , final Object[] obj2
    , final String[] str
    , final Type[] type
  ) throws CallbackException
  {
    // do nothing
  }

  public boolean onFlushDirty
  (   final Object obj
    , final Serializable serializable
    , final Object[] obj2
    , final Object[] obj3
    , final String[] str
    , final Type[] type
  ) throws CallbackException
  {
    // do nothing
    return false;
  }

  public boolean onLoad
  (   final Object obj
    , final Serializable serializable
    , final Object[] obj2
    , final String[] str
    , final Type[] type
  ) throws CallbackException
  {
    System.out.println("loading instance of " + obj.getClass().getName() + ": " + obj.hashCode());
    return false;
  }

  public boolean onSave
  (   final Object obj
    , final Serializable serializable
    , final Object[] obj2
    , final String[] str
    , final Type[] type
  ) throws CallbackException
  {
    // do nothing
    return false;
  }

  public void postFlush(final Iterator iterator) throws CallbackException
  {
    // do nothing
  }

  public void preFlush(final Iterator iterator) throws CallbackException
  {
    // do nothing
  }
 
}


Everything persists as expected. When I rehydrate, the Interceptor onLoad method is called for every Hibernate object except the composite-element in the map. Is this the expected behavior? If so, I'm curious as to why composite-elements don't get passed to onLoad, and if there a workaround to allow me to capture the loading of these as with other objects?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 10, 2004 11:43 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
element and composite element are not Entities. They do not have their own lifecycle but are attached to their parent entity lifecycle

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 7:00 am 
Newbie

Joined: Sun Apr 04, 2004 10:27 am
Posts: 4
Location: Atlanta, Georgia
Not running the composite element throught the Interceptor presents a problem for me. I am using the Interceptor onLoad to set additional properties on the object being rehydrated that are not part of the object's persistence. I need some way to capture the loading of the composite element. What alternatives are available or would be considered as an enhancement?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 7:23 am 
Beginner
Beginner

Joined: Mon Feb 23, 2004 1:17 pm
Posts: 28
You could make them normal entity classes instead of elements.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 9:26 am 
Newbie

Joined: Sun Apr 04, 2004 10:27 am
Posts: 4
Location: Atlanta, Georgia
I could do that, but I'd prefer not to, because object model-wise, they really aren't entities, and I'd be introducing a level of complexity that I really don't need, and shouldn't be forced into because of the tool. Logically, I should be able to have control over the life-cycle of a composite-element object in the same way I have control over the entity life-cycle (i.e., the Interceptor concept).


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:02 am 
Beginner
Beginner

Joined: Mon Feb 23, 2004 1:17 pm
Posts: 28
I agree, but as you saw this does not hold true with the current version of Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 18, 2004 2:36 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
mungo@knotwise wrote:
I need some way to capture the loading of the composite element. What alternatives are available or would be considered as an enhancement?

But the loading of a component IS the loading of it's entity. I'm fine with this interceptor behavior.
Map them as entities and set the cascade to all

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 18, 2004 12:49 pm 
Newbie

Joined: Sun Apr 04, 2004 10:27 am
Posts: 4
Location: Atlanta, Georgia
The loading of a component may be the loading of it's entities, but what if I need control over each of those entities. For example, the interceptor lets me intercept the instantiation of a component to substitute my own instantiation (maybe for example if I'm using a container like the Spring framework). By not allowing interception of a component's entities, Hibernate is preventing me from applying this to all the instances it instantiates.

And by not calling onLoad, it keeps me from setting non-persistent properties on the entities. Why should there be any difference between a component and it's entities when it comes to interception? These entities really, really are not top-level components, and I'd really, really like to not be forced into making them so just for infrastructure support.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.