-->
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.  [ 6 posts ] 
Author Message
 Post subject: composite primary key using JCS
PostPosted: Thu Sep 18, 2003 3:18 pm 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:28 pm
Posts: 33
Location: Cincinnati, OH
Hi all,

I hope somebody could help me out!

I have a table with two fields to represent primary key, and currently I am using <composite-id> to handle it, it worked fine for me when I use session.load(.class, primaryKey), and session.update(...)

But now I want to use JCS, I added <jcs-cache usage="read-only"/> into my .hbm.xml file, session.load(.class, primaryKey) works ok for me, but if I tried to use Iterator to load all the data from that table, it'll always load them from database not from cache. (I expected to see data will from cache after the first time load)

I also tried this with a table, which has only one field as primary key, it worked very well, so I am wandering whether or not I can use JCS (Iterator) if I have composite primary key?

Someone told me to write a UserType class to include these two fields may solve this problem; I don't know how to do it? Has anyone have this kind of problem before? Or do you know the solution of this?

Thanks for all your helps.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 19, 2003 11:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
I don't use the caching in Hibernate, but I do seem to recall that there was a problem with the cache and composite keys.

For the UserType, I'm not sure if that gets around the issue (if any) or not. A UserType is pretty easy to set up. Simply code an implementation of the net.sf.hibernate.UserType interface or the net.sf.hibernate.CompositeUserType interface. Then in your mapping file, for the "type" attribute use the FQN of the class you just created.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 21, 2003 9:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Are you certain that you have implemented equals()/hashCode() correctly?

There is a known bug relating to <key-many-to-one> and the cache, but apart from that, it should work just fine.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 22, 2003 10:54 am 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:28 pm
Posts: 33
Location: Cincinnati, OH
Thanks Steve, Gavin for the reply!

I did not use <key-many-to-one>, here is my .hbm.xml file:

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

<hibernate-mapping>
   <class name="com.hibernate.Coddsc" table="coddsc">
      <jcs-cache usage="read-only"/>
       <composite-id name="id" class="com.hibernate.CoddscKey">
          <key-property name="cdty" column="cdty"/>
          <key-property name="code" column="code"/>
       </composite-id>

        <property name="dddesc" column="dddesc"/>
        <property name="ddstat" column="ddstat"/>
                </class>

</hibernate-mapping>
<!-- parsed in 0ms -->


and also this is my CoddscKey java class:

Code:
package com.hibernate;
import java.io.Serializable;

public class CoddscKey implements Serializable{

   private String cdty;
   private String code;
   
   public CoddscKey()
   {
   }
   
   public CoddscKey(String cdty, String code) {
      this.cdty = cdty;
      this.code = code;
   }
   
   public CoddscKey(String cdty)
   {
      this.cdty = cdty;
      this.code = null;
   }
   
   public String getCdty()
   {
      return cdty;
   }
   
   public void setCdty(String cdty)
   {
      this.cdty = cdty;
   }
   
   public String getCode()
   {
      return code;
   }
   
   public void setCode(String code)
   {
      this.code = code;
   }
   
   public boolean equals(Object other) {
      CoddscKey that = (CoddscKey) other;
      return this.cdty.equals(that.cdty) && this.code==that.code;
   }
   
   /**
    * Returns the hash code for the key.
    */
   public int hashCode() {
      return (this.cdty.hashCode() + this.code.hashCode());
   }
}


Any suggestions about the equals()/hashCode() function?

Thanks a lot!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 1:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You are using == to compare two strings!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 2:06 pm 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:28 pm
Posts: 33
Location: Cincinnati, OH
Thank you Gavin!

I made this working! Thanks so much!


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