我正在使用hibernate和spring数据来处理db。我收到了以下错误。我尝试在db中设置默认值,但似乎没有解决这个问题。我做错了什么:
错误:
Caused by: java.sql.SQLException: Field 'USERNAME' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2535)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1911)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2145)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2081)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2066)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
表查询:
CREATE TABLE `User` (
`USERNAME` varchar(60) NOT NULL,
`PASSWORD` varchar(45) NOT NULL,
`IS_ACTIVE` char(1) NOT NULL,
`NAME` varchar(100) NOT NULL,
`EMAIL` varchar(100) NOT NULL,
`PHONE` varchar(45) NOT NULL,
PRIMARY KEY (`USERNAME`)
用户pojo:
@Entity
@Table(name= "User")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "USERNAME")
private String username;
@Column(name = "PASSWORD", nullable = false)
private String password;
@Column(name = "IS_ACTIVE", nullable = false)
private boolean isActive;
@Column(name = "NAME", nullable = false)
private String name;
@Column(name = "email", nullable = false)
private String email;
@Column(name = "phone", nullable = false)
private String phone;
@OneToOne(mappedBy = "user", cascade=CascadeType.ALL)
private UserRole userRole;
}
删除了setter和getter以提高可读性。
答案 0 :(得分:0)
“用户名”是表“用户”中的主键,因此它必须具有唯一值且不能为空。因此,在持久化用户实体之前,必须为字段“username”设置正确的值。