EJB and Injection
metal610 May 15, 2008 8:23 PMHi guys,
I have a few questions about injection. First off, yes, I know there is a section on this in the Seam reference PDF, and I did read it thoroughly. Second, I can get it to work, just not the way I want it to.
Ok so here's the problem: I have a JSF page that registers a user that is backed by a stateful backing bean. To keep with the methodology of tiered design, I did not place any logic or database access in that backing bean. There is a logic class that is associated with that bean and also a database class that goes with the logic class. Whenever I try and inject my EntityManager into the database class, it tells me that it is null, yet if I inject it into the backing bean and, through some icky looking code, pass the EntityManager back through the classes to the database class in order to use it, everything works just fine.
I have looked through the reference PDF and the seam examples thoroughly and I have everything set up the same way you guys do, except that all of your logic, database access, and getters and setters for the page are in the same class.
Is this just poor design on my part and I shouldn't be doing things like this or am I correct and something is just not working the way it should be?
Thanks a lot for any help,
Robert
Below are the classes I spoke of before:
RegisterBean:
@Stateful @Scope(SESSION) @Name("registrationBean") public class RegisterBean implements Register, Serializable{ private FacesMessages facesMessages; private String username; private String password; private String firstName, middleName, lastName; private String address, city, state; private int zip; private String phone; private String organizationName; private String email; private RegistrationLogic regLogic; public RegisterBean() { username = ""; password = ""; firstName = ""; middleName = ""; lastName = ""; address = ""; city = ""; state = ""; zip = 0; phone = ""; organizationName = ""; email = ""; regLogic = new RegistrationLogic(); facesMessages = FacesMessages.instance(); } public String submit() { regLogic.fill(username, password, firstName, middleName, lastName, address, city, state, zip, phone, organizationName, email); if(regLogic.fillEntities()){ facesMessages.add("Registration Successful",""); return "submitted"; } else{ facesMessages.add("Error Submitting Information",""); return "notSubmitted"; } } public String back() { return "back"; } public String restart() { return null; } @Destroy @Remove public void destroy() {} (getters and setters for the page members)
RegistrationLogic:
(RegistrationDataModel is just a holder for all of the information)
@Stateful @Scope(SESSION) @Name("registrationLogic") public class RegistrationLogic implements RegistrationLogicInterface, Serializable{ private FacesMessages facesMessages; private RegistrationDatabaseManager dbManager; private RegistrationDataModel regDataModel; private PersonT p; private AddressT a; private ElectronicAddressT phoneEA; private ElectronicAddressT emailEA; private UserT user; private PersonAddress pAddress; private PersonElectronicAddress pPhoneEA; private PersonElectronicAddress pEmailEA; private PersonOrganization pOrg; private PersonUser pUser; public RegistrationLogic() { dbManager = new RegistrationDatabaseManager(); p = new PersonT(); a = new AddressT(); phoneEA = new ElectronicAddressT(); emailEA = new ElectronicAddressT(); user = new UserT(); pAddress = new PersonAddress(); pPhoneEA = new PersonElectronicAddress(); pEmailEA = new PersonElectronicAddress(); pOrg = new PersonOrganization(); pUser = new PersonUser(); facesMessages = FacesMessages.instance(); regDataModel = new RegistrationDataModel(); } public void fill(String username, String password, String firstName, String middleName, String lastName, String address, String city, String state, int zip, String phone, String organizationName, String email) { regDataModel.fillFields(username, password, firstName, middleName, lastName, address, city, state, zip, phone, organizationName, email); } public boolean fillEntities() { // error checking for all of the values if (regDataModel.getUsername().equals("")) { facesMessages.add("Please enter a username", ""); return false; } if (regDataModel.getPassword().equals("")) { facesMessages.add("Please enter a password", ""); return false; } if (regDataModel.getFirstName().equals("")) { facesMessages.add("Please enter a first name", ""); return false; } if (regDataModel.getLastName().equals("")) { facesMessages.add("Please enter a last name", ""); return false; } if (regDataModel.getAddress().equals("")) { facesMessages.add("Please enter a street address", ""); return false; } if (regDataModel.getCity().equals("")) { facesMessages.add("Please enter a city", ""); return false; } if (regDataModel.getState().equals("")) { facesMessages.add("Please enter a state", ""); return false; } if (regDataModel.getZip() == 0) { facesMessages.add("Please enter a zip code", ""); return false; } if (!regDataModel.getPhone().equals("")) { String regex = "(\\d-)?(\\d{3}-)?\\d{3}-\\d{4}"; Pattern pattern = Pattern.compile(regex); Matcher m = pattern.matcher(regDataModel.getPhone()); if (!m.matches()) { facesMessages.add("Phone number is in the wrong format. The correct format is 555-555-5555", ""); return false; } } else { facesMessages.add("Please enter a phone number", ""); return false; } if (!regDataModel.getEmail().equals("")) { String regex = ".+@.+\\..+"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(regDataModel.getEmail()); if (!matcher.matches()) { facesMessages.add("Email address is in the wrong format", ""); return false; } } else { facesMessages.add("Please enter an email address", ""); return false; } try { // fill in the person object p.setId(UUID.randomUUID().toString()); p.setFname(regDataModel.getFirstName()); p.setMname(regDataModel.getMiddleName()); p.setLname(regDataModel.getLastName()); // fill in the address object a.setId(UUID.randomUUID().toString()); a.setAddress(regDataModel.getAddress()); a.setCity(regDataModel.getCity()); a.setState(regDataModel.getState()); a.setZip(regDataModel.getZip()); // fill in the user object user.setId(UUID.randomUUID().toString()); user.setUsername(regDataModel.getUsername()); user.setPassword(regDataModel.getPassword()); // fill in the phone electronic address object phoneEA.setId(UUID.randomUUID().toString()); phoneEA.setPhone(regDataModel.getPhone()); // fill in the email electronic address object emailEA.setId(UUID.randomUUID().toString()); emailEA.setEmail(regDataModel.getEmail()); // get the id of the organization from the name HashMap peopleOrganizationUUID = dbManager.getPersonOrganizationUUID(regDataModel.getOrganizationName()); ////////////////////////////////////////// // fill out the association entities ////////////////////////////////////////// // fill in the person address object pAddress.setId(new PersonAddressId(p.getId(), a.getId())); pAddress.setPersonT(p); pAddress.setAddressT(a); // fill in the person phone electronic address object pPhoneEA.setId(new PersonElectronicAddressId(p.getId(), phoneEA.getId(), 1)); pPhoneEA.setPersonT(p); pPhoneEA.setElectronicAddressT(phoneEA); // fill in the person email electronic address object pEmailEA.setId(new PersonElectronicAddressId(p.getId(), emailEA.getId(), 2)); pEmailEA.setPersonT(p); pEmailEA.setElectronicAddressT(emailEA); // get the organization's id from the map String id = (String) peopleOrganizationUUID.get("orgID"); // fill in the person organization object pOrg.setId(new PersonOrganizationId(p.getId(), id)); pOrg.setOrganizationT(dbManager.getOrganizationById(id)); // fill in the person user object pUser.setId(new PersonUserId(p.getId(), user.getId())); pUser.setPersonT(p); pUser.setUserT(user); // take all of the objects and condense them into a map so they can be easily passed to the database manager HashMap map = regDataModel.condenseEntities(p, a, phoneEA, emailEA, user, pAddress, pPhoneEA, pEmailEA, pOrg, pUser); dbManager.persist(map); return true; } catch (Exception exception) { facesMessages.add("An error has occured while inserting the information into our system. Please check your information for accuracy and try again.", ""); exception.printStackTrace(); } return false; } @Destroy @Remove public void destroy() {} }
RegistrationDatabaseManager:
@Stateful @Scope(SESSION) @Name("registrationDatabaseManager") public class RegistrationDatabaseManager implements Serializable, RegistrationDatabaseManagerInterface { @In EntityManager entityManager; /** * Queries the database for an OrganizationT object by it's UUID. * * @param id * @return An organization object from the database * @see OrganizationT */ public OrganizationT getOrganizationById(String id) { OrganizationT o = new OrganizationT(); //o = entityManager.find(OrganizationT.class, id); return o; } /** * Queries the database for an OrganizationT object by it's name. * * @param organizationName * @return * * @see OrganizationT */ public HashMap getPersonOrganizationUUID(String organizationName) { String id = ""; HashMap map = new HashMap(); id = (String) entityManager.createQuery("select id from OrganizationT o where o.name = :name").setParameter("name", organizationName).getSingleResult(); map.put("orgName", organizationName); map.put("orgID", id); return map; } /** * This takes the map of entities and persists them into the database. * @param map */ public void persist(HashMap map) { System.out.println("About to persist to the database"); entityManager.persist(map.get("PersonT")); entityManager.persist(map.get("AddressT")); entityManager.persist(map.get("PhoneElectronicAddress")); entityManager.persist(map.get("EmailElectronicAddress")); entityManager.persist(map.get("User")); entityManager.persist(map.get("PersonAddress")); entityManager.persist(map.get("PersonPhoneElectronicAddress")); entityManager.persist(map.get("PersonPhoneElectronicAddress")); entityManager.persist(map.get("PersonOrganization")); entityManager.persist(map.get("PersonUser")); System.out.println("All done!!"); } @Destroy @Remove public void destroy() {}