ahaha__123
feiyunzhanye
2019-03-28 17:04

IDEA整合ssh测试hibernate时报Field 'password' doesn't have a default value错误

5
  • spring
  • mysql
  • intellij-idea
  • hibernate

错误代码段:

Hibernate: insert into user (name, id) values (?, ?)

ERROR SqlExceptionHelper Field 'password' doesn't have a default value

ERROR ExceptionMapperStandardImpl HHH000346: Error during managed flush [org.hibernate.exception.GenericJDBCException: could not execute statement]

我数据库只有id 和name字段,id设置的自增,User实体和User.hbm.xml是根据数据库生成的。

User.java

package com.ideal.bean;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.Objects;

@Entity
public class User {
    private int id;
    private String name;

    @Id
    @Column(name = "id", nullable = false)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @Column(name = "name", nullable = true, length = 255)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

   @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return id == user.id &&
                Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }
}    

User.hbm.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="com.ideal.bean.User" table="user" schema="mc_learning">
        <id name="id">
            <column name="id" sql-type="int(11)"/>
        </id>
        <property name="name">
            <column name="name" sql-type="varchar(255)" not-null="true"/>
        </property>
    </class>
</hibernate-mapping>
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐

换一换