简体   繁体   English

当我从html表单传递值时,为什么在我的post api期间出现'Column not not 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

When I try to test this by running my application, navigating to the admin.html page. 当我尝试通过运行我的应用程序来测试它时,导航到admin.html页面。 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. 为了简洁起见,我已经排除了诸如getter,setter,contructors等代码。 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 这是我的admin.html页面,我在其中有一个表单,用于将值发布到我的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>

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 这是我在EmployeeAPI.java类中的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;
}

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. 这是我的Employee.java模型类 - 我有一个员工的构造函数,只有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;

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]

During debugging of the EmployeeAPI the values were coming in null from the front end 在调试EmployeeAPI期间,值从前端开始为空

调试期间后端的屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在点击这个 api 时得到一个 null 值? - Why am I getting a null value while hitting this api? 为什么我在alertDialog.setMessage()上获得空值? - Why am i getting a null value on my alertDialog.setMessage()? 将字符串的ArrayList从一个活动传递到另一个活动时,为什么会出现空指针异常? - Why am I getting a null pointer exception when passing an ArrayList of strings from one activity to another? 为什么我的Android代码中出现空指针错误? - Why am I getting a null pointer error in my Android Code? 为什么我将参数值设置为null? - Why am I getting parameter value as null? 为什么我为此线性布局获取空值 - Why am I getting a null value for this linearlayout 编译时,为什么在Java程序中出现“找不到符号”错误? - Why am I getting a “cannot find symbol” error in my java program when I compile? 为什么在运行应用程序时出现此错误? - Why am I getting this error when I run my app? 为什么我会收到“无法从 Stad 投射到 Edge”的错误消息? - Why am I getting this “Cannot cast from Stad to Edge” error? 为什么在尝试与我的服务器通信时出现此错误? - Why am i getting this error when trying to communicate with my server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM