Hibernate. Check if daterange is within another daterange - java

This sounds pretty simple, but i just cant wrap my head around it.
I have two fields in my DB. availableFromDate and availableToDate.
The user is doing a search to find all entities available within a given range. So my action receives two dates:
searchFromDate and searchToDate.
All i need to do is to return all entities that is available the whole period specified by the user.
Preferably using Criterias.
Anyone?

If I understand you question correctly - That is, that the event availability should lie completely within the search date range - then it should boil down to this:
If you ensure that "availableFromDate.before(availableToDate)" and "searchFromDate.before(searchToDate)" - which you should do anyway in this case - Then both your availableFromDate and your availableToDate have to be in the search date range.
You can use the solution from this answer to make sure both dates are within the range: How to compare dates in hibernate
criteria.add(Restrictions.between("availableFromDate", searchFromDate, searchToDate));
criteria.add(Restrictions.between("availableToDate", searchFromDate, searchToDate));

Related

How to auto-increment database id with uniqe product code and then reset it to 1 when change mont in spring boot?

I am currently working with java spring boot. I am using JPA Repository and postgresql as database
My business owner want me to create id of transaction that contain
PRODUCT_CODE-YEAR-MONTH-INCREMENT_NUMBER
increment number contain 8 length number
Example : 0000001, 0000011, 0000201
example : 48-2022-04-0000001
48-2022-04-0000001
48-2022-04-0000002
48-2022-04-0000003
-----> 48-2022-04-0000203
The increment number will be reset to be 0000001 when next month.
Will be:
48-2022-05-0000001
What the best way to do this?
Also if there is any query to select last data on given month and year?
Please help me, I just want to say thank you in advance
Multiple sequences, one per year-month
You don’t have one sequence. You have many sequences, one for each month.
Use Postgres command CREATE SEQUENCE to create a sequence named for each month. I suggest using a sequence name that includes the year-month in standard ISO 8601 format, YYYY-MM.
In Java, use the YearMonth to represent a particular month.
You said:
the increment number will be reset to be 0000001 when next month
No, don’t think in terms of resetting. For one thing, there may be exceptional circumstances where you need to create transactions for previous or future months. Resetting would preclude that.
So keep multiple sequences, one per month.

Date difference in days Restriction Hibernate

I am using Hibernate and Criteria API to write my database quires. What I need to do is I need two dates difference in days and compare that days with with specific number.
E.g. Commonly written criteria restriction as:
criteria.add(Restrictions.eq("somProperty", someValue));
What I needs is:
criteria.add(Restrictions.ge("dateProperty1 - dateProperty2", 15));
Means date difference between two dates is greater than or equal 15 days.
I don't see how to achieve this. And yes I did lots of Google to find out the possible solution but didn't get proper material what I need.
If you check the documentation of the Restrictions class, you will see that:
most functions operate on a "property vs value" pair (just like in your first example)
the remaining comparator functions operate on "property vs other property" pair
But no customized function is available for your case. So what option you have is using the sqlRestriction, which can be used to express this condition in a native form of your DBMS. That would be a different, but much easier problem, altough clearly not as elegant as your original idea.

Size of Strings and Calendar Objects Java

I am doing some basic (edit: reading and writing to a txt file), which requires me to store a bunch of expenses, and their attributes (i.e. name, price, date of purchase, etc.) I would like to be able to compare dates of purchases if possible. It occured to me that I had a few options when it came to what type of object the date of purchase should be:
I could make the date a Calendar object, and store it on the .txt this would mean storing lots of Calendar objects at once, and then easily compare the dates
I could make the date a String, store it, transmute it to a Calendar object, and then compare them
I could leave the dates as strings and when I am ready to compare them, create some kind of code to go through individual characters and pick out a certain phrase or set of characters.
Which of these would probably be best for keeping the load on the computer down? Also, how would you go about loading objects as they build up over time? Once a person has a lot of spending, it would get pretty hefty to load every single item.
I would strongly suggest using Joda Time wherever possible, rather than Calendar and Date - it's a much cleaner date/time API.
Beyond that, definitely make your object model match your domain as closely as possible. You're dealing with dates, not strings - so make your object model reflect that. You should be converting between strings and dates as rarely as possible. It not clear what you mean by "store it on the .txt" (given that elsewhere you're talking about a database) but using JDBC you'd use parameters anyway, without string conversions.
As for load - work out your performance requirements beforehand, try the simplest approach that works, and test whether that meets your requirements. Usually when people talk about having to have an efficient solution they haven't actually considered what they need. You talk about it getting "pretty hefty" to load every single item - how many items? Can you load them in a batch? Where will the database be? You'd be amazed how much data can be processed these days - but you need to understand the parameters of your problem before you make too many decisions that are hard to change later.

Is it Java best practice to store dates as longs in your database?

My reason for doing so is that dates stored as date objects in whatever database tend to be written in a specific format, which may greatly differ from what you need to present to the user on the front-end. I also think it's especially helpful if your application is pulling info from different types of data stores. A good example would be the difference between a MongoDB and SQL date object.
However, I don't know whether this is recommended practice. Should I keep storing dates as longs (time in milliseconds) or as date objects?
I can't speak for it in relation to MongoDB, but in SQL database, no, it's not best practice. That doesn't mean there might not be the occasional use case, but "best practice," no.
Store them as dates, retrieve them as dates. Your best bet is to set up your database to store them as UTC (loosely, "GMT") so that the data is portable and you can use different local times as appropriate (for instance, if the database is used by geographically diverse users), and handle any conversions from UTC to local time in the application layer (e.g., via Calendar or a third-party date library).
Storing dates as numbers means your database is hard to report against, run ad-hoc queries against, etc. I made that mistake once, it's not one I'll repeat without a really good reason. :-)
It very much depends on:
What database you're using and its date/time support
Your client needs (e.g. how happy are you to bank on the idea that you'll always be using Java)
What information you're really trying to represent
Your diagnostic tools
The third point is probably the most important. Think about what the values you're trying to store really mean. Even though you're clearly not using Noda Time, hopefully my user guide page on choosing which Noda Time type to use based on your input data may help you think about this clearly.
If you're only ever using Java, and your database doesn't have terribly good support for date/time types, and you're only trying to represent an "instant in time" (rather than, say, an instant in a particular time zone, or a local date/time with an offset, or just a local date/time, or just a date...), and you're comfortable writing diagnostic tools to convert your data into more human readable forms - then storing a long is reasonable. But that's a pretty long list of "if"s.
If you want to be able to perform date manipulation in the database - e.g. asking for all values which occur on the first day of the month - then you should probably use a date/time type, being careful around time zones. (My experience is that most databases are at least shocking badly documented when it comes to their date/time types.)
In general, you should use whatever type is able to meet all your requirement and is the most natural representation for that particular environment. So in a database which has a date/time type which doesn't give you issues when you interact with it (e.g. performing arbitrary time zone conversions in an unrequested way), use that type. It will make all kinds of things easier.
The advantage of using a more "primitive" representation (e.g. a 64 bit integer) is precisely that the database won't mess around with it. You're effectively hiding the meaning of the data from the databae, with all the normal pros and cons (mostly cons) of that approach.
It depends on various aspects. When using the standard "seconds since epoch", and someone uses only integer precision, their dates are limited to the 1970-2038 year range.
But there also is some precision issue. For example, unix time ignores leap seconds. Every day is defined to have the same number of seconds. So when computing time deltas between unix time, you do get some error.
But the more important thing is that you assume all your dates to be completely known, as your representation does not have the possibility to half only half-specified dates. In reality, there is a lot of events you do not know at a second (or even ms) precision. So it is a feature if a representation allows specifing e.g. only a day precision. Ideally, you would store dates with their precision information.
Furthermore, say you are building a calendar application. There is time, but there also is local time. Quite often, you need both information available. When scheduling overlaps, you of course can do this best in a synchronized time, so longs will be good here. If you however do also want to ensure you are not scheduling events outside of 9-20 h local time, you also always need to preserve timezone information. For anything that does span more than one location, you really need to include the time zone in your date representation. Assuming that you can just convert all dates you see to your current local time is quite naive.
Note that dates in SQL can lead to odd situations. One of my favorites is the following MySQL absurdity:
SELECT * FROM Dates WHERE date IS NULL AND date IS NOT NULL;
may return records that have the date 0000-00-00 00:00:00, although this violates the popular understanding of logic.
Since this question is tagged with MongoDB: MongoDB does not store dates in String or what not, they actually store it as a long ( http://www.mongodb.org/display/DOCS/Dates ):
A BSON Date value stores the number of milliseconds since the Unix epoch (Jan 1, 1970) as a 64-bit integer. v2.0+ : this number is signed so dates before 1970 are stored as a negative numbers.
Since MongoDB has no immediate plans to utilise the complex date handling functions (like getting only year for querying etc) that SQL has within standard querying there is no real downside, it might infact reduce the size of your indexes and storage.
There is one thing to take into consideration here, the aggregation framework: http://docs.mongodb.org/manual/reference/aggregation/#date-operators there are weird and wonderful things you can only with the supported BSON date type in MongoDB, however, as to whether this matters to you depends upon your queries.
Do you see yourself as needing the aggregation frameworks functions? Or would housing the extra object overhead be a pain?
My personal opinion is that the BSON date type is such a small object that to store a document without it would be determental to the entire system and its future compatibility for no apparent reason. So, yes, I would use the BSON date type rather than a long and I would consider it good practice to do so.
I dont think its a best practice to store dates as long because, that would mean that you would not be able to do any of the date specific queries. like :
where date between
We also wont be able to get the date month of year from the table using sql queries easily.
It is better to use a single date format converter in the java layer and convert the date into that and use a single format throughout the application.
IMHO , storing dates in DB will be best if you can use Strings. Hence avoid unnecessary data going up and down to server , if you don't need all the fields in Calendar.
There is a lot of data is in Calendar and each instance of Calender is pretty heavy too.
So store it as String , with only required data and convert it back to Calendar , whenvever you need them and use them.

Number of days since registration

I code a little Console program and now I store the date they joined in a database like.
CreateDate
2011-04-15 17:52:57
Now I want to do a check like this: a function that gets how many days the guy have been registered.
if(player.getDaysSinceRegistration) {
Thanks for any help.
joda-time has an easy way to do this:
Days.daysBetween(new DateTime(registeredDate), new DateTime()).getDays();
Without 3rd party libraries:
(System.currentTimeMillis() - registeredDate.getTime()) / MILLIS_PER_DAY;
First of all, don't store dates in databases in textual form.
You should save them as database dates (a type like numbers or varchars), as this will allow you to both ask the database do date calculations as well - the exact way to do so is database specific - as have it automatically pulled up in a Java date object by the JDBC driver which is much easier to work with than strings. See Bozho's answer for suggestions to do this in the Java layer.

Categories