I'm using eclipselink 2.1 and I would like to get a little help in CriteriaBuilder,
How can I do this SQL ruler in Criteria API ?
AND TRUNC ( e008.atdt_008dtinclusao + e008.atni_008nro_dias_visual ) >= TRUNC (SYSDATE)
The first field is a Date, in my entity is a Joda DateTime
The Second field is a Integer is a number of date to view.
I can not sum and truncate this part with the Criteria.
Thank you for the attention and help.
Using org.eclipse.persistence.expressions.ExpressionBuilder I could do:
ExpressionBuilder officeExpression = new ExpressionBuilder(OfficeEntity.class);
officeExpression.anyOf(OfficeFields.judicialNotifications);
officeExpression.anyOf(OfficeFields.eventsHistoric);
officeExpression.anyOf(OfficeFields.contracts);
.and(ExpressionMath.add(officeExpression.get(OfficeFields.judicialNotifications).get(JudicialNotificationFields.dateInsert).toDate()
, officeExpression.get(OfficeFields.judicialNotifications).get(JudicialNotificationFields.daysViewer)).equal(JodaTimeHelper.now().toDate()))
Related
I'm struggling to find a proper example to group by date instead of date time in a jpql. Note that I am connecting to a postgres db.
Query looks as follows:
#Query("select new example.model.ExampleModel(te.dateCreated, te.transactionStatus, sum(te.amount), count(te)) from ExampleEntity te group by te.dateCreated, te.transactionStatus")
fun findAggregatedExamples(): List<ExampleModel>
I need to convert the contructor arg to date and the group by to date.
Managed to find something useful in here
Working Code:
#Query("select new example.model.ExampleModel(cast(te.dateCreated as date), te.transactionStatus, sum(te.amount), count(te)) from ExampleEntity te group by cast(te.dateCreated as date), te.transactionStatus")
fun findAggregatedExamples(): List<ExampleModel>
Constructor arg0 had to be changed from LocalDateTime to java.util.Date.
I am getting excapsulated expression missing issue while writing named query.
int a = em.createQuery("UPDATE MyProsess d SET d.updateDate = current_date() WHERE d.personId = :personId")
Even when i pass multiple parameters to above query it gives some new errors. For example
int a = em.createQuery("UPDATE MyProsess d SET d.updateDate = current_date(), d.status=:stat WHERE d.personId = :personId")
Anyone could suggest how to resolve this issue?
Ok I got it. Please replace current_date() with "current_date" remove the brackets I think HQL is not able to recognize it with brackets. Now in your Entities you have java.util.Date while current_date will return java.sql.Date this needs to be converted. Mark the attribute updateDate with #Temporal(TemporalType.Date) annotation to ensure smooth conversion.
I am to fix an older project, in which there is a database query gone wrong. In the Table, there is a field
viewTime TIME NOT NULL DEFAULT 0
I need to filter out the rows that actually have 0 as their viewTime:
Criteria query = /* create criteria */;
query.add(Restrictions.gt("viewTime", 0));
However, since viewTime is defined as a Date:
#Temporal(TemporalType.TIME)
private Date viewTime;
I get a casting exception. On the other hand, I have no idea how to create a valid Date object that represents time 0. I can't change the type of the field as well for this.
Any way I can express viewTime > 0 in this Criteria object?
I think you have to compare date object with (00/00/00) but the any API will not produce DATE value like it.
This might your solution convert to null refer this link
It seems to me, that you can try such construction:
Criteria query = /* create criteria */;
query.add(Restrictions.gt("viewTime", new Date(0)));
I would like to use Date() function in hibernate criteria, since one field is timestamp.
My sql would be:
"select * from feed where DATE(date) = DATE(NOW()) + INTERVAL 5 DAY"
When I try in criteria:
critera.add(Restrictions.eq("DATE(date)", "DATE(NOW()) + INTERVAL 5 DAY"));
I get:
could not resolve property: DATE(date) of: com.mycomapany.model.Feed
I have there in feed a field by the name date.
What is the problem? I am using MySQL
The best solution is to use the criteria api which you started to use.
Calendar c = Calendar.getInstance(LOCALIZATION);
//Add 5 Days
c.add(Calendar.DATE, 5);
criteria.add(Expression.ge("dateColumn",c.getTime());
You could probably find a way to make this work, but in my opinion, this is the wrong way to approach it.
One of the major features of Hibernate is the abstraction of the database-vendor-specific features, via the Hibernate Dialect.
You are attempting to access MySQL features directly, but through the vendor-neutral interface, thus your difficulty. Hibernate could have support for date intervals , but does not - see https://hibernate.onjira.com/browse/HHH-2434
So, the options seem to be:
Calculate the date ranges in your code, rather than in the HQL
Do what you need to do in SQL rather than HQL
Create a custom Hibernate Dialect, as in Performing Date/Time Math In HQL?
Using Joda Time:
public List<Feed> findByDaysFromToday(int daysFromToday) {
return em.createQuery(
"SELECT f FROM Feed f WHERE f.date BETWEEN :start AND :end")
.setParameter("start",
new LocalDate()
.toDateMidnight()
.toDate(),
TemporalType.DATE)
.setParameter("end",
new LocalDate().plusDays(daysFromToday)
.toDateMidnight()
.toDateTime()
.minusMillis(1)
.toDateTime()
.toDate(),
TemporalType.DATE)
.getResultList();
}
I am looking for some thing like this.
public class User
{
private Date createdOn;
}
Now i need a query to fetch users created on day 10th of any month or year.
Like i need users created on 10th Jan 2012, 10th Feb 2012, 10th June 2010.....
How can i get part of a date in gae query?
Thanks,
Ramesh.V
Just create a date object for whatever date you're looking for and use it as part of the query. There's nothing special about Dates in queries.
Not sure if you're using JDO, JPA or the datastore directly, but either way
See the GQL reference (not really python specific): http://code.google.com/appengine/docs/python/datastore/gqlreference.html
SELECT * FROM User WHERE createdOn == :date
In JDO it would look something like:
Query query = pm.newQuery(User.class);
query.setFilter("createdOn == dateParam");
List<User> results = (List<User>) query.execute(date);
For your filter, you could use:
"(createdOn >= p1) && (createdOn <= p2)"
where you have set the parameters p1 and p2 to be the start and finish Dates of the day in question.