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

时间:2018-10-30 17:32:42

标签: spring hibernate jsp insert

我在Web应用程序(使用spring,hibernate,thymeleaf和mySQL db)中创建员工记录时遇到问题。休眠插入返回:

cin >> a;

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

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

对于数据库插入中的所有属性?

我的 Employee 实体类:

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

并添加员工方法:

EmployeeController 类:

@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;
}

EmployeeServiceImpl 类:

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");
 }
}

以及我在JSP中的发布表单映射

@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;
 }

任何帮助将不胜感激! 预先感谢!

1 个答案:

答案 0 :(得分:2)

如果您确定控制器正在接收不为null值的对象,您可以尝试使用正确的大小写格式(例如,

)转换实体类上的所有设置器吗?

public void SetAccountStatus(String accountStatus)

public void setAccountStatus(String accountStatus)