I have a Query to parse on JPA , some errors have happened . I want a right way to make this.
Original:
Query q = this.em.createQuery("SELECT e FROM Entity e WHERE e.codigo = :codigo ORDER BY e.data ASC");
My solution but doesn't work:
Query q = this.em.createQuery("SELECT e FROM Entity e WHERE e.codigo = :codigo ORDER BY to_date(e.data,'DD/MM/YYYY') ASC");
where e.data is a String like "01/01/2014"
Solved
I´d replaced it with a nativeQuery.
Then a had be able to use Oracle Date Function
Related
String q = "select id from Calendar c " +
"where c.isActive = 1 and " +
"date_part('dow', '2017-09-19 13:23:23'::date) = c.frequencyValue)";
Query query = em.createQuery(q);
List results = query.getResultList();
If I include ::date, hibernate would complain because : conflicts with parameter, but if I don't, postgres will complain. Could not choose a best candidate function. You might need to add explicit type casts. What can I do?
https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-expressions
as specified extract function should work if the underlying db supports them so:
extract(dow from date '2017-09-19 13:23:23');
should works
I want filter by Time in jpql but I think that I´m not doing well.
SELECT e FROM Pedido e WHERE e.fechaEntrega = :fechaInicio AND e.horaEntrega < :horaEntrega and que.setParameter("horaEntrega", horaEntrega, TemporalType.TIME); but when i see return this not filter by horaEntrega. I'm using eclipselink 2.5 any idea???
I tryed use SELECT e FROM Pedido e WHERE e.fechaEntrega = :fechaInicio AND CAST(e.horaEntrega AS TIMESTAMP) < :horaEntrega and doesn´t work and if i try cast to Time says me that expected NUMBER and got DATE
It´s weird when I write SELECT in sql im using cast(cast(etretst as timestamp) as time) < '08:00:00' and this works fine. And when I write this say me that expected TIME not a DATE
I resolved SELECT SELECT e FROM Pedido e WHERE e.fechaPedido = :fechaInicio AND CAST(CAST(e.horaPedido AS TIMESTAMP) AS TIME) < :horaZona and parameter is que.setParameter("horaZona", new Time(horaPedido.getTime()).toString());
I started to work with eclipse link and from what I read in the net the eclipse link is some kind of JPA implementation and you are not working directly with DB code when you are using eclipse link you are working with objects.
My question is what is this if not DB command?
Query q = em
.createQuery("SELECT p FROM Person p WHERE p.firstName = :firstName AND p.lastName = :lastName");
It's JPQL. You can find more here
Its JPA query equivalent to SELECT * FROM PERSON WHERE FIRSTNAME='Stefan' and LASTNAME='Strooves'; further you need to set value for parameters.
Query q = em
.createQuery("SELECT p FROM Person p WHERE p.firstName = :firstName AND
p.lastName = :lastName");
q.setParameter("firstName", "Stefan");
q.setParameter("lastName", "Strooves");
List<Person> resultList = q.getResultList();
Result list contains all the Person entities matched with query.
I am somewhat new to jpa. I have created a few simple queries. But now i have a problem with a simple query. I have an entity Payment with 3 columns(Id,Amount1,Amount2).
Now i want to find all the rows from Payment where Amount1+Amount2 is greater than somevalue.I tried something like:
Query query = getEntityManager().createQuery(
"Select p from Payment p WHERE p.Amount1 + p.Amount2 >' " + someamount +" ' ");
But it is not working. I tried using SUM also but that is only one column.
Can anyone help me with this.
Thanks.
Assuming your entity is properly defined, the following query should work just fine:
final BigDecimal threshold = BigDecimal.valueOf(1000);
TypedQuery<Payment> query = getEntityManager().createQuery(
"SELECT p FROM Payment p WHERE p.Amount1 + p.Amount2> :value",
Payment.class);
query.setParameter("value", threshold);
List<Payment> results = query.getResultList();
I try to delete a list of rows from a table using this Native Query:
#NamedNativeQuery(name="WebGroup.DeleteIn",
query="DELETE FROM WebGroup WHERE
WebGroup.GROUP_ID IN (:IDsList)"
getEm().createNamedQuery("WebGroup.DeleteIn")
.setParameter("IDsList", groupToDeleteIDs)
.executeUpdate();
and this is the SQL that MySQL executes:
DELETE FROM WebGroup WHERE WebGroup.GROUP_ID IN (:IDsList)
SO, JPA doesn't replace the variable IDsList...
Some one could help me please?
One way that works is if you not use the id value like you tried, but instead use the entity and let JPA handle the identification of it like this:
HashSet<Transaction> transactions = new HashSet<Transaction>();
...
entityManager.createQuery(
"DELETE FROM Transaction e WHERE e IN (:transactions)").
setParameter("transactions", new ArrayList<Transaction>(
transactions)).executeUpdate();
Hope it helps you in the right direction.
native queries do not support collection expansion neither named parameters.
you should write:
#NamedNativeQuery(name="WebGroup.DeleteIn", query="DELETE FROM WebGroup WHERE WebGroup.GROUP_ID IN (?,?,?,?)"
Query query = getEm().createNamedQuery("WebGroup.DeleteIn");
for(int i = 0; i < 4; i++) query.setParameter(i + 1, groupToDeleteIDs.get(i));
query.executeUpdate();
but it is horrible
on eclipselink + mysql this one works:
#NamedNativeQuery(name="WebGroup.DeleteIn", query="DELETE FROM WebGroup WHERE WebGroup.GROUP_ID IN (?)"
Query query = getEm().createNamedQuery("WebGroup.DeleteIn");
query.setParameter(1, StringUtils.join(groupToDeleteIDs, ",");
query.executeUpdate();
however it is not very nice...
but there isn't any other solution using a named query.