Proving that I am capable of overanalyzing myself into oblivion, I came up with the following (code abbreviated for brevity.. *lol*)
Code:
public class User
{
private Integer userId;
private Set<Role> roles = new HashSet<Role>();
...
...
public void addRole( Role role ) { roles.add( role ); }
public Role getRole( Class roleClass ) { ...; }
}
public class Role<T>
{
private T member;
public Role( T memberRole ) { member = memberRole; }
public T get() { return member; }
public boolean supportsRole( String roleName ) { ... }
public void addTo( User user ) { ... }
etc.
}
public class SampleRole1
{
private String someUniqueAttribute;
...
...
}
Under this "assignment of roles" philosophy, I might have a User who is a SampleRole1, a SampleRole2, etc. Each "role" is actually a wrapper for a regular POJO containing whatever role-specific data is needed.
For example:
Code:
SampleRole1 role1 = new SampleRole1();
SampleRole2 role2 = new SampleRole2();
User user = new User();
user.addRole( new Role<SampleRole1>( role1 ));
user.addRole( new Role<SampleRole2>( role2 ));
Later in code, I can a method in User to obtain the details of SampleRole2 by saying (method described is not shown in code, but you get the idea)
Code:
SampleRole2 role = user.getRole(SampleRole2.class);
But now I'm a bit stuck. How would I go about mapping this in Hibernate? I *think* in addition to the users table, I want to have a table for each role with a userId column. Ideally, I'd like to be able to add new roles without impacting anything other than the roles themselves.... but... I'm confused how I would map this since the types aren't known until runtime. How would Hibernate know which table to load from and save to?
If confusion were valuable, I'd be rich.
Dan