-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: ManyToMany association and and join table
PostPosted: Fri Jan 12, 2007 11:20 am 
Newbie

Joined: Fri Jan 12, 2007 10:41 am
Posts: 4
Hello

I have an issue that I hope someone may be able to shed some light on.

I have a problem modelling the security concepts of principals and roles with a many-to-many association.

I am using Hibernate Core & Hibernate Annotations 3.2.1.GA on MySQL 5.0.

My entities are as follows:

Code:
@Entity()
@Table()
public class Authority {
   ...
   
   @Column(unique = true, nullable = false)
   private String name;

   ...
}

@Entity()
@Table()
public class Principal {
   ...
   
   @Column(unique = true, nullable = false)
   private String identity;

   private String password;

   @ManyToMany(
      cascade = {CascadeType.PERSIST, CascadeType.MERGE},
      fetch=FetchType.EAGER,
      targetEntity=Authority.class
   )
   private Set<Authority> authorities;
   
   ...
}


I create authorities first, then create principals later. When I create a principal, I find the exisiting authority I want to assign to the principal using a query, then add it to his authorities set.

But the join table, principal_authorities, remains empty.

I believe I am missing something simple here, but I just can't see it.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 12, 2007 8:22 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you need to show your code

_________________
Emmanuel


Top
 Profile  
 
 Post subject: RE: ManyToMany association and and join table
PostPosted: Tue Jan 16, 2007 12:55 pm 
Newbie

Joined: Fri Jan 12, 2007 10:41 am
Posts: 4
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.


Top
 Profile  
 
 Post subject: RE: ManyToMany association and and join table
PostPosted: Tue Jan 16, 2007 1:08 pm 
Newbie

Joined: Fri Jan 12, 2007 10:41 am
Posts: 4
The addAuthority method on Principal is simple and looks like this:

Code:
   
public void addAuthority(Authority authority) {
   this.authorities.add(authority);
}


Top
 Profile  
 
 Post subject: RE: ManyToMany association and and join table
PostPosted: Wed Jan 17, 2007 4:58 pm 
Newbie

Joined: Fri Jan 12, 2007 10:41 am
Posts: 4
This was down to transaction management error: case closed.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.