ignite-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Denis Magda <dma...@apache.org>
Subject Re: Using object as key with BinaryMarshaller
Date Tue, 10 Jan 2017 20:53:18 GMT
Hi Alisher,

You need to fields identity resolver if want to specify a set of fields to be used for hashCode
and equals. 
http://apacheignite.gridgain.org/docs/binary-marshaller#section-binary-field-identity-resolver
<http://apacheignite.gridgain.org/docs/binary-marshaller#section-binary-field-identity-resolver>

—
Denis

> On Jan 10, 2017, at 5:30 AM, Alisher Alimov <alimovalisher@gmail.com> wrote:
> 
> Hello!
> 
> I have a little problem when try to use object as key with BinaryMarshaller. Bug occurred
only with BinaryMarshaller, on JdkMarshaller all is working fine as expected.
> 
> Here is example:
> 
> UserObject class used as key that override hashCode and equals methods but seems that
BinaryMarshaller using all fields from key object for building hashCode
> 
> private static class UserObject implements Serializable {
>     private final Integer id;
>     private final Integer notIdField;
> 
>     UserObject(Integer id, Integer notIdField) {
>         this.id = id;
>         this.notIdField = notIdField;
>     }
> 
>     public Integer getId() {
>         return id;
>     }
> 
>     public Integer getNotIdField() {
>         return notIdField;
>     }
> 
>     @Override
>     public int hashCode() {
>         return Objects.hash(id);
>     }
> 
>     @Override
>     public boolean equals(Object obj) {
>         if (obj instanceof UserObject) {
>             return Objects.equals(((UserObject) obj).getId(), getId());
>         }
> 
>         return false;
>     }
> }
> 
> 
> public void testObjectKey() throws Exception {
>     IgniteCache<UserObject, Integer> cache = ignite.getOrCreateCache("test");
> 
> 
>     for (int i = 0; i < 10; i++) {
>         cache.put(new UserObject(0, i), i);
> 
>         Assert.assertEquals(1, cache.size(CachePeekMode.PRIMARY));
>     }
> 
>  }
> 
> Is it expected behaviour or bug?
> 
> With best regards
> Alisher Alimov
> alimovalisher@gmail.com <mailto:alimovalisher@gmail.com>
> 
> 
> 
> 


Mime
View raw message