用hibernate配置插入数据库默认值

本文介绍如何在Hibernate配置文件中启用动态插入功能,以实现在数据库插入操作时自动使用字段的默认值,同时避免客户端提供不必要的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当数据库中字段有默认值且不为空时,如果想实现不需要在客户端插入值,而是插入数据库默认值时,应该在配置hibernate实体文件时,为<class> 加入dynamic-insert="true",并且该<property>不能设置 not-null="true"。
dynamic-insert 是hibernate 根据实体具体的插入数据情况生成 sql语句。
如:User表 mysql
create table user
(
   id int primary key unique,
   realName varchar(10) not null ,
   idCode varchar(20) not null,
   createDate timestamp default now()
);


<class name="user" table="user" dynamic-insert="true">
  <id name="id" type="java.lang.Integer">
<column name="id" />
     <generator class="native" />
</id>
<property name="realName" type="java.lang.String">
<column name="realName" length="10" not-null="true" />
</property>
<property name="idCode" type="java.lang.String">
<column name="idCode" length="20" not-null="true" />
</property>
<property name="createDate" type="java.util.Date">
<column name="createDate" length="19" not-null="true"(不能加)/>
</property>
</class>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值