我有以下课程:
@Entity
@Table(name = "USERS")
public class User {
private Long userID;
@Id
@Column(name = "userID")
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
public Long getUserID() {
return userID;
}
@Column(nullable = false)
private boolean isActive;
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false)
private String password;
@Column
@Temporal(TemporalType.TIMESTAMP)
private Calendar lastLoggedIn;
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Calendar createdDate;
@Version
private Integer version;
public String getEmail() {
return email;
}
public boolean isActive() {
return isActive;
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Calendar getLastLoggedIn() {
return lastLoggedIn;
}
public void setLastLoggedIn(Calendar lastLoggedIn) {
this.lastLoggedIn = lastLoggedIn;
}
public Calendar getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Calendar createdDate) {
this.createdDate = createdDate;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public void setUserID(Long userID) {
this.userID = userID;
}
public User(Long userID, String email, String password, Calendar lastLoggedIn, Calendar createdDate, Integer version) {
}
// No argument constructor
public User() {}
}
当我运行我的应用程序时,Hibernate 会加载配置并为用户创建表。我正在使用 Postgresql。这是正在生成的 SQL 语句(由 hibernate 记录):
Hibernate: create table USERS
(userID int8 not null, active boolean not null, createdDate timestamp,
email varchar(255), lastLoggedIn timestamp,
password varchar(255), version int4, primary key (userID))
为什么将布尔值创建为非空而字符串/时间戳不是?如果您尝试插入没有电子邮件或密码的用户,我不会有例外,但是使用此生成的模式不会。是否有另一种方法可以强制 Hibernate 将该字段创建为不可为空?