Hibernate插入语句中的SQL函数 [英] SQL function in Hibernate insert statement

查看:335
本文介绍了Hibernate插入语句中的SQL函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆