Hibernate插入语句中的SQL函数 [英] SQL function in Hibernate insert statement
问题描述
在Hibernate查询的插入语句中,如何在 getdate()
包含一个函数? 可以说映射类的其中一个属性是 datetime
列,put如何指定 getdate()
code> .save()方法调用使用服务器时间戳?
简单答案是不要在插入语句中包含它,并让SQL执行它。
在SQL表格设计器中,您将插入的日期列设置为缺省值为列属性中的 GETDATE()
,SQL Server将为您处理它。
参考和示例: SQL Server GETDATE()函数
CREATE TABLE订单
(
OrderId int NOT NULL PRIMARY KEY,
ProductName varchar(50)N OT NULL,
OrderDate datetime NOT NULL DEFAULT GETDATE()
)
How do include a function like getdate()
in an insert statement for Hibernate query?
Lets say one of the properties of a mapped class is a datetime
column how do put specify getdate()
in order for the .save()
method call to use the server timestamp?
Simple answer is don't include it in the insert statement and let SQL do it.
In the SQL table designer where you have the date column you are inserting into, set the Default Value to GETDATE()
in the column properties and SQL Server will handle it for you.
Reference and sample: SQL Server GETDATE() Function
Table create script will look like this:
CREATE TABLE Orders
(
OrderId int NOT NULL PRIMARY KEY,
ProductName varchar(50) NOT NULL,
OrderDate datetime NOT NULL DEFAULT GETDATE()
)
这篇关于Hibernate插入语句中的SQL函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!