Seeking design advice on the following portion of my design:
Relevant portions of classes:
public class Person {
String firstName;
String lastName
Address address;
Company company;
}
public class Contact extends Person{}
public class User extends Contact{
String username;
}
public class PhoneNbr{}
public class Address{}
public class Company{
String companyName;
}
I want to L2 cache all defined User objects so they can be displayed in a list for selection by the current User. However, I do not want to cache Contact or Person objects in L2 cache. The list of User objects displays the "username" from User, "first & last name" from Person, company name (from Company referenced by Person), and city/state (from Address) referenced by Person.
I am aware that I cannot cache User without also caching Contact and Person. I am considering the following approaches and would like to solicit other opinions on how to proceed.
1. Don't change the object model. Add a new class, "UserListItem" that contains all of the data I want to display in the list items, username, first/last name, company name, city/state. If I do this, is there anyway to use HQL to obtain UserListItem objects to populate my lists through the L2 cache for UserListItem (but not use the L2 cache for User/Contact/Person)?
Doesn't seem so to me, as UserListItem objects contain the same primary key as their associated User/Contact/Person. My plan is to obtain the associated User object when a user selects a UserListItem from a list, and then perform the requested operation against the User object. However, I would think that this requires me to evict the UserListItem, only to have to reload it after finishing up with the User. I suspect that this will cause further problems, as my list of UserListItem objects will be incomplete during these periods. An alternative is to implement my own cache (which I already have, actually) and populate and refresh it periodically with UserListItem objects. I'm leaning in this direction, even though I have to use a special-purpose API to access this cache, rather than HQL.
2. Change the object model to make User extend Object, maintaining a reference to Contact in order to obtain the rest of the User list data. This would allow me to cache User (as it is no longer a subclass of Contact). However, in order to make this caching useful to me, I still need to access first/last name from Person, companyName from Company, and city/state from Address, both referenced by Person. How does the L2 cache behave in this case? I'd be caching User objects in L2, but then referencing non-L2 cached objects to get most of the useful per-User data elements. Is there any way to get the non-L2 cached objects to stay in the L2 cache when referenced by User objects in the L2 cache, or will these other objects (Contact, Person, Company, Address) get reloaded into the session cache for each session?
Thanks in advance for any help...btw, any other approaches are very welcome.
Rick
|