简体   繁体   English

Hibernate 的 nullable = false 不适用于所有情况

[英]Hibernate's nullable = false not working in all cases

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.当我运行我的应用程序时,Hibernate 加载配置并为用户创建表。 I'm using Postgresql.我正在使用 Postgresql。 This is the SQL statement being generated (logged by hibernate):这是正在生成的 SQL 语句(由休眠记录):

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?有没有另一种方法可以强制 Hibernate 将字段创建为不可为空?

From 5.1.4.1.2.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.默认情况下,类层次结构的访问类型由 @Id 或 @EmbeddedId 注释的位置定义。 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.如果在 getter 上有注解,那么只有 getter 被考虑用于持久化,并且通过 getter/setter 访问状态。

As you annotate @Id on the getter , you are using property access.当您在 getter 上注释@Id时,您正在使用属性访问。 So , hibernate will ignore all mapping annotations marked on the fields and only use annotations on getters to generate DDL .因此,hibernate 将忽略字段上标记的所有映射注释,只使用 getter 上的注释来生成 DDL。 Because there are no annotations on getters , default settings are used .因为 getter 上没有注释,所以使用默认设置。

You should get your desired result if you change to annotate @Id on the userID instead of getUserID()如果您更改为在userID上注释@Id而不是getUserID()您应该得到您想要的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM