Movatterモバイル変換


[0]ホーム

URL:


ObjectDBObjectDB
ObjectDB Manual

Date and Time in JPQL and Criteria Queries

Date and time expressions may appear in JPQL queries:

This page covers the following topics:

Current Date and TimeExtracting Date PartsDate and Time in Criteria Queries

Current Date and Time

JPA defines special JPQL expressions that are evaluated to the date and time on the database server when the query is executed:

Extracting Date Parts

JPA doesn't define standard methods for extracting date and time parts but some JPA implementations, as well as ObjectDB, support such functions as an extension. ObjectDB supports 6 functions for extracting theYEAR,MONTH,DAY,HOUR,MINUTE andSECOND.

For example:

Date and Time in Criteria Queries

TheCriteriaBuilderjakarta.persistence.criteria.CriteriaBuilder - JPA Interface Used to construct criteria queries, compound selections, expressions, predicates, orderings. interface provides three factory methods for building date and time expressions that represent the current date and/or time:

// Create current date expression:Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<javax.sql.Date> date= cb.currentDateCriteriaBuilder.currentDate()-JPAMethodCreate expressiontoreturn current date.();// date only// Create current time expression:Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<javax.sql.Time> time= cb.currentTimeCriteriaBuilder.currentTime()-JPAMethodCreate expressiontoreturn current time.();// time only// Create current date & time expression:Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<javax.sql.Timestamp> ts= cb.currentTimestampCriteriaBuilder.currentTimestamp()-JPAMethodCreate expressiontoreturn current timestamp.();// both

Unlike constant date literals which are built once on the client side, the current date and time expressions are re-evaluated on the server on every query execution to reflect the date and time when the query is run.

Functions for extracting date and time parts are also available in criteria queries by using the genericCriteriaBuilderjakarta.persistence.criteria.CriteriaBuilder - JPA Interface Used to construct criteria queries, compound selections, expressions, predicates, orderings.'sfunctionCriteriaBuilder.function(name,type,args) - JPA Method Create an expression for the execution of a database function. method, as follow:

// Create expressions that extract date parts:Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<Integer> year= cb.functionCriteriaBuilder.function(name,type,args)-JPAMethodCreate an expressionfor the execution of a database function.("year",Integer.class, date);Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<Integer> month= cb.functionCriteriaBuilder.function(name,type,args)-JPAMethodCreate an expressionfor the execution of a database function.("month",Integer.class, date);Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<Integer> day= cb.functionCriteriaBuilder.function(name,type,args)-JPAMethodCreate an expressionfor the execution of a database function.("day",Integer.class, ts);// Create expressions that extract time parts:Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<Integer> hour= cb.functionCriteriaBuilder.function(name,type,args)-JPAMethodCreate an expressionfor the execution of a database function.("hour",Integer.class, time);Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<Integer> minute=    cb.functionCriteriaBuilder.function(name,type,args)-JPAMethodCreate an expressionfor the execution of a database function.("minute",Integer.class, time);Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<Integer> second= cb.functionCriteriaBuilder.function(name,type,args)-JPAMethodCreate an expressionfor the execution of a database function.("second",Integer.class, ts);
< Strings in JPQL^ Query ExpressionsCollections in JPQL >

[8]ページ先頭

©2009-2025 Movatter.jp