-->
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.  [ 4 posts ] 
Author Message
 Post subject: Issue with composite key and caching
PostPosted: Thu Apr 01, 2004 4:02 pm 
Newbie

Joined: Thu Apr 01, 2004 3:31 pm
Posts: 4
Hi,

I'm using hibernate version 2.1 with OSCache for process level caching. Using MS SQL. Till now, I was using it to cache objects with a single key and it worked. Now I'm trying to cache an object with a composite key and it fails and instead retrieves the data from the db all the time. I have put some debug statements and I don't see the 'equals' method on the key object (FqmTaskPK) being invoked.
Listed below is the Java code (for the object that I'm attempting to cache), hbm.xml file auto generated by middlegen.

Appreciate your help.

Regards,
Jiten

package com.test;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/** @author Hibernate CodeGenerator */
public class FqmTaskPK implements Serializable {

/** identifier field */
private String id;

/** identifier field */
private String businessId;

/** full constructor */
public FqmTaskPK(String id, String businessId) {
this.id = id;
this.businessId = businessId;
}

/** default constructor */
public FqmTaskPK() {
}

/**
* @hibernate.id
* generator-class="assigned"
*
*/
public String getId() {
return this.id;
}

public void setId(String id) {
this.id = id;
}

/**
* @hibernate.id
* generator-class="assigned"
*
*/
public String getBusinessId() {
return this.businessId;
}

public void setBusinessId(String businessId) {
this.businessId = businessId;
}

public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.append("businessId", getBusinessId())
.toString();
}

public boolean equals(Object other) {
if ( !(other instanceof FqmTaskPK) ) return false;
FqmTaskPK castOther = (FqmTaskPK) other;
return new EqualsBuilder()
.append(this.getId(), castOther.getId())
.append(this.getBusinessId(), castOther.getBusinessId())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getId())
.append(getBusinessId())
.toHashCode();
}

}


hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->

<class
name="com.test.FqmTask"
table="fqm_task"
>
<meta attribute="field-description">
@hibernate.class
table="fqm_task"
</meta>
<cache usage="nonstrict-read-write"/>
<composite-id name="comp_id" class="com.hp.goit.fqm.model.FqmTaskPK">
<meta attribute="field-description">
@hibernate.id
generator-class="assigned"
</meta>
<key-property
name="id"
column="id"
type="java.lang.String"
length="50"
/>
<key-property
name="businessId"
column="business_id"
type="java.lang.String"
length="50"
/>
</composite-id>

<property
name="name"
type="java.lang.String"
column="name"
length="50"
>
<meta attribute="field-description">
@hibernate.property
column="name"
length="50"
</meta>
</property>
<property
name="role"
type="java.lang.String"
column="role"
length="50"
>
<meta attribute="field-description">
@hibernate.property
column="role"
length="50"
</meta>
</property>

<!-- associations -->
<!-- bi-directional one-to-many association to FqmActionCod -->
<set
name="fqmActionCods"
lazy="true"
inverse="true"
>
<meta attribute="field-description">
@hibernate.set
lazy="true"
inverse="true"

@hibernate.collection-key
column="action_id"
@hibernate.collection-key
column="business_id"

@hibernate.collection-one-to-many
class="com.hp.goit.fqm.model.FqmActionCod"
</meta>
<cache usage="nonstrict-read-write"/>
<key>
<column name="action_id" />
<column name="business_id" />
</key>
<one-to-many
class="com.hp.goit.fqm.model.FqmActionCod"
/>
</set>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 02, 2004 1:16 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
OSCache base key comparison on toString() method.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 02, 2004 2:53 pm 
Newbie

Joined: Thu Apr 01, 2004 3:31 pm
Posts: 4
Thanks for the clue Emmanuel. The issue with composite id caching was the key being used for caching included the FQN of the class along with it's instance id. This is ofcourse in addition to the 2 keys that we have for the table. The instance is what was causing the cache search to miss each time. Modifying the toString method fixed the problem.


Top
 Profile  
 
 Post subject: Composite Key in child entity, Hits DB each time, EHCache
PostPosted: Mon Jan 31, 2005 12:00 pm 
Newbie

Joined: Tue Jan 18, 2005 9:33 am
Posts: 3
Hi,
I am facing similar problem with EHCache.
When i see the log, i find that same Key, which was used to save data in cache is being used to retrieve the data.

Still the data is not found in cache (says, object is null) and re-queries the data

I have turned Off Lazy Loading.

Please see details on the following message

http://forum.hibernate.org/viewtopic.ph ... 2&start=15

Pls. help.

Thanks,
Gaurav


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