0

I enter all object values in JSP page and when I press submit button:

   <h:panelGrid columns="4">
                <h:outputText value="Employee name:"/>
                <h:inputText id="eName"
                             value="#{employeeBean.employee.name}" required="true"
                             requiredMessage="#{msgs.nameRequired}">
                </h:inputText>
                <h:message for="eName" style="color:red"/>
                <h:outputText value=" "/>                 

                <h:outputText value="Employee role:"/>
                <h:selectOneMenu value="#{employeeBean.role}">
                    <f:selectItems value="#{employeeBean.idNameRoleMap}"/>
                </h:selectOneMenu>

            </h:panelGrid>
            <h:form><h:commandButton value="Submit" action="#{employeeBean.saveEmployee}"/></h:form>
    </ui:define>

I get an exception:

 javax.servlet.ServletException: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Column 'NAME'  cannot accept a NULL value.
Error Code: 20000
Call: INSERT INTO EMPLOYEE (BIRTHDATE, EMAIL, HIREDATE, NAME, PASSWORD, READY, role_id) VALUES (?, ?, ?, ?, ?, ?, ?)
    bind => [7 parameters bound]
Query: InsertObjectQuery(Employee{id=0, name='null', email='null', password='null', birthDate=null, hireDate=null, ready=false, role=null})

All the fields passed as nulls as query shows. Bean method is:

public String saveEmployee() {
    if (employee == null) {
        employee = new Employee();
    }
    employee.setRole(idRoleMap.get(role));
    employeeService.save(employee);
    return "employeeList";
}

what's wrong with it?

DimaSan
  • 12,264
  • 11
  • 65
  • 75
  • 1
    Could you please post your submit form complete code? I bet you are setting wrong the properties of your Employee cause the root cause is very clear: name field can't be null – guilhermerama Nov 29 '15 at 18:55
  • 1
    You shouldn't have `if employee == null` in `saveEmployee` or to be more precise it should not be `null` if it is an object bound to JSF page by EL. If you have field `private Employee employee` in `EmployeeBean` then change it to `private Employee employee = new Employee()`. Exception clearly says, that values are `null` and you can see it in `Query: InsertObjectQuery`. Print them out before persisting or in `saveEmployee()` and find it out yourself. – Geinmachi Nov 29 '15 at 18:56
  • at first i didn't have such null-check then read somewhere here to try to do it. – DimaSan Nov 29 '15 at 19:04
  • I already have the private field `employee` and a constructor, that initializes it as you described. Why null values pass? – DimaSan Nov 29 '15 at 19:11
  • 1
    You should not use constructor, use method with `@PostConstruct` annotation. Your fields are not in `h:form` component, only the `h:commandButton`, change it. – Geinmachi Nov 29 '15 at 19:14
  • You saved my life! Thank you so much. – DimaSan Nov 29 '15 at 19:26

0 Answers0