我有一个MySQL表,它有三列:id int(10), status varchar(10), created_on timestamp NULL DEFAULT CURRENT_TIMESTAMP。当我尝试使用INSERT INTO EMPLOYEE(status) VALUES('STATUS1')子句手动在表中插入记录时,它也正确地填充了created_on列的值。
但是,当我试图通过Hibernate插入记录时,没有填充created_on列。它仍然是空的。我知道我的专栏目前允许null,因为我没有明确宣布它为null,但我的问题更集中在hibernate方面。
如何确保自动填充created_on列,就像手动INSERT子句那样?或者这就是hibernate设计的工作方式?
这是我的hibernate片段:
Transaction tx = session.beginTransaction();
Employee employee = new Employee(status);
session.save(employee);
tx.commit();下面是Hibernate配置
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Employee" table="EMPLOYEE">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="status" column="status" type="string"/>
<property name="created_on" column="created_on" type="date"/>
</class>
</hibernate-mapping>发布于 2016-11-01 07:01:10
只有在插入查询中未指定列时,MySQL才会插入默认值。根据当前映射,hibernate将生成以下查询:
insert
into
EMPLOYEE
(status, created_on)
values
(?, ?)它将向created_on列插入null。要使默认的CURRENT_TIMESTAMP由MySQL实现,请在映射中使用insert="false"。
<property name="created_on" column="created_on" type="date" insert="false"/>https://stackoverflow.com/questions/40353169
复制相似问题