I got this error when I tried to submit my Create New employee Form:
Error:
Hibernate: insert into employee (name, password) values (?, ?)
2021-03-31 17:04:41.395 WARN 1848 --- [nio-8080-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1364, SQLState: HY000
2021-03-31 17:04:41.395 ERROR 1848 --- [nio-8080-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper : Field 'useremail' doesn't have a default value
2021-03-31 17:04:41.410 ERROR 1848 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement] with root cause
java.sql.SQLException: Field 'useremail' doesn't have a default value
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.23.jar:8.0.23]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.23.jar:8.0.23]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) ~[mysql-connector-java-8.0.23.jar:8.0.23]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092) ~[mysql-connector-java-8.0.23.jar:8.0.23]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040) ~[mysql-connector-java-8.0.23.jar:8.0.23]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1347) ~[mysql-connector-java-8.0.23.jar:8.0.23]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025) ~[mysql-connector-java-8.0.23.jar:8.0.23]
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.4.5.jar:na]
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197) ~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57) ~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:43) ~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3195) ~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3801) ~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:84) ~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]....
Could someone help me understand that why am I getting this error?
Below is my Code:
- Controller: below is the controller for employee
@Controller
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@RequestMapping("/")
public String viewHomePage(Model model) {
List<Employee> listEmployees = employeeService.listAll();
model.addAttribute("listEmployees" , listEmployees);
return "index";
}
@RequestMapping("/new")
public String showNewProductForm(Model model) {
Employee employee = new Employee();
model.addAttribute("employee" , employee);
return "views/new_employee";
}
@RequestMapping(value ="/save" , method = RequestMethod.POST)
public String saveEmployee(@ModelAttribute("employee") Employee employee) {
System.out.print(employee);
employeeService.save(employee);
return "redirect:/";
}
}
- Model: This part is the Employee class part
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(unique = true)
private String useremail;
private String name;
private String password;
public Employee(String useremail, String name, String password) {
this.useremail = useremail;
this.name = name;
this.password = password;
}
public Employee() {
}
public String getUseremail() {
return useremail;
}
public void setUseremail(String useremail) {
this.useremail = useremail;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
- Repository :
This part is the Repository part
public interface EmployeeRepository extends JpaRepository<Employee, String>{
}
- Service : this part is the service part
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
public List<Employee> listAll() {
return employeeRepository.findAll();
}
public void save(Employee employee) {
employeeRepository.save(employee);
}
public Employee get(String useremail) {
return employeeRepository.findById(useremail).get();
}
public void delete(String useremail) {
employeeRepository.deleteById(useremail);
}
}
- index.html : This HTML part is use to show list of employee
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8"/>
<title>Employee Management</title>
</head>
<body>
<div align="center">
<h1>Product List</h1>
<a href="/new">Create New Employee</a>
<br/><br/>
<table border="1" cellpadding="10">
<thead>
<tr>
<th>Email</th>
<th>Name</th>
<th>Password</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr th:each="employee : ${listEmployees}">
<td th:text="${employee.useremail}">Email</td>
<td th:text="${employee.name}">Name</td>
<td th:text="${employee.password}">Password</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
- new_employee.html: this part is my HTML for creating a new employee
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>Create New Employee</title>
</head>
<body>
<div align="center">
<h1>Create New Employee</h1>
<br />
<form action="#" th:action="@{/save}" th:object="${employee}"
method="post">
<table border="0" cellpadding="10">
<tr>
<td>Email:</td>
<td><input type="text" th:field="*{useremail}" /></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" th:field="*{name}" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="text" th:field="*{password}" /></td>
</tr>
<tr>
<td colspan="2"><button type="submit">Save</button> </td>
</tr>
</table>
</form>
</div>
</body>
</html>
DATABASE IMAGE: