michal9225
michal9225

Reputation: 35

Spring - Can't update or insert any value into MySQL using JPA

I have a problem with employee record creation in my web app (Using spring, hibernate, thymeleaf and mySQL db). Hibernate insert returns:

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

Could you please take a look at this? Maybe someone know what could be wrong here. The same thing happens when i trying to update a record via form.

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

For all attributes from db insert?

My Employee entity class:

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

And add employee methods:

EmployeeController class:

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 class:

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

And my post form mapping in 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>

Any help will be appreciated! Thanks in advance!

Upvotes: 0

Views: 1233

Answers (1)

Defaulter
Defaulter

Reputation: 368

If your are sure that controller is receiving object with not null values, can you try converting all setter on entity class with proper case format i.e.

public void SetAccountStatus(String accountStatus)

to

public void setAccountStatus(String accountStatus)

Upvotes: 2

Related Questions