定义了一个报告类,其中报告内容content使用@Lob定义为大数据字段
@Embeddable
public class ReportPo
{
@Column(name="report_title")
private String title;
@ManyToOne(targetEntity=AccountPo.class)
@JoinColumn(name="report_tester", referencedColumnName="account_id", nullable=false)
private AccountPo tester;
@ManyToOne(targetEntity=AccountPo.class)
@JoinColumn(name="report_reporter", referencedColumnName="account_id", nullable=false)
private AccountPo reporter;
@ManyToOne(targetEntity=AccountPo.class)
@JoinColumn(name="report_verifier", referencedColumnName="account_id", nullable=false)
private AccountPo verifier;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="report_creat_date")
private Date createDate;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="report_publish_date")
private Date publishDate;
@Lob
@Column(name="report_content", nullable=false)
private String content;
@ManyToOne(targetEntity=CorporationPo.class)
@JoinColumn(name="corporation_id", referencedColumnName="corporation_id", nullable=false)
private CorporationPo corporation;
}
启动后发现hibernate没有自动创建report表,console没有报错信息。
hibernate版本如下图:
由于其他表都可以正确自动创建,因此排除hibernate配置错误的可能。
采用排除法依次注释report中的字段,直到发现若定义content字段hibernate就无法自动建表report,去掉content字段就可以正常建表。
再次测试发现,问题出在nullable=false。将其删除,或者修改为nullable=true均可正常建表。
@Lob
@Column(name="report_content", nullable=true)
private String content;
或
@Lob
@Column(name="report_content")
private String content;
那么问题来了,这个究竟是hibernate的bug,还是hibernate故意要求“不能强制longtext字段不能为null”?
+---------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+-------+
| order_id | int(11) | NO | PRI | NULL | |
| report_content | longtext | YES | | NULL | |
| corporation_id | int(11) | NO | PRI | NULL | |
| report_creat_date | datetime | YES | | NULL | |
| report_publish_date | datetime | YES | | NULL | |
| report_reporter | int(11) | NO | PRI | NULL | |
| report_tester | int(11) | NO | PRI | NULL | |
| report_title | varchar(255) | YES | | NULL | |
| report_verifier | int(11) | NO | PRI | NULL | |
+---------------------+--------------+------+-----+---------+-------+