-2

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: Database Image

talex
  • 17,973
  • 3
  • 29
  • 66
  • Does that field have a default value in SQL? Maybe it's the same problem as in [this question](https://stackoverflow.com/questions/15438840/mysql-error-1364-field-doesnt-have-a-default-values)... Would be good to see the SQL here. – deHaar Mar 31 '21 at 10:58
  • hi thank you for your respond, the field (useremail) doesnt have a default value because it's a primary key – Martin Hazard Mar 31 '21 at 11:02
  • 1
    "Could someone help me understand that why am I getting this error?" Yes. It happens because "Field 'useremail' doesn't have a default value". You need to supply it in your `insert` statement. – talex Mar 31 '21 at 11:03
  • For the case of a primary key, you maybe have to weaken some configurational restrictions (remove the requirement *default value* from this table or disable it generally). See [this post](https://stackoverflow.com/questions/25865104/field-id-doesnt-have-a-default-value) – deHaar Mar 31 '21 at 11:05
  • Show your DDL. It looks like hibernate expect DB will fill primary key, but DB do not do it. – talex Mar 31 '21 at 11:07
  • here https://i.stack.imgur.com/HqLJY.png @talex – Martin Hazard Mar 31 '21 at 11:15
  • I have tried it but still got the same error result @talex – Martin Hazard Mar 31 '21 at 11:19
  • Why is `useremail` annotated with `@GeneratedValue(strategy = GenerationType.IDENTITY)`? It is not a generated column, and definitely not generated as an identity). BTW: Using something like an email address as a primary key is a bad idea. Make it a unique constraint and use an artificial key. – Mark Rotteveel Mar 31 '21 at 11:57
  • it works!, after I removed the @GeneratedValue() :') , I didn't expect that this is the root of the error – Martin Hazard Mar 31 '21 at 12:31

1 Answers1

0

It's look like the changes made to the model may not impact the database, try recreating the database (or at least creating a new one) and scaffolding.

gantoin
  • 158
  • 1
  • 8