Following code getting only projectId = 1 records but also wish account null ones. I am missing something?
#Query(value = "SELECT M FROM Message M WHERE M.account IS NULL OR M.account.project.id = 1")
List<Message> findMessages();
this one gets true result.
#Query(value = "SELECT M FROM Message M WHERE M.account IS NULL")
List<Message> findMessages();
also true result
#Query(value = "SELECT M FROM Message M WHERE M.account IS NULL OR M.account.id = 1")
List<Message> findMessages();
Thank you.
I think it is JPQL null issue with join.
This is alternative solution , fixed and applied for my other complex script.
Anyway appreciated if explain the base reason of this issue.
#Query(value = "SELECT M FROM Message M LEFT JOIN M.account A WHERE M.account IS NULL OR A.project.id = 1")
List findMessages();
Related
I have a simple query as follows. I get the expected result if I hard code the id value as follows. But it throws IllegalArgumentException exception if I try to get the value from the Param instead. Note that I have tried to use the Param as both long and String and still the same results. Please advise what I am doing wrong. Thanks.
My Query
public interface FeedDetailRepository extends JpaRepository<FeedDetail, Long> {
#Query("select fd.message from FeedDetail as fd where fd.feedId =: id")
String custom(#Param("id") long id);
}
At Controller, if I run the following, I get an exception.
#GetMapping("/something/{id}")
public String getDetail(#PathVariable long id){
return feedDetailRepository.custom(id);
}
But if I hard code the id value as follows, I get the wanted result.
public interface FeedDetailRepository extends JpaRepository<FeedDetail, Long> {
#Query("select fd.message from FeedDetail as fd where fd.feedId = 4")
String getDetailBasedOnFeedId(#Param("id") long id);
}
The exception
nested exception is java.lang.IllegalArgumentException:
org.hibernate.QueryException: Named parameter not bound : id
I would change
#Query("select fd.message from FeedDetail as fd where fd.feedId =: id")
To (difference lies in space)
#Query("select fd.message from FeedDetail as fd where fd.feedId = :id")
This is a small difference for you but big for Spring. He recognizes a parameter by attaching name to colon like that
:id
For more details refer to the official Spring Data JPA Reference.
I had the same issue:
Wrong query:
SELECT * FROM `cars` as c where c.driver_one = :id OR c.driver_two = **:id;**
Correct query:
SELECT * FROM `cars` as c where c.driver_one = :id OR c.driver_two = **:id**
This is likely because of the way you have provided space with in the query.
My suggestion would be to format the code and in the string quotes use something similar
SELECT fd.message from FeedDetail AS fd where fd.feedId = :id
Here municipalities parameter can be null. but IN clause doesnt work for null. If there was only one paramater i.e municipalities, I could have handle this in java level. but there is another condition in OR, which has to be executed.
List<Municipality> municipalities = myDao.findAll(); // returns empty list
em.createQuery("SELECT p FROM Profile p JOIN p.municipality m WHERE m IN (:municipalities) OR m.city = :city")
.setParameter("municipalities", municipalities)
.setParameter("city", city)
.getResultList();
So how to do this? Can we handle null in JPA IN clause. like if it is null then dont execute that condition
you can modify the query
String sql="";
if(municipalities!=null){
sql="SELECT p FROM Profile p JOIN p.municipality m WHERE m IN (:municipalities) OR m.city = :city"
}
else
{
sql="SELECT p FROM Profile p JOIN p.municipality m WHERE m.city = :city"
}
List<Municipality> municipalities = myDao.findAll(); // returns empty list
em.createQuery(sql)
.setParameter("municipalities", municipalities)
.setParameter("city", city)
.getResultList();
I have named queries which looks like the following:
#NamedQueries({
#NamedQuery(name = "table.getvalues", query = "select p from table p where p.a = :a and p.b = :b and p.c = :c order by id"),
#NamedQuery(name = "table.getvalueswhencisnull", query = "select p from table p where p.a = :a and p.b = :b and p.c is null order by id")
})
The only difference between 2 named queries is the the value of the c,c can be null and the syntax of the sql query differs only because of that.
Is there a way where I can club both the statements effectively?
use this :
select p from table p where p.a = :a and p.b = :b and (p.c is null or p.c=:c) order by id
Maybe this filter does a conditional selection.
...AND (p.c=:c OR (:c IS NULL and p.c IS NULL))...
I try to make a namedQuery:
#NamedQuery(name = "Interval.findByMemoryType",
query = "select i from Interval i JOIN i.intervalDatas id "
+ "where id.fragments.memoryType = :memoryType")
My problem is, that fragments is a list of fragment. I'm only interested in memory type of first element in the list.
So I should have something like this:
#NamedQuery(name = "Interval.findByMemoryType",
query = "select i from Interval i JOIN i.intervalDatas id "
+ "(select first(id.fragments)) as fid) where fid.memoryType = :memoryType")
But I get always “The query contains a malformed ending” problem.
Could somebody help me??
You can take the first result:
TypedQuery<Interval> q = em.createQuery ("Interval.findByMemoryType", Interval.class);
q.setParameter("memoryType", memoryType);//+other parameters if you have
Interval interval = q.getSingleResult();
The small disadvantage is that it may load all its intervalDatas (depending on the mapping). Also check the documentation for possible exceptions.
I have got the following problem, maybe somebody can help me.
I wrote a SQL query:
SELECT v.bezeichnung, count(*) as total
FROM veranstaltung v
JOIN auffuehrung a ON v.id = a.veranstaltung_id
JOIN platz p ON p.auffuehrung_id = a.id
JOIN transaktion t ON t.id = p.transaktion_id
WHERE t.status = 2 AND (a.datumuhrzeit + 30 DAY) >= CURRENT_DATE
GROUP BY v.bezeichnung
ORDER BY total DESC
I have to implement it in JPA but everything works except GROUP BY... it can't be grouped..
CriteriaBuilder builder = this.entityManager.getCriteriaBuilder();
CriteriaQuery<Veranstaltung> query = builder.createQuery(Veranstaltung.class);
Root<Veranstaltung> rootVeranstaltung = query.from(Veranstaltung.class); query.where(builder.equal(rootVeranstaltung.join("auffuehrungen").join("plaetze").join("transaktion").<Transaktionsstatus>get("status"), Transaktionsstatus.BUCHUNG));
query.groupBy(rootVeranstaltung);
List<Veranstaltung> result = this.entityManager.createQuery(query).getResultList();
return list;
I haven't finished the code yet, e.g. the COUNT..
should i write COUNT already before GROUP BY or would it work without COUNT?