Dear all,
I don't understand the merge use. I've a USER table with firstname, lastname, email and password entities. So I want to create different JSF pages to modify separately password and the other data.
But I cannot, because when I try to submit the JSF form with user data modification I receive the error:
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseExcepti on
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Column 'PASSWORD' cannot accept a NULL value.
Error Code: -1
Call: UPDATE LOCALUSER SET LASTNAME = ?, PASSWORD = ? WHERE (EMAIL = ?)
bind => [3 parameters bound]
This is the JSF:
<h:form id="formID"> <h:panelGrid columns="3"> <rich:validator > <h:outputText value="Firstname: " /> <h:inputText id="firstname" value="#{localUserEJB.user.firstName}" label="click to enter your firstname" required="true" requiredMessage="Firstname is required" > <f:validateLength minimum="3" /> </h:inputText> <rich:message for="firstname" ajaxRendered="true" /> <h:outputText value="Lastname " /> <h:inputText id="lastname" value="#{localUserEJB.user.lastName}" label="click to enter your lastname" required="true" requiredMessage="Lastname is required" > <f:validateLength minimum="3" /> </h:inputText> <rich:message for="lastname" ajaxRendered="true" /> <h:outputText value="Email " /> <h:inputText id="email" value="#{localUserEJB.user.email}" required="true" readonly="true" /> <h:outputText value=" " /> <h:outputText value="Group " /> <h:inputText id="group" value="#{localUserEJB.group}" required="true" readonly="true" /> <h:outputText value=" " /> <rich:message for="confirm" ajaxRendered="true" /> <h:outputLabel for="group" value="Group" rendered="#{request.isUserInRole('SUPERADMINISTRATOR')||request.isUserInRole('ADMINISTRATOR')}" /> <h:selectOneMenu id="SgroupName" value="#{localUserEJB.group}" required="true" requiredMessage="Please select a group." rendered="#{request.isUserInRole('SUPERADMINISTRATOR')}"> <f:selectItem itemValue="#{null}" itemLabel="-- select one --" /> <f:selectItem itemValue="SUPERADMINISTRATOR" itemLabel="SUPERADMINISTRATOR" /> <f:selectItem itemValue="ADMINISTRATOR" itemLabel="ADMINISTRATOR" /> <f:selectItem itemValue="UPLOADUSER" itemLabel="UPLOADUSER" /> <f:selectItem itemValue="USER" itemLabel="USER" /> </h:selectOneMenu> <h:selectOneMenu id="AgroupName" value="#{localUserEJB.group}" required="true" requiredMessage="Please select a group." rendered="#{request.isUserInRole('ADMINISTRATOR')}"> <f:selectItem itemValue="#{null}" itemLabel="-- select one --" /> <f:selectItem itemValue="ADMINISTRATOR" itemLabel="ADMINISTRATOR" /> <f:selectItem itemValue="UPLOADUSER" itemLabel="UPLOADUSER" /> <f:selectItem itemValue="USER" itemLabel="USER" /> </h:selectOneMenu> </rich:validator> <rich:notifyMessages stayTime="2000" nonblocking="true" /> </h:panelGrid> <h:inputHidden value="#{localUserEJB.user.email}" /> <h:inputHidden value="#{localUserEJB.group}" rendered="#{request.isUserInRole('UPLOADUSER')||request.isUserInRole('USER')}" /> <a4j:commandButton value="Modify" action="#{localUserEJB.writeModification()}"/> </h:form>
The EJB:
public void writeModification() { System.out.println(user + " " + principal + " " + group); //String password = user.getPassword(); oldData = localuserDAO.find(user.getEmail()); // if (!password.isEmpty()) { // password = DigestUtils.sha512Hex(password); // user.setPassword(password); // } else { // user.setPassword(oldData.getPassword()); // } if (!oldData.getFirstName().equals(user.getFirstName())) { user.setFirstName(user.getFirstName()); } if (!oldData.getLastName().equals(user.getLastName())) { user.setLastName(user.getLastName()); } List<Group> groups = new ArrayList<Group>(); groups.add(Group.valueOf(group)); user.setGroups(groups); localuserDAO.modify(user, principal); addMessage("User " + user.getLastName() + " updated succesfully"); }
And the DAO:
public void modify(LocalUser user, String principal) { System.out.println(principal); em.merge(user); LocalUser u = em.find(LocalUser.class, user.getEmail()); if (!u.getGroups().isEmpty()) { if (u.getGroups().get(0).toString().equals("UPLOADUSER") || u.getGroups().get(0).toString().equals("USER")) { setCreator(user.getEmail(), principal); } } }
Isn't possible to update only some entities?
Thanks
--- Update ---
For the moment I solved in this mode.
For the password:
public void writePasswordModification() { String password = user.getPassword(); localuserDAO.modifyPassword(user.getEmail(), password); addMessage("User " + user.getLastName() + " updated succesfully"); }public void modifyPassword(String email, String password) { LocalUser user; user = find(email); if (!password.isEmpty()) { password = DigestUtils.sha512Hex(password); user.setPassword(password); em.merge(user); } }
And for the other user data:
public void writeModification() { oldData = localuserDAO.find(user.getEmail()); user.setPassword(oldData.getPassword()); if (!oldData.getFirstName().equals(user.getFirstName())) { user.setFirstName(user.getFirstName()); } if (!oldData.getLastName().equals(user.getLastName())) { user.setLastName(user.getLastName()); } List<Group> groups = new ArrayList<Group>(); groups.add(Group.valueOf(group)); user.setGroups(groups); localuserDAO.modify(user, principal); addMessage("User " + user.getLastName() + " updated succesfully"); }
public void modify(LocalUser user, String principal) { em.merge(user); LocalUser u = em.find(LocalUser.class, user.getEmail()); if (!u.getGroups().isEmpty()) { if (u.getGroups().get(0).toString().equals("UPLOADUSER") || u.getGroups().get(0).toString().equals("USER")) { setCreator(user.getEmail(), principal); } } }
I don't know if its correct, but works.