-->
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.  [ 3 posts ] 
Author Message
 Post subject: Serializing a Map
PostPosted: Fri Jun 02, 2006 3:02 am 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:14 am
Posts: 30
Hi All,

I am using Hibernate 3.1.2 inside Spring to power my Tomcat webapp. I have an object with some arbitrary metadata attached to it; the metadata is stored in a HashMap<String, Serializable>, and is serialized into a BYTEA column (as you can see from the excerpts below).

In my webapp, I display a list of objects on the screen, and provide a "next" button that the user can click to display them one after another. The objects are in a parent-child relationship, as you can see below.

The problem is that Hibernate always tries to update each object, because it always thinks that the "metadata" property is dirty. As far as I can tell, Hibernate uses the following code to check if properties are dirty:

Code:
public final class EqualsHelper {

   public static boolean equals(Object x, Object y) {
      return x==y || ( x!=null && y!=null && x.equals(y) );
   }
   
   private EqualsHelper() {}

}


So, my question is twofold:

1). How come x !=y in this case ? This can only happen if I have two instances of an object with the same primary key, but I'm pretty sure I'm not creating extra instances. Besides, shouldn't Hibernate keep track of identical instances in its session cache ?

2). Assuming that, in the worst-case scenario, #1 is unsolvable, how do I fix this ? One answer might be, "represent your Map in a table in the database instead of serializing it in". I'd like to avoid this solution, though, because I could have literally millions of objects in the database, and if each of them has 5 metadata properties on the average, I will have to join against a metadata table with 5 million rows in it.

Thanks for your help...

Hibernate version: 3.1.2

Mapping documents:
<class name="com.digitalmountain.domain.ReviewableItem" table="reviewable_item">

<id name="id" type="long">
<column name="id" sql-type="bigint" not-null="true"/>
<generator class="assigned"/>
</id>

<discriminator column="subclass" type="string"/>

<version name="version" column="version"/>
<many-to-one name="parent" column="parent_id" class="com.digitalmountain.domain.ReviewableItem"
not-null="false" lazy="proxy" cascade="persist"/>

<property name="metadata" column="metadata_map" type="serializable"/>
...

Name and version of the database you are using: PostgreSQL

Debug level Hibernate log excerpt:
2006-06-01 23:38:00,703 DEBUG [org.hibernate.util.SerializationHelper] - Starting serialization of object [{MimeType=application/x-msexcel, primary_author=Dominik Kacprzak, company=Digital Mountain, creation_date=2/18/2005 06:02:14, last_print_date=2/18/2005 08:13:43, Encoding=0, AttachmentShortName=search_e.xls, links_dirty=no, _PID_HLINKS=Features, scale_crop=no, security=0, source=Microsoft Excel, last_saved_by=Dominik Kacprzak}]
2006-06-01 23:38:00,703 DEBUG [org.hibernate.util.SerializationHelper] - Starting serialization of object [{MimeType=application/x-msexcel, primary_author=Dominik Kacprzak, company=Digital Mountain, creation_date=2/18/2005 06:02:14, last_print_date=2/18/2005 08:13:43, Encoding=0, AttachmentShortName=search_e.xls, links_dirty=no, security=0, scale_crop=no, _PID_HLINKS=Features, source=Microsoft Excel, last_saved_by=Dominik Kacprzak}]
2006-06-01 23:38:00,703 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] - com.digitalmountain.domain.FileItem.metadata is dirty


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 12:58 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
If you don't want the behaviour of the default equals() method, define your own in the ReviewableItem class to override the Object.equals() method. You can have it ignore the metadata and only look at the primary key.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 2:05 pm 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:14 am
Posts: 30
hibernate_user wrote:
If you don't want the behaviour of the default equals() method, define your own in the ReviewableItem class to override the Object.equals() method. You can have it ignore the metadata and only look at the primary key.


That won't solve my problem, because I'm not trying to find out whether two ReviewableItem instances are equal -- I'm (or rather, Hibernate is) trying to find out whether a value is dirty. In this case, Hibernate is calling something like, "reviewableItemX.getMetadata().equals(reviewableItemY.getMetadata())". Overriding ReviewableItem.equals will have no effect.

Also, as it happens, I actually do have ReviewableItem.equals overridden to compare their primary keys...


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