Converting date as timestamp while selecting using Hibernate APIs - java

We have a GUI application which sends around 40 parameters to the DB.
Based on these parameters a dynamic query is generated to fetch data. Number of fields that needs to be selected remains the same, only where condition differs.
Hibernate criteria API is used to form dynamic queries. We are using SQLMX as the database running on HP Nonstop server.
Certain dates need to be stored as 0001-01-01. Now, while selecting this field (DATE datatype), application displays this as 2001-01-01. When query is run from RazorSQL also, we are seeing incorrect results.
I could resolve this issue in RazorSQL by doing "select (cast(date1 as timestamp) from table". This gives date correctly, but with timestamp. This is OK.
In Java application, I suggested project team to use NamedNativeQueries wherein CAST as TIMESTAMP construct is specified in the query itself. Later on , I came to know that team cannot used NamedNativeQueries since queries are dynamic in nature. (Where condition can have 1 or 5 or no clauses).
Tried using Temporal.DATE annotation. Did not work. What i observed is that, we need to cast the date to timestamp while selecting itself. setMethod for the variable gets called after query has happened and if we do not do cast while doing select, value of 0001-01-01 gets changed to 2001-01-01.
Is there any way we can apply SQL constructs (similar to_date in Oracle ) before the query is executed?

Related

What location CURRENT_TIMESTAMP in JPA get in Java using database postgres?

I using jpa and using jpa custom query excute method. I using TypedQuery and Entity manage excute when i write query. My Jpa excute look like :
String query = "select s from Student s where (b.beginDate + b.beginTime) < CURRENT_TIMESTAMP";
But when i excute it, it wrong when i select. It differs up to 2 hours
I have 2 question:
When i excute it, CURRENT_TIMESTAMP get database time, server
containing database or server running java machine. Because I have 2
servers. A server contains a database with a Paris time zone and a
server running spring with a JDK containing a Japan time zone.
When I create it, I want format CURRENT_TIMESTAMP as 'YYYY-MM-DD HH:Mm:SS'
Please help. Thanks
Your problem has nothing todo with JPA !
In Postgresql documentation it is written
The PostgreSQL CURRENT_TIMESTAMP() function returns the current date
and time with time zone, which is the time when the transaction
starts.
The TimeStamp returned is always a data and time WITH TIME ZONE.
In your SELECT the comparison will work if you have previously saved a time in same format.
If you have a field BEGIN_DATE_TIME with correct format, no problem.
You have certainly a problem because (b.BEGINDATE + b.BEGINTIME) will not produce a date and time with time zone value !
Can you give us more information about these 2 fields ?

How to store date as Oracle SYSDATE with JpaRepository save method?

I am working on a Spring Data JPA project, where I want to store Oracle SYSDATE in a date field of a table. I can not modify the table at all.
Right now, what I am doing is passing new Date() to that date field. Which is not correct as the Oracle server is in a different timezone.
I am not writing any query to insert the data, instead I am using JpaRepository save() method.
How can I do this?
P.S. I do not want to hard code the timezone of the database server in my code.
There is no direct way to do this (see Setting default values for columns in JPA).
What you could do is to perform a select SYSDATE from dual and use the result to set your property.
The method to get the sysdate could be in your Spring Data Repository
#Query(value=`select SYSDATE from dual`, nativeQuery=true)
Date currentDate();
You could set the value in a #PrePersist Listener (see onSave() (for any Entity saved with Hibernate/Spring Data Repositories) ).
But I think you can't perform queries in those listeners, so the next thing would be to create a custom implementation of Spring Data's save method, so that it gets such a value and keeps it available for the Listener, before actually saving anything. Alternatively one could use a separate connection of the query.
Obviously, this all adds another database roundtrip, which is rather expensive.
An alternative would be to get the current time of the server once and use that just to determine the correct offset to use and create the timestamps locally, using that offset. This is much faster and easier but breaks when application server and database server have different daylight saving time rules.

Documentum: ORDER_BY r_modify_date takes too long

I'm new to Documentum and have a simple problem, I am trying to retrieve all the record according to last modified.
Basically I have a datatable with 1000 records.
current we use
Select * from docfolder enabled (FETCH_ALL_RESULTS 1000)
The problem with the above statement is sometimes a newly created report or modified report will out of the 1000 range and our users will complain report not found * valid complain *
actually the last modified record does not even need to be the first on the list, it just need to appear.
I tried using
Select * from docfolder order by r_modify_date enabled (FETCH_ALL_RESULTS 1000)
but this takes too long(never complete). I try replacing * with a,b,c,d (fields) but it does not work too.
May I know if there is other solutions to my issue?
I am considering documentum "ENABLE (RETURN_TOP 10)" hint but I doubt it work for Oracle 11g and how does documentum define top 1000?
UPDATE: It seems that using data link via toad is faster than using DQL, but I need a DQL solution due to legacy issues.
Documentum 6.0 and Oracle 11g.
What version of Documentum are you using?
Ensure that there are indexes on the r_object_id. You may also want to add an index to the r_modify_date.
Further, when adding fields a,b,c,d - ensure that these fields are "non-repeating". In this way, Documentum will not need to join the _r table making the overall query faster.
Further, in DA, if you do the query, you can actually see the SQL query passed to Oracle. Take this query and run it in Toad and look for optimizations. You may also register the _s table so that your can DQL query the _s table directly.
I manage to solve this problem by querying the under lining table in oracle database.
The reason for slow performance was because of the table begin joint behind to obtain the result.
In future if you have exhausted all ways to optimize your DQL, just fall back to querying the oracle database.
I have recommended for all table view and search to query via oracle.
Only individual report are retrieved via documentum, sometimes I question the purpose of having documentum.

Object getting wrong time stamp from Hibernate saved to database

I am new to Hibernate and working on a web project that uses it.
I have an object called area that has a date object(java.sql.Timestamp) attribute modifiedDate. When I create a new object modifieDate is null and after send it down to getHibernateTemplate().saveOrUpdate(area); in my own class that extends org.springframework.orm.hibernate3.support.HibernateDaoSupport it is set with current timestamp and saved in the database. In the database it is saved as a datetime.
My problem is most of the time the object is updated with a Date that is 1 millisecond off compared to what it is saved as in the database, this leads to this exception
if anything is attempted updated without reloading the page:
an org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
There are no problems with the correct date when getting the object from database subsequent, it is only at creation it gets the wrong value.
Is there a way to get the correct modifiedDate at SaveOrUpdate?
Incase it is needed the mapping uses <timestamp name="modifiedDate" column="ModifiedDate"/> and all test are run on localhost with everything running on the same machine.
I already have a work around, by calling getHibernateTemplate().refresh(area); right after the saveOrUpdate I get the right timestamp, but I would still like to know if there is a way to get the correct modifiedDate at saveOrUpdate.
My theory:
What's happening is that you're using a less precise data type on the database side than on the Java side, so the values you persist to the DB are losing precision (or rounded somehow, see below) during generation of the DB-specific SQL. Here's how to tell for sure:
Remember, Hibernate works by generating SQL statements that are executed in your database. In order to diagnose issues with Hibernate mappings like this, you will need to see the SQL being executed in order to know precisely what's going on.
Turn on the 'show SQL' setting for Hibernate. This will cause Hibernate to dump the raw SQL being executed on the database.
Examine the logs from your tests. It will help to implement toString on your class and log the value to compare against the SQL hibernate generates for an INSERT. Do the values in the SQL statements match the value of the Java timestamp field?
You should check the precision settings for your database's DATETIME type. For example, on SQL Server, DATETIME implements millisecond rounding (see 'accuracy' field) that effectively causes loss of precision for millisecond values. You will want to change the Java type your column is mapped to or change the type of the column in the database, depending on your precision needs.

Using a criteria for time restriction

I'm having some problems with a simple query I'm doing.
I have a postgresql database, with a time wihout zone column.
I cannot change the type of this column. Also, I have to use criterias, so don't tell me to change this. However, if there is a better solution for the future instead of using the time type I'm curious about it.
This column is mapped to a java.util.Date attribute. This could be changed to Time or whatever.
What I'm doing is adding this :
Time someTime = someDate.getTime();
criteria.add(Restrictions.eq(propertyName, someTime)));
This criteria doesn't return anything, when it should. What is done is the following: The user puts some string time, that is parsed to Date, being that 1970-1-1 and the time. This time is then added to the criteria. Also, the time in the database I'm using as example, is the same Date (1970-1-1, etc) that the one that is created when parsing the user data.
I have tried to search for some documentation on how criterias are used against time without timezone but haven't found anything.
Suggestions why this is failing?
UPDATE:The problem has been solved using the library JodaTime. But I still don't understand why was failing...

Categories

Resources