【发布时间】:2020-07-07 04:59:23
【问题描述】:
如果我像下面这样查询,我们会通过 jpql 使用 oracle db 获取数据。
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("postgres");
EntityManager entityManager = emf.createEntityManager();
try {
entityManager.getTransaction().begin();
String jpqlQuery = "SELECT coalesce(c.actualDateTime, "
+ "coalesce("
+ "c.propDepDateTime+"
+ "TO_NUMBER(c.estOnDateTime-c.estOffDateTime),"
+ "c.estOnDateTime )) FROM Flight c ";
Query query = entityManager.createQuery(jpqlQuery);
List<Object> objects = query.getResultList();
objects.stream().forEach((x) -> System.out.println(x));
entityManager.getTransaction().commit();
} finally {
entityManager.close();
emf.close();
}
}
import org.springframework.format.annotation.DateTimeFormat;
@Entity
@Table(name = "FLIGHT")
public class Flight {
@Id
private Long id;
@Column(name = "ACT_DATETIME", columnDefinition = "Date")
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(style = "M-")
private Date actualDateTime;
@Column(name = "PROPAG_DEP_DATETIME", columnDefinition = "Date")
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(style = "M-")
private Date propDepDateTime;
@Column(name = "EST_ON_DATETIME", columnDefinition = "Date")
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(style = "M-")
private Date estOnDateTime;
@Column(name = "EST_OFF_DATETIME", columnDefinition = "Date")
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(style = "M-")
private Date estOffDateTime;
DROP TABLE IF EXISTS flight;
CREATE TABLE flight(
id BIGINT PRIMARY KEY ,
ACT_DATETIME TIMESTAMP WITHOUT TIME ZONE,
PROPAG_DEP_DATETIME TIMESTAMP WITHOUT TIME ZONE,
EST_ON_DATETIME TIMESTAMP WITHOUT TIME ZONE,
EST_OFF_DATETIME TIMESTAMP WITHOUT TIME ZONE
);
INSERT INTO flight(ID,ACT_DATETIME, PROPAG_DEP_DATETIME,EST_ON_DATETIME,EST_OFF_DATETIME)
VALUES(
1,(SELECT now()::timestamp),(SELECT now()::timestamp+1 * INTERVAL '1 DAY'),(SELECT now()::timestamp+2* INTERVAL '1 DAY'),
(SELECT now()::timestamp+3* INTERVAL '1 DAY')
);
在迁移到 Postgres 时,我将 jpql 更改如下,但抛出 ERROR:
operator does not exist: timestamp without time zone + numeric
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
尝试乘以区间等,但尚未奏效。欢迎任何帮助。
String jpqlQuery = "SELECT coalesce(c.actualDateTime, "
+ "coalesce("
+ "c.propDepDateTime+("
+ "TO_NUMBER(quote_literal(c.estOnDateTime-c.estOffDateTime),'99999999.99999999')),"
+ "c.estOnDateTime )) FROM Flight c ";
【问题讨论】:
-
(SELECT now()::timestamp)可以简化为now() -
抱歉,无法编辑帖子。在编辑它告诉,“我的帖子是更多的代码。添加更多的细节”。对于选择我遇到问题。 Insert 在 pgadmin 中执行时工作正常。
-
问题中无需更改,只需修复您的实际代码即可。在表达式中使用函数调用时,无需在函数调用前加上
select。你在 Oracle 中使用过values ( (select sysdate from dual), (select sysdate + 1 from dual))吗? -
在实际代码中完成
标签: postgresql jpa jpql