1
我使用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和可讀性更好乾將。
你能否提供一個java代碼,在哪裏創建User對象並將其保存在數據庫中? – mateuszlo