Reputation: 280172
I have the following class:
@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() {}
}
When I run my application, Hibernate loads the configuration and creates the table for Users. I'm using Postgresql. This is the SQL statement being generated (logged by 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))
Why is it that a boolean is created as not null and a String/Timestamp is not? I won't there to be an exception if you try to insert a User without an email or password, but with this generated schema it won't. Is there another way to force Hibernate to create the field as not nullable?
Upvotes: 2
Views: 2134
Reputation: 90567
From 5.1.4.1.2. Access type :
By default the access type of a class hierarchy is defined by the position of the @Id or @EmbeddedId annotations. If these annotations are on a field, then only fields are considered for persistence and the state is accessed via the field. If there annotations are on a getter, then only the getters are considered for persistence and the state is accessed via the getter/setter.
As you annotate @Id
on the getter , you are using property access. So , hibernate will ignore all mapping annotations marked on the fields and only use annotations on getters to generate DDL . Because there are no annotations on getters , default settings are used .
You should get your desired result if you change to annotate @Id
on the userID
instead of getUserID()
Upvotes: 2