code: private void createMembership(Session session, MembershipImpl membership, boolean broadcast)
        throws InvalidNameException, Exception
     {
        Node userNode;
        try
        {
           userNode = utils.getUserNode(session, membership.getUserName());
        }
        catch (PathNotFoundException e)
        {
           throw new InvalidNameException("The user " + membership.getUserName() + " does not exist");
        }
  
        Node groupNode;
        try
        {
           groupNode = utils.getGroupNode(session, membership.getGroupId());
        }
        catch (PathNotFoundException e)
        {
           throw new InvalidNameException("The group " + membership.getGroupId() + " does not exist");
        }
  
        Node typeNode;
        String membershipType =
           membership.getMembershipType().equals(MembershipTypeHandler.ANY_MEMBERSHIP_TYPE)
              ? JCROrganizationServiceImpl.JOS_MEMBERSHIP_TYPE_ANY : membership.getMembershipType();
        try
        {
           typeNode = utils.getMembershipTypeNode(session, membershipType);
        }
        catch (PathNotFoundException e)
        {
           throw new InvalidNameException("The membership type " + membership.getMembershipType() + " does not exist");
        }
  
        Node membershipStorageNode = groupNode.getNode(JCROrganizationServiceImpl.JOS_MEMBERSHIP);
  
        Node refUserNode;
        try
        {
           refUserNode = membershipStorageNode.addNode(membership.getUserName());
           refUserNode.setProperty(MembershipProperties.JOS_USER, userNode);
        }
        catch (ItemExistsException e)
        {
           refUserNode = membershipStorageNode.getNode(membership.getUserName());
        }
  
        Node refTypeNode;
        try
        {
           refTypeNode = refUserNode.addNode(membershipType);
           refTypeNode.setProperty(MembershipProperties.JOS_MEMBERSHIP_TYPE, typeNode);
        }
        catch (ItemExistsException e)
        {
           // the membership already exists
           return;
        }
  
        String id = utils.composeMembershipId(groupNode, refUserNode, refTypeNode);
        membership.setId(id);
  
        if (broadcast)
        {
           preSave(membership, true);
        }
  
        session.save();
        putInCache(membership);
  
        if (broadcast)
        {
           postSave(membership, true);
        }
     }
docs: Persist new membership.