When I try to test this by running my application, navigating to the admin.html page. I fill out a value for a first name, last name and email in the form. On click of the submit button, an error appears for 'Column email cannot be null'. I have excluded code such as getters,setters, contructors etc. for brevity. This is my admin.html page where I have a form which I use to post values to my api where the values are used to create an employee object
<form role="form" action="api/employees/create" method="post">
<div class="form-group">
<label for="firstName">First Name</label>
<input type="text" class="form-control" id="firstName" placeholder="Enter first name">
</div>
<div class="form-group">
<label for="lastName">Last Name</label>
<input type="text" class="form-control" id="lastName" placeholder="Enter last name">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" id="email" placeholder="Enter email">
</div>
<button type="submit" class="btn btn-success btn-block">Create</button>
</form>
This is my POST method in EmployeeAPI.java class where I handle the post and create an object with the values passed in from the form and try to persist this new object
@POST
@Path("create")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_HTML)
public Response createEmployee(@FormParam(value = "firstName") String firstName,
@FormParam(value = "lastName") String lastName,
@FormParam(value = "email") String email) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session session = factory.getCurrentSession();
URI location;
try{
session.getTransaction().begin();
Employee newEmployee = new Employee();
newEmployee.setFirstName(firstName);
newEmployee.setLastName(lastName);
newEmployee.setEmail(email);
session.persist(newEmployee);
session.getTransaction().commit();
session.close();
location = new URI("http://localhost:8080/index.html");
return Response.temporaryRedirect(location).build();
} catch (Exception e) {
session.getTransaction().rollback();
e.printStackTrace();
}
return null;
}
This is my Employee.java model class - I have a constructor for an employee with just firstName,lastName and email and one for all values.
@XmlRootElement
@Entity
public class Employee {
@Id
@GeneratedValue
private int id;
@Expose
@Column(nullable = false)
private String firstName;
@Expose
@Column(nullable = false)
private String lastName;
@Expose
@Column(nullable = false, unique = true)
private String email;
This is the error I am seeing on my server side
Hibernate: insert into Employee (availability_id, email, firstName, isAdmin, isManager, isMentee, isMentor, lastName, mentorDuration, topic_name, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2019-03-04 16:07:41 WARN SqlExceptionHelper:129 - SQL Error: 1048, SQLState: 23000
2019-03-04 16:07:41 ERROR SqlExceptionHelper:131 - Column 'email' cannot be null
2019-03-04 16:07:41 INFO AbstractBatchImpl:193 - HHH000010: On release of batch it still contained JDBC statements
2019-03-04 16:07:41 ERROR ExceptionMapperStandardImpl:39 - HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]