如何使用JPQL获取数据库时间?

发布于 2024-08-10 03:26:45 字数 223 浏览 7 评论 0原文

使用本机 SQL,我可以通过如下语句获取数据库时间:

SELECT CURRENT_TIMESTAMP

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

SELECT CURRENT_TIMESTAMP
FROM Customer c
WHERE c.id=1

有没有办法去掉最后两行?

谢谢,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤蝉 2024-08-17 03:26:45

根据 JSR 220:Enterprise JavaBeans 3.0 规范:

4.6.16 函数表达式

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

如果任何参数的值是
函数表达式为空或
未知,泛函的值
表达未知。

[...]

4.6.16.3 日期时间函数

functions_returning_datetime:=
             当前日期 |
             当前_时间 |
             CURRENT_TIMESTAMP

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

因此,我已经很惊讶您可以编写第二种形式,该形式根据规范不正确,因此可能不可移植。

对我来说,“正确”的方法是创建一个具有 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;

@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;
    }
}

然后:

@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();
}

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();
}
暖树树初阳… 2024-08-17 03:26:45

对于 java 8,可以将 Date 更新为 Instant,因此我将 @Pascal 的代码更改为:

import java.time.Instant;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class DateItem {
    private Instant date;

    /**
     * @return the date
     */
    @Id
    @Column(name = "DATE_VALUE")
    public Instant getDate() {
        return date;
    }

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

然后:

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

注意:SELECT 更改为@Dominic 指出要让它发挥作用。但是,“SELECT CURRENT_TIMESTAMP AS DATE_VALUE FROM DUAL”适用于 H2,但不适用于 postgres。 “SELECT CURRENT_TIMESTAMP AS DATE_VALUE” 适用于“在我的系统上”(TM) 的 H2 和 postgres

For java 8, one would update Date to Instant, so I changed @Pascal's code to:

import java.time.Instant;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class DateItem {
    private Instant date;

    /**
     * @return the date
     */
    @Id
    @Column(name = "DATE_VALUE")
    public Instant getDate() {
        return date;
    }

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

And then:

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

Note: The SELECT is changed as pointed by @Dominic to make it work. However, "SELECT CURRENT_TIMESTAMP AS DATE_VALUE FROM DUAL"will work for H2 but not postgres. "SELECT CURRENT_TIMESTAMP AS DATE_VALUE" works for both H2 and postgres "on my system"(TM)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文