ofy() appengine Date search query - java

I want to search a list of employee between two dates ie. 1 jan 2014 to 31 Dec 2014 using GAE ofy(objectify) for a branch.How do I do that?
ulist=ofy().load().type(Employee.class).filter("branch", branch).filter("date >=", fromdate).filter("date <=", toDate).list();
There are 2 fields
-Branch is auto generated field.
-For date I have taken 2 Datebox, fromDate and toDate and using those 2 date I have to query on date field.
Above query written is not working & not giving results when searched between date. However if same date is entered in both from date & todate then its working.
Is there any isssue with my logic or is there any other method which I can use to find out employees of branch joined between two dates?

You will need to specify a custom index to support this query as it combines multiple filters on different properties.
As per the documentation:
Other forms of query require their indexes to be specified in the index configuration file, including:
...
Queries with one or more inequality filters on a property and one or more equality filters on other properties
...
Your query has an equality filter (on branch) with two inequality (on date) filters.
The documentation tells you all you need to know about configuring your custom indexes.

Related

Split date range into each day within the range with start and end date

I have a scenario where a row with a date range has to be split into multiple rows for all the days within that date range. Does anyone know how to obtain target (as attached) in this scenario in Informatica powercenter?
SOURCE
code start date end date
ADMISSION 01/01/2017 02:05:11 01/01/2017 04:20:53
TRANSFER 01/01/2017 04:20:54 01/03/2017 18:30:48
DISCHARGE 01/03/2017 18:30:49 01/03/2017 20:18:11
TARGET
code start date end date
ADMISSION 01/01/2017 02:05:11 01/01/2017 04:20:53
TRANSFER 01/01/2017 04:20:54 01/01/2017 11:59:59
TRANSFER 01/02/2017 00:00:00 01/02/2017 11:59:59
TRANSFER 01/03/2017 00:00:00 01/03/2017 18:30:48
DISCHARGE 01/03/2017 18:30:49 01/03/2017 20:18:11
Thank you in advance!
Easiest is to have a table of dates which you join against each record (sql join in source qualifier) by virtue of the date record's day being greater than or equal to the start date's day and less than or equal to the end date's day. Then add extra logic later in the mapping to pick the start-time from the starttime field wben the days are the same otherwise from the datetablestarttime and similar for the end time
Please try with java transformation!
Find the difference between the start and end date,
Create the new records using rowgen in java transformation.
Hope this answers your question.
There is no native support for row multiplication in PowerCenter. Hence a workaround is needed. Calculate the date differences and use it to multiply rows. The options are:
Use Java Transformation to generate extra rows (here's an example)
Create a dummy table with data like [1,2,2,3,3,3,...] and use it to join on the date difference. Keep in mind the number of rows grows fast and it's not recommended for large numbers
Create active, noncached lookup that will return appropriate number of rows for given input. Use SQL query override to fetch required number of rows for each processed row*
*assuming you create a myNumbers table with an myNumber column type bigint and populate it once with a sequence from 1 to X, you could use the following query for lookup override: SELECT 1 FROM myNumbers WHERE myNumber = ?in_lookup_port_name?. Use dummy port with value of 1 as condition.

Hibernate: how to get time gap between two Date columns?

For example, my columns look like
1990-02-13 00:00:00.0 and 2015-02-19 00:00:00.0.
How can I get the time gap (difference) in days?
If you are using MySQL dialect for Hibernate you can use the following functions,
unix_timestamp(datetime_value) - to take the difference between two columns
current_date() to get the current date.
a list of reference functions could be found here
https://hibernate.atlassian.net/secure/attachment/11953/MySQLDialect.java
For the second question can you try using
criteria.add( Restrictions.le("date_column", current_date() ) )

java criteria Date comparison

I am filtering a search with criteria object.
but the filter doesn't work for date.
I made this for instance :
criteria.add(Restrictions.and(
Restrictions.like("serialNumber", device.getSerialNumber()),
Restrictions.like("installDate", device.getInstallDate()), // a date
Restrictions.like("ipAdress", device.getIpAdress())));
then i made this :
else if (device.getInstallDate() != null) {
criteria.add(Restrictions.like("installDate", device.getInstallDate()));
}
Do you have any idea to filter by date ?
Your code/approach looks fine. You may want to enable SQL logging to see what statements exactly are sent to the DB and what values are bound to the statement parameters. This should help you figure out the issue (the issue may be just some detail like e.g. dates with/without time parts, or something similar).
See also:
Hibernate show real SQL
To search for exact dates I use:
criteria.add(Restrictions.eq("installDate", device.getInstallDate()));
Note also that dates and timestamps are treated differently by the underlying database based on the corresponding SQL types. A field declared as a date will not include hours/minutes/etc. If the desire is to compare both date and time, be sure to use timestamp in the Hibernate declaration.
The fastest way to show the SQL statements is to set the show_sql property to true in your Hibernate configuration

Using Hibernate's Restrictions.ilike() on a Timestamp

I've been thinking it over, and I'm starting to wonder if this is even possible.
User Perspective:
There's a table of data, and one column contains a date. The user can type in a search term like dec and get all rows that occurred during December.
Backend: A jqGrid is used for displaying the table. It sends the entered search terms to the server. The server uses the code
Criteria cr = session.createCriteria( DetailedLogEntry.class );
Disjunction disjunction = Restrictions.disjunction();
MatchMode matchMode = MatchMode.ANYWHERE;
disjunction.add( Restrictions.ilike( searchKey.getField(), searchKey.getData(), matchMode ) );
cr.add( disjunction );
to apply the search terms, and where DetailedLogEntry contains a Date variable to represent the database's TIMESTAMPfield.
Because searchKey.getData() returns a string, comparing it against a date object results in an empty set.
So I guess the question is...is it possible, preferbly through Hibernate, to apply a restriction against a Date object as if it were a String?
That's not possible. You'd need to use Restrictions.between() and give it an upper and lower date values. You could use SimpleDateFormat to convert from your String values to Date values and then perform the search?
If the user searched for Dec, would you expect all the log entries from December of every year to show up? Can they type in :"1, Dec" and expect to see all the logs from the 1st December for every year? If it is string matching on dates you are looking for, it might be easier to load all the data into your jqGrid and use javascript to filter the table based on the string formatted date values.

Query by age in hql

I have a class User with one field called birthDate which is a java.sql.Date.
How do I do a hql query that will retrieve all Users that are between min and max years old?
(My real scenario is slightly more complex than that but that's where I am stuck right now).
UPDATE
It must be an hql expression so I can put the age expression in a computed property.
Calculate the birth dates corresponding to the min and max ages. Then use the below HQL.
Select u from User u where u.birthDate between :minDate and :maxDate
Setup the the minDate and maxDate to the values you computed before executing the query.
It depends on the database. Most have some way of handling date arithmetic. MySQL has a datediff function that will return a number of days so you could divide that by 365 and get pretty close. The MySQL dialect already has datediff as a registered function. For other databases you may need to use different functions and possibly register them in a custom dialect. But you may be off by a little unless you take leap years into account which is tricky in an HQL expression. Using dates is easier because you can keep the month and day constant, change the year, and then use < or > in HQL.

Categories