Spring - 无法使用JPA更新或插入任何值到MySQL

问题描述 投票:0回答:1

我的网络应用程序中创建员工记录存在问题(使用spring,hibernate,thymeleaf和mySQL db)。 Hibernate插入返回:

java.sql.SQLIntegrityConstraintViolationException: Column 'ACCOUNT_STATUS' 
cannot be null

你能看一下吗?也许有人知道这里可能有什么问题。当我尝试通过表单更新记录时,会发生同样的事情。

Hibernate: insert into temployee (account_status, account_type, birth_day, 
branch, city, department, email_address, first_name, full_name, home_nbr, is_manager, last_name, phone_number, position, position_desc, post_code, state,street, valid_from, valid_until) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2018-10-30 18:18:05.959  WARN 17792 --- [nio-8090-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1048, SQLState: 23000
2018-10-30 18:18:05.960 ERROR 17792 --- [nio-8090-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : Column 'ACCOUNT_STATUS' cannot be null

对于db insert的所有属性?

我的员工实体类:

@Entity
@Table(name="TEMPLOYEE")
public class Employee implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="EMPLOYEE_ID")
private int employeeId;  
@Column(name="FIRST_NAME")
private String firstName;
@Column(name="LAST_NAME")
private String lastName;
@Column(name="FULL_NAME")
private String fullName;
@Column(name="EMAIL_ADDRESS")   
private String emailAddress;
@Column(name="PHONE_NUMBER")    
private String phoneNumber;
@Column(name="ACCOUNT_TYPE")    
private String accountType;
@Column(name="ACCOUNT_STATUS")  
private String accountStatus;
@Column(name="VALID_FROM")  
private String validFrom;
@Column(name="VALID_UNTIL") 
private String validUntil;
@Column(name="IS_MANAGER")  
private String isManager;
@Column(name="STREET")  
private String street;
@Column(name="HOME_NBR")    
private String homeNbr;
@Column(name="CITY")    
private String city;
@Column(name="STATE")   
private String state;
@Column(name="POST_CODE")   
private String postCode;
@Column(name="BIRTH_DAY")   
private String birthDay;
@Column(name="BRANCH")  
private String branch;
@Column(name="DEPARTMENT")  
private String department;
@Column(name="POSITION")    
private String position;
@Column(name="POSITION_DESC")   
private String positionDesc;


public int getEmployeeId() {
    return employeeId;
}
public void SetEmployeeId(int employeeId) {
    this.employeeId = employeeId;
}
public String getFirstName() {
    return firstName;
}
public void SetFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void SetLastName(String lastName) {
    this.lastName = lastName;
}
public String getFullName() {
    return fullName;
}
public void SetFullName(String fullName) {
    this.fullName = fullName;
}
public String getEmailAddress() {
    return emailAddress;
}
public void SetEmailAddress(String emailAddress) {
    this.emailAddress = emailAddress;
}
public String getPhoneNumber() {
    return phoneNumber;
}
public void SetPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}
public String getAccountType() {
    return accountType;
}
public void SetAccountType(String accountType) {
    this.accountType = accountType;
}
public String getAccountStatus() {
    return accountStatus;
}
public void SetAccountStatus(String accountStatus) {
    this.accountStatus = accountStatus;
}
public String getValidFrom() {
    return validFrom;
}
public void SetValidFrom(String validFrom) {
    this.validFrom = validFrom;
}
public String getValidUntil() {
    return validUntil;
}
public void SetValidUntil(String validUntil) {
    this.validUntil = validUntil;
}
public String getIsManager() {
    return isManager;
}
public void SetIsManager(String isManager) {
    this.isManager = isManager;
}
public String getStreet() {
    return street;
}
public void SetStreet(String street) {
    this.street = street;
}
public String getHomeNbr() {
    return homeNbr;
}
public void SetHomeNbr(String homeNbr) {
    this.homeNbr = homeNbr;
}
public String getCity() {
    return city;
}
public void SetCity(String city) {
    this.city = city;
}
public String getState() {
    return state;
}
public void SetState(String state) {
    this.state = state;
}
public String getPostCode() {
    return postCode;
}
public void SetPostCode(String postCode) {
    this.postCode = postCode;
}
public String getBirthDay() {
    return birthDay;
}
public void SetBirthDay(String birthDay) {
    this.birthDay = birthDay;
}
public String getBranch() {
    return branch;
}
public void SetBranch(String branch) {
    this.branch = branch;
}
public String getDepartment() {
    return department;
}
public void SetDepartment(String department) {
    this.department = department;
}
public String getPosition() {
    return position;
}
public void SetPosition(String position) {
    this.position = position;
}
public String getPositionDesc() {
    return positionDesc;
}
public void SetPositionDesc(String positionDesc) {
    this.positionDesc = positionDesc;
}

并添加员工方法:

EmployeeController类:

package com.project.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.project.model.Employee;
import com.project.service.EmployeeService;

@Controller
@RequestMapping(value="/employee")
public class EmployeeController {

@Autowired
EmployeeService employeeService;

@RequestMapping(value="/list", method=RequestMethod.GET)
public ModelAndView list() {
ModelAndView model = new ModelAndView("employee_list");
List<Employee> employeeList = employeeService.getAllEmployees();
model.addObject("employeeList", employeeList);

return model;
}

@RequestMapping(value="/addEmployee/", method=RequestMethod.GET)
public ModelAndView addEmployee() {
ModelAndView model = new ModelAndView();

Employee employee = new Employee();
model.addObject("employeeForm", employee);
model.setViewName("employee_form");

return model;
}

@RequestMapping(value="/updateEmployee/{employeeId}", method=RequestMethod.GET)
public ModelAndView editArticle(@PathVariable int employeeId) {
ModelAndView model = new ModelAndView();

Employee employee = employeeService.getEmployeeById(employeeId);
model.addObject("employeeForm", employee);
model.setViewName("employee_form");

return model;
 }

  @RequestMapping(value="/saveEmployee", method=RequestMethod.POST)
  public ModelAndView save(@ModelAttribute("employeeForm") Employee employee) {
  employeeService.saveOrUpdate(employee);

  return new ModelAndView("redirect:/employee/list");
 }

 @RequestMapping(value="/deleteEmployee/{employeeId}", method=RequestMethod.GET)
 public ModelAndView delete(@PathVariable("employeeId") int employeeId) {
 employeeService.deleteEmployee(employeeId);

return new ModelAndView("redirect:/employee/list");
 }
}

EmployeeServiceImpl类:

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

 @Autowired
 EmployeeRepository employeeRepository;

 @Override
 public List<Employee> getAllEmployees() {
 return (List<Employee>) employeeRepository.findAll();
}

 @Override
 public Employee getEmployeeById(int employeeId) {
 return employeeRepository.findById(employeeId).get();
 }

 @Override
 public void saveOrUpdate(Employee employee) {
 employeeRepository.save(employee);
 }

 @Override
 public void deleteEmployee(int employeeId) {
 employeeRepository.deleteById(employeeId);
 }


 @Transactional
 public Employee updateEmployee(Employee employee) {
 entityManager.merge(employee);
 return employee;
 }

我在JSP中的帖子表单映射:

 <div class="container">
<spring:url value="/employee/saveEmployee" var="saveURL" />
<h2>Employee</h2>
<form:form modelAttribute="employeeForm" method="post" action="${saveURL }"     cssClass="form" >
<form:hidden path="employeeId"/>
<div class="form-group">
<label>First Name</label>
<form:input path="firstName" cssClass="form-control" id="firstName" />
</div>
<div class="form-group">
<label>Last Name</label>
<form:input path="lastName" cssClass="form-control" id="lastName" />
</div>
<div class="form-group">
<label>Full Name</label>
<form:input path="fullName" cssClass="form-control" id="fullName" />
</div>
<div class="form-group">
<label>Email Address</label>
<form:input path="emailAddress" cssClass="form-control" id="emailAddress" />
</div>
<div class="form-group">
<label>Phone Number</label>
<form:input path="phoneNumber" cssClass="form-control" id="phoneNumber" />
</div>
<div class="form-group">
<label>Account Type</label>
<form:input path="accountType" cssClass="form-control" id="accountType" />
</div>
<div class="form-group">
<label>Account Status</label>
<form:input path="accountStatus" cssClass="form-control" id="accountStatus"     />
</div>
<div class="form-group">
<label>Valid From</label>
<form:input path="validFrom" cssClass="form-control" id="validFrom" />
</div>
<div class="form-group">
<label>Valid Until</label>
<form:input path="validUntil" cssClass="form-control" id="validUntil" />
</div>
<div class="form-group">
<label>Is Manager?</label>
<form:input path="isManager" cssClass="form-control" id="isManager" />
</div>
<div class="form-group">
<label>Street</label>
<form:input path="street" cssClass="form-control" id="street" />
</div>
<div class="form-group">
<label>Home Number</label>
<form:input path="homeNbr" cssClass="form-control" id="homeNbr" />
</div>
<div class="form-group">
<label>City</label>
<form:input path="city" cssClass="form-control" id="city" />
</div>
<div class="form-group">
<label>State</label>
<form:input path="state" cssClass="form-control" id="state" />
</div>
<div class="form-group">
<label>Post Code</label>
<form:input path="postCode" cssClass="form-control" id="postCode" />
</div>
<div class="form-group">
<label>Birth Day</label>
<form:input path="birthDay" cssClass="form-control" id="birthDay" />
</div>
<div class="form-group">
<label>Branch</label>
<form:input path="branch" cssClass="form-control" id="branch" />
</div>
<div class="form-group">
<label>Department</label>
<form:input path="department" cssClass="form-control" id="department" />
</div>
<div class="form-group">
<label>Position</label>
<form:input path="position" cssClass="form-control" id="position" />
</div>
<div class="form-group">
<label>Position Desc</label>
<form:input path="positionDesc" cssClass="form-control" id="positionDesc" />
</div>
<button type="submit" class="btn btn-primary">Save</button>

任何帮助将不胜感激!提前致谢!

spring hibernate jsp insert
1个回答
2
投票

如果您确定控制器正在接收非空值的对象,您可以尝试使用正确的大小写格式转换实体类上的所有setter,即

public void SetAccountStatus(String accountStatus)

public void setAccountStatus(String accountStatus)

© www.soinside.com 2019 - 2024. All rights reserved.