Apex has a class called Schema. It contains the entire schema of your organization. Objects, their fields, field type etc. can all be retrieved using this class.
To retrieve the list of objects, one needs to create a list of Entity :1. Create an empty list called the results list.2. Loop through the items in the Entity B list.3. For each item in the list, retrieve Entity A from the associated table.4. Add the results of the retrieve to the results list. Also if you are looking for an option to handle errors in Salesforce, do check this blog