I have a groovy class that is responsible for running a query against a sql database table and extracting some data from the results to build a DTO. The database column I am interested in is called 'release_date' is of type (date,null). I was expecting the results to return a java.util.Date object but instead I am getting a java.lang.String. The strange thing is, if I change the type of the column to a datetime it seems to work correctly.
Is this expected behaviour from groovy? Is there a way to get it to return a Date object without having to change the column of the database?
My Groovy Code:
Sql opticsSql=myDb;
def run(Map<Long, OriginalDateDTO> dtoMap) {
def query = """select p.id,p.release_date as planned from releases p; """;
opticsSql.eachRow(query.toString()) { val->
Long id = val.id;
Date planned = val.planned;
Map localMap = dtoMap;
Boolean hasKey = localMap.containsKey(id);
if(hasKey) {
dtoMap.get(id).originalPlannedDate = planned;
}
}
}
The column "mobile" does indeed exists in my table. I am running mySQL database version 5.8+
Here is my java code that throws the exception:
Where query =
dao.queryBuilder().selectColumns("mobile").where().eq(ACCOUNT,
account).and().ge(DATE, startStamp).and().le(DATE, endStamp);
the selectColumns only takes strings or iterator
If i run the query below on my database. i get the desired results:
SELECT mobile FROM account_profile_metric_usage WHERE ((account = 'qa23-redis-smoke' AND date >= '2017-01-28' ) AND date <= '2017-02-27' )
If i run the query below it fails to return the results since mobile is in quotes. Which is not what i want.
SELECT 'mobile' FROM account_profile_metric_usage WHERE ((account = 'qa23-redis-smoke' AND date >= '2017-01-28' ) AND date <= '2017-02-27' )
I need to be able to run the first SQL query in ormLite. mobile is of type bigInt
I have an ADD form which adds name, start date and end date into a database. What I want is to make a query which satisfies that if another date is entered for the same name, it will return false. Any suggestions please?
This is what I've tried, but it fails:
DetachedCriteria criteria = createCriteria();
criteria.add(Restrictions.ge("start_date", startDate));
criteria.add(Restrictions.lt("end_date", endDate));
criteria.add(Restrictions.eq("name", name));
I am trying to pull information from a table where the current date is between the first and last day of any given month.
I am getting a runtime error "Errors in named queries: Department.byDate"
I am providing you with what code I think could be causing the problem, if any additional code is needed please let me know in a comment.
My named query which looks like this:
#NamedQuery(name="Department.byDate", query="select * from department where date >= :first AND date <= :last")
I am using this named query in my DAO in a method which looks like this:
public List<Department> getRecords(Date dateFirst, Date dateLast){
Session session= sessionFactory.openSession();
session.beginTransaction();
Query query = session.getNamedQuery("Department.byDate");
query.setDate("first", dateFirst);
query.setDate("last", dateLast);
List<Department> depList = (List<Department>)query.list();
session.getTransaction().commit();
session.close();
return depList;
}
My method of getting that first and last days of the months looks like this:
Calendar first = Calendar.getInstance();
first.set(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().getActualMinimum(Calendar.DAY_OF_MONTH));
Date dateFirst = first.getTime();
Calendar last = Calendar.getInstance();
first.set(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH));
Date dateLast = last.getTime();
In HQL/JPQL you are working with entities and their properties, thus * character has no meaning.
HQL/JPQL class and property names are case sensitive.
You should write your query the following way:
select d from Department d where d.date >= :first AND d.date <= :last
I want to extract the year part from a row in the database in order to compare it with a value.
Here's my function
public List<Dossier> getAllDossierParAn() {
Date date = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
int strI = calendar.get(Calendar.YEAR);
TypedQuery<Dossier> query;
query = em.createQuery("SELECT d FROM DOSSIER d WHERE EXTRACT(YEAR ,d.dateCreation)=2015", Dossier.class);
System.out.println(strI);
return query.getResultList();
}
I get always
An exception occurred while creating a query in EntityManager:
Exception Description: Problem compiling [SELECT d FROM DOSSIER d WHERE EXTRACT(YEAR FROM d.dateCreation)=2015]. [14, 21] The abstract schema type 'DOSSIER' is unknown. [48, 62] The state field path 'd.dateCreation' cannot be resolved to a valid type.
I test it directly in the database and it works
select * from dossier where extract(year from date_creation)=2015
I'm using jpa and ejb and jdeveloper as IDE.
First, the main problem with your query is what the error message say:
The abstract schema type 'DOSSIER' is unknown
Since JPA is mapping your POJOs as entities, their names are case sensitive. Your query should be:
SELECT d FROM Dossier d WHERE ...
Also, regarding the problem you mentioned, the EXTRACT function is only supported by EclipseLink, as far as I know. By the error message, I think this is your JPA implementation, but if it's not, there are two options:
If you're using Hibernate, it has built in functions for retrieving date parts, such as YEAR(date), MONTH(date), DAY(date), HOUR(date), MINUTE(date) and SECOND(date).
For any other JPA implementation, or if you want to keep it JPQL compliant, you can workaround with SUBSTRING: SUBSTRING(d.dateCreation, 1, 4). Note the first position of a string is denoted by 1;
Hope it helps
"Directly in the database" is called SQL.
createQuery takes in JPQL not SQL, and YEAR / EXTRACT are invalid keywords (though YEAR, but not EXTRACT, is supported by some JPA providers). Any decent JPA docs would spell that out.
thank you guys i solved the problem:
first i got the current year than format it to int than to String in order to do the comparaison and substract it here's my code it work fine:
public List<Dossier> getAllDossierParAn() {
Date date = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
int strI = calendar.get(Calendar.YEAR);
String strInt =Integer.toString(strI);
String nvlstri= strInt.substring(2, 4);
TypedQuery<Dossier> query;
query = em.createQuery("SELECT d FROM Dossier d WHERE SUBSTRING(d.dateCreation, 7, 2) = :vr", Dossier.class);
query.setParameter("vr",nvlstri);
System.out.println("l anne est " +strI);
System.out.println("la date formaté " +nvlstri);
return query.getResultList();
}
On eclipselink, the way that works for me was something like:
SELECT a.id, EXTRACT(WEEK CURRENT_DATE ) FROM Account a
This works on postgres and sql server at least but should work with other supported databases too.
From javadocs the syntax supported is :
extract_expression ::= EXTRACT(date_part_literal [FROM] scalar_expression)
The FROM seems to cause funny exceptions on sql server.