-->
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.  [ 7 posts ] 
Author Message
 Post subject: CharArrayType always leads to dirty entity
PostPosted: Mon Apr 02, 2007 5:37 pm 
Newbie

Joined: Thu Apr 06, 2006 2:09 pm
Posts: 14
Using Hibernate 3.2.2

I have a class that has a char array mapped by the CharArrayType. I noticed bad performances when working with collections of object of that class. This was due to excessive updates performed by hibernate on those objects.

After some investigations I saw that hibernate was considering instances of this class dirty as soon as they were retrieved from database.

I made some debugging and I saw as expected that Hibernate compares the values of the objects disassembled when added in the first level cache with the value of the object at the actual state.
When the object snapshot is done at the time it is added in the first level cache, hibernate makes a deep copy (looks obvious) but at the time of the dirty checking hibernate compares thevalues with the equals method.
The problem is that when checking dirtiness of char arrays the equals method is the one from object and thus always returns false in this case.


I had to make a custom UserType for char arrays to get rid of this issue.

Is it normal that the isEqual method of the NullableType class is not redefined in the CharArrayType class to perform an array comparison?

Thanks in advance for your answers!


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 9:34 am 
Newbie

Joined: Thu Apr 06, 2006 2:09 pm
Posts: 14
No answer ... Should I report this to JIRA ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 9:43 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Take a unit test from the test suite that has a char array, adapt it to fail, post to JIRA please.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 12:02 pm 
Newbie

Joined: Thu Apr 06, 2006 2:09 pm
Posts: 14
Done. See http://opensource.atlassian.com/project ... e/HHH-2606


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 7:14 pm 
Newbie

Joined: Tue May 29, 2007 6:30 pm
Posts: 3
I got the a similar issue.

We have a column defined in Oracle as Object Type (its elements are Varray of Varchar2). It always states that the entity is dirty (for those records in the the table with not null values in the Object Type column) even if there is no change to the entities at all.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 30, 2007 6:50 pm 
Newbie

Joined: Tue May 29, 2007 6:30 pm
Posts: 3
Hi Mirk,

I think my issue is similar to yours. In my case, the Java object has String Arrays which maps to the table column of Oracle Object type. I tried to overwrite the equals() method in this class to make it a deep comparizon. However, it still complains the entity is dirty after loading. How did you fix your issue if this is really a Hibernate bug?

Thank you very much!

My email: jerryl@yesmail.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 7:04 am 
Newbie

Joined: Thu Apr 06, 2006 2:09 pm
Posts: 14
I made my own CharArrayType (see below) and put @Type(type="my.package.CharArrayType") on the getters of my arrays.

Code:
package my.package;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

/**
* Fixes the build-in Hibernate CharArray type.<br />
* See <a href="http://opensource.atlassian.com/projects/hibernate/browse/HHH-2606">Jira issue</a>
*/
public class CharArrayType implements UserType {

   private static final int[] SQL_TYPES = { Types.VARCHAR };

   public int[] sqlTypes() {
      return SQL_TYPES;
   }

   public Class returnedClass() {
      return char[].class;
   }

   public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
         throws HibernateException, SQLException {
      String value = (String) Hibernate.STRING.nullSafeGet(resultSet, names);
      return value != null ? value.toCharArray() : null;
   }

   public void nullSafeSet(PreparedStatement preparedStatement, Object value,
         int index) throws HibernateException, SQLException {
      Hibernate.STRING.nullSafeSet(preparedStatement,
            value != null ? new String((char[]) value) : null, index);
   }

   public Object deepCopy(Object value) throws HibernateException {
      if(value == null){
         return null;
      }
      char[] chars = (char[]) value;
      char[] copy = new char[chars.length];
      System.arraycopy(chars, 0, copy, 0, chars.length);
      return copy;
   }

   public boolean isMutable() {
      return true;
   }

   public Object assemble(Serializable cached, Object owner)
         throws HibernateException {
      return cached;
   }

   public Serializable disassemble(Object value) throws HibernateException {
      return (Serializable) value;
   }

   public Object replace(Object original, Object target, Object owner)
         throws HibernateException {
      return original;
   }

   public int hashCode(Object x) throws HibernateException {
      return x.hashCode();
   }

   public boolean equals(Object x, Object y) throws HibernateException {
      return Arrays.equals((char[]) x, (char[]) y);
   }
}


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