Thanks for the response.
Okay, more of the code follows, which builds on the classes above.
The class that manages Principals is as follows:
Code:
public class PrincipalManager {
private PrincipalDao principalDao;
private AuthorityDao authorityDao;
public Principal createPrincipal(String accountIdentifier, String password, String[] authorityNames) {
Principal principal = constructPrincipal(accountIdentifier, password, authorityNames);
principalDao.create(principal);
return principal;
}
private Principal constructPrincipal(String accountIdentifier, String password, String[] authorityNames) {
Principal principal = new Principal(accountIdentifier, password);
if (authorityNames.length > 0) {
for (String authName : authorityNames) {
List<Authority> matchingAuthorities = authorityDao.findByName(authName);
Authority matchingAuthority = (matchingAuthorities.size() > 0) ? matchingAuthorities.get(0) : null;
if (null != matchingAuthority) {
principal.addAuthority(matchingAuthority);
}
}
}
return principal;
}
public Set<Authority> addAuthorities(String[] authorityNames) {
Set<Authority> createdAuthorities = new HashSet<Authority>();
for (String authname : authorityNames) {
createdAuthorities.add(addAuthority(authname));
}
return createdAuthorities;
}
public Authority addAuthority(String name) {
Authority dba = new Authority(ROLE_PREFIX + name.toUpperCase());
authorityDao.create(dba);
return dba;
}
}
The code snippet used to create the authorities and the principal is as follows:
Code:
// create set of available roles
String[] authorities = { "SITE.USER", "SITE.ADMIN" };
principalManager.addAuthorities(authorities);
// create principal with a single role assigned
String[] authoritiestToGrant = { "SITE.USER" };
Principal principal = principalManager.createPrincipal("someone@somewhere.com", "pass1234", authoritiestToGrant);
And the code snippet used to create entities (within each DAO class) is as follows:
Code:
boolean allowCreate = true;
SessionFactoryUtils.getSession(sessionFactory, allowCreate).save(object);
I think the problem is somehow related to the fact that each Principal contains a set of authorities, but I create the 'master list' of authorities before creating the principal.
As you can see when the Principal is constructed, I search for a matching role and assign it to the Principal.
After this code has executed, the join table principal_authorities remains empty.