当我从 html 表单传递值时,为什么我在 post api 期间收到 'Column cannot be null' 错误
Why am I getting an error for 'Column cannot be null' during my post api when I am passing a value from a html form
当我尝试通过 运行 我的应用程序对此进行测试时,导航到 admin.html 页面。我在表单中填写了名字、姓氏和电子邮件的值。
单击提交按钮时,出现 'Column email cannot be null' 的错误。
为了简洁起见,我排除了诸如 getter、setter、contructor 等代码。
这是我的 admin.html 页面,我在其中有一个表单,我使用该表单将 post 值转换为 api,其中这些值用于创建员工对象
<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>
这是我在 EmployeeAPI.java class 中的 POST 方法,我在其中处理 post 并使用从表单传入的值创建一个对象并尝试保留这个新对象
@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;
}
这是我的 Employee.java 模型 class - 我有一个只包含 firstName、lastName 和 email 的员工构造函数,以及一个用于所有值的构造函数。
@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;
这是我在服务器端看到的错误
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]
在调试 EmployeeAPI 期间,前端的值变为 null
当我尝试通过 运行 我的应用程序对此进行测试时,导航到 admin.html 页面。我在表单中填写了名字、姓氏和电子邮件的值。 单击提交按钮时,出现 'Column email cannot be null' 的错误。 为了简洁起见,我排除了诸如 getter、setter、contructor 等代码。 这是我的 admin.html 页面,我在其中有一个表单,我使用该表单将 post 值转换为 api,其中这些值用于创建员工对象
<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>
这是我在 EmployeeAPI.java class 中的 POST 方法,我在其中处理 post 并使用从表单传入的值创建一个对象并尝试保留这个新对象
@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;
}
这是我的 Employee.java 模型 class - 我有一个只包含 firstName、lastName 和 email 的员工构造函数,以及一个用于所有值的构造函数。
@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;
这是我在服务器端看到的错误
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]
在调试 EmployeeAPI 期间,前端的值变为 null