如何使用JPQL获取数据库时间? [英] How to get the database time with JPQL?

查看:150
本文介绍了如何使用JPQL获取数据库时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用原生SQL我使用如下语句获取数据库时间:

  SELECT CURRENT_TIMESTAMP 

使用JPQL我得到的结果与此相同:

  SELECT CURRENT_TIMESTAMP 
FROM Customer c
WHERE c.id = 1

有没有办法摆脱最后两行?



谢谢,

解决方案

根据 JSR 220:Enterprise JavaBeans 3.0 规范:


4.6.16功能表达式



Java持久性查询语言
包含以下内置的
函数,可用于查询的
WHERE或HAVING子句



如果
函数表达式的任何参数的值为null或
unknown,则函数
expressi的值on未知。



[...]

4.6.16.3日期时间函数

  functions_returning_datetime:= $​​ b $ b CURRENT_DATE | 
CURRENT_TIME |
CURRENT_TIMESTAMP

日期时间函数返回当前日期,时间的
值,和
时间戳记在数据库服务器上。

所以我很惊讶你可以编写第二个不正确的表单因此可能无法移植。



对我来说,正确的方法是创建一个类型为<$ c的日期字段的类$ c> java.util.Date 并用本机查询填充它。类似的东西:

  import java.util.Date; 

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@实体
公共类DateItem {
私人日期日期;

/ **
* @返回日期
* /
@Id
@Column(name =DATE_VALUE)
@ Temporal(TemporalType.TIMESTAMP)
public Date getDate(){
return date;

$ b / **
* @参数日期
*设置日期
* /
public void setDate(Date date) {
this.date = date;


$ / code $ / pre>

然后:

  @PersistenceContext 
EntityManager em;
$ b $ **
* @return数据库服务器上的系统日期
* /
public Date getSystemDate(){
Query query = em.createNativeQuery(
SELECT CURRENT_TIMESTAMP,DateItem.class);
DateItem dateItem =(DateItem)query.getSingleResult();
return dateItem.getDate();
}


with native SQL I get the database time with a statement like:

SELECT CURRENT_TIMESTAMP

with JPQL I get the same result with:

SELECT CURRENT_TIMESTAMP
FROM Customer c
WHERE c.id=1

Is there a way to get rid of the last two lines?

thanks,

解决方案

According to the JSR 220: Enterprise JavaBeans 3.0 specifications:

4.6.16 Functional Expressions

The Java Persistence query language includes the following built-in functions, which may be used in the WHERE or HAVING clause of a query.

If the value of any argument to a functional expression is null or unknown, the value of the functional expression is unknown.

[...]

4.6.16.3 Datetime Functions

functions_returning_datetime:=
             CURRENT_DATE |
             CURRENT_TIME |
             CURRENT_TIMESTAMP

The datetime functions return the value of current date, time, and timestamp on the database server.

So I'm already surprised you can write the 2nd form that is not correct per specification and might thus not be portable.

To me, the "right" way to do this would be to create a class with a date field of type java.util.Date and to populate it with a native query. Something like that:

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class DateItem {
    private Date date;

    /**
     * @return the date
     */
    @Id
    @Column(name = "DATE_VALUE")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getDate() {
        return date;
    }

    /**
     * @param date
     *            the date to set
     */
    public void setDate(Date date) {
        this.date = date;
    }
}

And then:

@PersistenceContext
EntityManager em;

/**
 * @return System date on DB server
 */
public Date getSystemDate() {
    Query query = em.createNativeQuery(
            "SELECT CURRENT_TIMESTAMP", DateItem.class);
    DateItem dateItem = (DateItem) query.getSingleResult();
    return dateItem.getDate();
}

这篇关于如何使用JPQL获取数据库时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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