我有实体:
@Entity
@Table(name = "message")
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "email")
private String email;
@Column(name = "subject")
private String subject;
@Column(name = "content")
private String content;
@Column(name = "html")
private String html;
@Column(name = "status")
private Integer status;
@Column(name = "creation_date")
private Date creationDate;
@Column(name = "send_date")
private Date sendDate;
//GET, SET
}
表 message
对列 creation_date 的定义如下:
creation_date timestamp without time zone NOT NULL DEFAULT now()\
作为结论,当我没有为 creation_date
设置特定值时,hibernate 将生成如下所示的查询:
insert into partner.message (content, creation_date, email, html, send_date, status, subject)
values ('text', null, 'text', 'text', null, '1', 'text')
是否有可能避免生成那些 null
值?
顺便说一句,我正在使用 PostgreSQL
。它与 PostgreSQL
-dialect 有关系吗?
最佳答案
您应该将列 creationDate
设置为不可空:
@Column(name = "creation_date", nullable = false)
private Date creationDate;
IIRC,如果插入为空,Hibernate 应该抛出异常。这样您就必须确保它始终处于设置状态。
另一种选择是为创建日期设置默认值:
@Column(name = "creation_date", nullable = false)
private Date creationDate = new Date(); //Or set it in the constructor
关于java - 是否可以指示hibernate在执行插入时不要设置空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29513143/