【问题标题】:java.sql.SQLException: Field 'useremail' doesn't have a default value [duplicate]java.sql.SQLException:字段'useremail'没有默认值[重复]
【发布时间】:2021-06-27 09:08:45
【问题描述】:

我在尝试提交创建新员工表单时遇到此错误:

错误:


    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]....

有人可以帮我理解为什么会出现这个错误吗?

以下是我的代码:

  • 控制器: 以下是员工的控制人
 

       @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:/";
                
            }
            
        }

  • 型号: 这部分是Employee类部分
 

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

这部分是Repository部分

        public interface EmployeeRepository extends JpaRepository<Employee, String>{
    
    }
  • 服务: 这部分是服务部分

    @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: 此 HTML 部分用于显示员工列表

    <!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: 这部分是我创建新员工的 HTML

     <!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>

数据库图像:

【问题讨论】:

  • 该字段在 SQL 中是否有默认值?也许这和this question 中的问题相同...如果在这里看到 SQL 就好了。
  • 您好,感谢您的回复,该字段(useremail)没有默认值,因为它是主键
  • "有人能帮我理解为什么会出现这个错误吗?"是的。发生这种情况是因为“字段'useremail'没有默认值”。您需要在 insert 声明中提供它。
  • 对于主键的情况,您可能需要削弱一些配置限制(从该表中删除要求默认值或一般禁用它)。见this post
  • 显示您的 DDL。看起来休眠期望数据库将填充主键,但数据库不这样做。

标签: java mysql sql hibernate


【解决方案1】:

看起来对模型所做的更改可能不会影响数据库,请尝试重新创建数据库(或至少创建一个新数据库)并搭建脚手架。

【讨论】:

    猜你喜欢
    • 2015-07-07
    • 2017-11-15
    • 2014-12-07
    • 2015-07-19
    • 2019-02-09
    • 2020-09-30
    • 1970-01-01
    • 2023-01-03
    • 2013-11-11
    相关资源
    最近更新 更多