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

错误代码段:

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个回答

数据库里给password字段加上默认值,或者允许为null
hibernate里也加上默认值

是数据库链接写成了别的数据库名

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问