Object persistence/rollback misconception
beligum Jan 2, 2007 4:13 AMHello everyone.
(Perhaps I should start with a little NY-wish, well, this be it)
I'm trying to wrap my thoughts around the entire ORM concept of JBoss (Hibernate+Seam in particular) and find it a little confusing, since my whole DB-background has been purely relational. I'd truly appreciate it if someone would take the time to explain it to me.
This is my case: I have a stateful session bean that creates a new mapped user-object (user data) and a new mapped loginEntity-object (login data) when someone tries to register on the register-page. I'm using the same bean as the ajax-callback bean to validate the entered data while registering.
When the user submits his/her data, the two entity beans are persisted to the DB, but sometimes, things go wrong with one of them, after the other one has been persisted successfully. I want to do a container-managed rollback on the successfully persisted bean, when the other one fails, using the @Rollback annotation, or any suitable other solution.
Please advise, here is the code:
@Stateful @Scope(CONVERSATION) @Name("register") public class RegisterAction implements IRegister { @In(create=true) @Out private Person currentUser; @In(create=true) @Out private LoginEntity loginEntity; private boolean renderSuccess = false; ResourceBundle msg = GeneralSettings.getGeneralMessageBundle(); ResourceBundle regMsg = GeneralSettings.getRegisterMessageBundle(); @In private EntityManager em; //-----CONSTRUCTORS----- public RegisterAction() { } //-----AJAX CALL ENTRIES----- /* * Note: if a call is added, removed, also remove it from the register() * checklist. */ public String getFirstNameCheck() { String curVal = currentUser.getFirstName(); String retVal = null; if (curVal!=null && curVal.equals("")) { retVal = regMsg.getString("missingFirstName"); } return retVal; } ... more ajax-callbacks ... //-----ACTIONS----- public boolean getRenderSuccess() { return this.renderSuccess; } @TransactionAttribute(TransactionAttributeType.REQUIRED) @Rollback(ifOutcome={"failure"}) public String register() { if (this.hasErrors()) { return "failure"; } //add the loginObject to the entity this.loginEntity.setEntity(this.currentUser); try { this.em.persist(this.currentUser); this.em.persist(this.loginEntity); this.renderSuccess = true; } catch (Exception e) { FacesMessages.instance().add("registerForm", regMsg.getString("registerPersistanceFailure")); return "failure"; } return "success"; } @Remove @Destroy public void destroy() { } //-----PRIVATE METHODS----- protected boolean hasErrors() { return !( getFirstNameCheck()==null && getLastNameCheck()==null && getAddressCheck()==null && getPostalCodeCheck()==null && getCityCheck()==null && getTelephoneCheck()==null && getEmailCheck()==null && getEmailCheck()==null && getPasswordCheck()==null ); } }