Hazelcast Complex Object Model in Cache

483 views
Skip to first unread message

Robert Simmons

unread,
Sep 12, 2014, 1:45:04 PM9/12/14
to haze...@googlegroups.com
I am looking to put a complex model into Hazelcast to use it as the data tier of an application with MapStore implementations rendering the actual objects to the database. So for example, lets say we have the following noxiously common model where I have stripped out getters and setters for brevity:

class Customer {
 
public int id;
 
public String name;
 
public Address address;
}


class Address {
 
public int id;
 
public String street;
 
public string city;
 
public String state;
 
public String zip;
}


class InterestGroup {
 
public int id;
 
public String name;
 
public List<Customer> customers;
}

This is a model that I want to store in the database but I also want to map into Hazelcast. Furthermore lets say that I want customers to share addresses such that if the address changes for one, it will change for all customers with that address. I understand the concepts of data denormalization and sometimes you want to copy data but just for the sake of the question assume that isn't desirable in this case.

I can write MapStore classes to read this information out of the database and even give each object a primary key to use as a map key. What I am having trouble with is setting up navigation within the map between entities. Lets say I obtain a customer and want to navigate to the address of that customer and then get all customers that use that address. I could, of course, give each address an ID, store the ID in the customer and have a method go to hazel cast to get the address if needed.

If I load customers and addresses into a map, I dont want to embed all customers in an address nor do I want to embed the address in each customer. I want to navigate transparrently from the customer to the address. Is there a means by which I could do this in hazelcast without breaking the dynamics of a nested object but while allowing addresses to live in another map? The situation is similar for interest groups. If I embed all customers in an interest group then I am duplicating data all over especially if the customer is in several interest groups.

Additionally if the Customers are in one map and the Addresses are in another map there are times where I might want to do a query across both maps.

To accomplish this without duplication all over do I have to compromise the object structure of my entities?

Thanks in advance.

and...@hazelcast.com

unread,
Sep 15, 2014, 10:38:20 AM9/15/14
to haze...@googlegroups.com
Hi Robert,

When you put a nested object into an IMap, it will get serialized so multiple Customers can't really share the same instance of Address object.
I think your approach with storing every entity in a separate map is more suitable. Each Address has a key (id) and that key is is referenced in the Customer object.

- To get a Customer's address you would lookup the Address by key in addresses map.
- To get all Customers for an address you can filter the customers IMap with a Predicate: customers.entrySet( Predicates.equal("addressId", addressId)

If you are performing this type of navigation frequently then it might be worth adding utility functions:
class Customer {
  int addressId;
  Address getAddress(HazelcastInstance hz) { 
    return  hz.getMap("addresses").get(addressId); 
  }
}

Additionally you could make Customer implement the HazelcastInstanceAware interface. In that case HazelcastInstance will be injected into Customer and you will be able to have an even simpler utility method: Address getAddress() 

In general it sounds like you are after some form of ORM but that's not provided in Hazelcast out of the box.

Best,
Andrejs
Reply all
Reply to author
Forward
0 new messages