Inside my entity class I have a column of type timestamp:
#Column(name = "TESTD")
#Temporal(TemporalType.TIMESTAMP)
private Date test_date;
Inside my session bean I'm creating a select query which return a resultList, and then I'm creating from it a json object:
Query query = em.createNamedQuery("findAllTest");
List<entityClass> results = query.getResultList();
JSONSerializer.toJSON((List)results ,jsonConfig);
when creating the json object I want the timestamp column to be formatted (and not to return as object). How can this be done? how can I cast/format the timestamp column according to the date format I want? what is the best way to do this?
I guess you're using json-lib, based on the code sample, and I've never used it, but the javadoc shows that JsonConfig provides the following method:
public void registerJsonValueProcessor(Class propertyType,
JsonValueProcessor jsonValueProcessor)
Registers a JsonValueProcessor.
[Java -> JSON]
So I guess you could use that method, and register a processor for Calendar.class that would transform the Calendar object into a String using the format you want..
I create a second transient getter that returns the date as a String formatted how I want. It's a bit if a hack but works.
Related
I have an entity named A, which has a createdDate with this type #Temporal(TemporalType.TIMESTAMP).
I tried to retrieve all the rows having a certain createdDate. I attached a certain date to the request and I printed that date, it looks fine like "2021-02-11 12:14:02.425", which has all the values up to millisecond.
But the sql from hibernate, the value for createdDate in the where clause is set as '11-Feb-21'. Therefore, I do not find any rows because the createdDate is saved as 2021-02-11 12:14:02.425 in the db.
public Response getByCreatedDate(Request req) {
List<AResponse> aList = aRepository.findByIdAndCreatedDate(req.getId(), req.getCreatedDate());
}
I am new for Hibernate, I tried find some useful information about it but could not. Do I have to explicitly create a new Date with those specific date and time and send it to the method 'findByIdAndCreatedDate'? If anyone has the same experience, could you give some information about it?
I want to save the date that a post was created in Firestore but I do not want to use the System time. Rather I want to use the server timestamp for accuracy sake. So I am using FieldValue.serverTimestamp() to get the server timestamp but the data type of my variable that holds this is Date. So How can I cast FieldValue.serverTimestamp() to Date?
Below is how my data class looks
data class MyModel( var timeStamp: Date,
constructor(): this(Calendar.getInstance().time, "")
}
PS: When I declare the timestamp as FieldValue in the data class, I get the error below:
java.lang.RuntimeException: No properties to serialize found on class
com.google.firebase.firestore.FieldValue
You get the following error:
java.lang.RuntimeException: No properties to serialize found on class com.google.firebase.firestore.FieldValue
Because FieldValue is not a supported data type. You should use the Date class or any other class that extends Date class, for example Timestamp class.
How do I cast FieldValue.serverTimestamp() to Kotlin/Java Date Class
There is no need to do any cast. In Java there is even no need to initialize the timeStamp field. To make it work, you should only use an annotation, as explained in my answer from the following post:
ServerTimestamp is always null on Firebase Firestore
Edit:
In Kotlin, you should initialize your timeStamp field in the constructor with a null value like this:
data class MyModel(
#ServerTimestamp
val timeStamp: Date? = null)
You can make use of an object to hold this value and later while using this value check the type of the object and make use of it. As of my knowledge the datatype returned is Long and you have to convert it manually to Data if you need.
The code for this will look like this,
replace this
data class MyModel( var timeStamp: Date,
with
data class MyModel( var timeStamp: Object,
And when using this timeStamp anywhere check it's type.
In java it will look like
if (timeStamp instanceof Long) {
// change Long to Date
//do this
}else{
//do something else
}
set the value for timeStamp as FieldValue.serverTimestamp() itself.
model class
data class MyModel(
#get: PropertyName("timestamp") #set: PropertyName("timestamp") var timestamp: Date= Date()
)
when initialize it;
val model = MyModel().apply{
this.timestamp to FieldValue.serverTimestamp()
}
I got document that looks like this
#Document(collection="myDocument")
public class MyDocument {
#Id
private String id;
private List<Dates> dates;
}
public class Dates{
private String key;
private DateTime value;
}
And OtherDocument is container for DateTime values from various sources, I can't simply make fields like DateTime birthdate; inside MyDocument because I don't know what key will be, they are just some dates that describe MyDocument. Now, I need to create search engine for those values, for example, someone want's to find all MyDocuments with dates that contains:
key : "Birthdate" greater than
value : "1990-01-01 00:00:00 +00:00"
and key : "Mather's birthday" less than
value: "1975-01-01 00:00:00 +00:00"
So, Criteria (using MongoTemplate here) first may look like this
Criteria criteria = Criteria.where("myDocument.dates.value")
.exists(true)
.gt(DateTimeUtil.valueOf("1990-01-01 00:00:00 +00:00")) //just converting String to DateTime here
.and("myDocument.dates.name")
.exists(true)
.all("Birthday"));
And second one:
Criteria criteria = Criteria.where("myDocument.dates.value")
.exists(true)
.lt(DateTimeUtil.valueOf("1975-01-01 00:00:00 +00:00"))
.and("myDocument.dates.name")
.exists(true)
.all("Mather's birthday"));
The problem is, I can't put those both Criteria in one Query, it will cause error. The only soultion I found till now is to make 2 separate Query in that case and then find common part by using
resultA.retainAll(resultB)
But the point is, I don't want to, this database will store a lot of data and those requests will be very frequent. I need this to work fast, and combining 2 lists in pure Java will be slow as hell with that amount of data. Any ideas how to deal with that?
edit#
here is the error thrown when I try to combine 2 Criteria like this in one Query
caught: (java.lang.RuntimeException), msg(json can't serialize type :
class org.joda.time.DateTime) java.lang.RuntimeException: json can't
serialize type : class org.joda.time.DateTime
You can use below code. $and the query together and use $elemMatch to match the dates fields on multiple condition.
Something like
Criteria criteria1 = Criteria.where("dates").
elemMatch(
Criteria.where("value").exists(true).gt(DateTimeUtil.valueOf("1990-01-01 00:00:00 +00:00"))
.and("name").exists(true).all("Birthday")
);
Criteria criteria2 = Criteria.where("dates").
elemMatch(
Criteria.where("value").exists(true).lt(DateTimeUtil.valueOf("1975-01-01 00:00:00 +00:00"))
.and("name").exists(true).all("Mather's birthday")
);
Criteria criteria = new Criteria().andOperator(criteria1, criteria2);
Note: You may still have the problem with joda time conversion.
I fetched data from the oracle database through java code using a query similar to the below:
select min(specDate) from table
The result is supposed to be of type Date.
Since there was no specDate populated for any of the rows in the table, the result was null.
I used this query in my java code and mapped it to Date object using BeanPropertyRowMapper.
The result I got after mapping was the system date or the current date.
Not sure as to why the mapper returned current date instead of null.
Found the reason.
When the BeanPropertyRowMapper was provided with Date.class for mapping,
new Date() is called for instantiation of the class like for any object.
But for Date.class, new Date() returns sysDate.
I am using Compass to make queries on data inside in memory data structure. It works fine for searching String and enum values, now I want to search dates.
Search criteria are annotated by #SearchRestriction annotation. Example about someDate:
#SearchRestriction(path="fooBar.someDate" type = SearchRestrictionType.EQUAL)
String someDate;
At searchable data SomeDate is annotated like the following:
#SearchableProperty
Date someDate;
SomeDate inside the searchable data is generated with new Date();) and query String is given as 20120802.
Situation on debugger:
This code generates queries like this:
someDate:20120802
Here someDate is the name of the field I am looking for and 20120802 is a date in order yyyyMMdd.
Problem:
No results is returned, when this query is run. I get an empty list. The Date in query is the same as in the Date object.
What is wrong??
Is this wrong way to search Dates with Compass? I can find only range queries about Date, but a search with exact Date or part of exact Date I cannot find.
You need to specify the format for Searchable property [Date]
#SearchableProperty(format = "yyyyMMdd")
To some extent, it relates to Grails: Lucene, Compass Query Builder and date ranges