Hibernate Query doesn't work as expected - sql

Hi i have the following Query:
String hql = "UPDATE Raumreservierung as rr " +
"set VON = :begin " +
"where VON = :Von " +
"and Raum_ID IN (SELECT r.ID FROM Raum r " +
"inner join r.Panel as pl with pl.ID = " + clientId + "";
IQuery query = CurrentSession.CreateQuery(hql);
query.SetParameter("begin", DateTime.Now);
query.SetParameter("Von", v.Von);
int result = query.ExecuteUpdate();
The Query do an Update on "VON". That works fine, but the rest of the Query is not working. It seems that the rest of the query is not working. But did not get any Error.
With the rest of the Query i mean the following part of the query:
"and Raum_ID IN (SELECT r.ID FROM Raum r " +
"inner join r.Panel as pl with pl.ID = " + clientId + "";
Because it should happen only a Update on the column "VON" for example when "clientId" is "AT2"
But that part is not working. Because the update happens also on other clientId.

You forgot to close your parentheses.
(Also, you should use a parameter for clientId too)

Related

Running HQL query in console

I am trying to run the following query in IntelliJ:
#Query(value = "" +
"SELECT offer FROM OfferEntity offer " +
" JOIN offer.placeOwnership AS owner " +
" JOIN owner.place AS place " +
"WHERE " +
" place.id = :placeId AND " +
" offer.dayFrom = :offerDate AND " +
" offer.repeating = false")
as I am doing so, I get ask to provide the parameters placeId and offerDate. My problem is I have no idea about the latter.
I have no idea how I can run this query.
I also tried to run it manually inside the console like this:
SELECT offer FROM OfferEntity offer
JOIN offer.placeOwnership AS owner
JOIN owner.place AS place
WHERE
place.id = 1L AND
offer.dayFrom = java.time.LocalDate.parse("2018-10-08") AND
offer.repeating = false
but that gives my a syntax error.

#query with named #param returning empty result set when using sql IN

Btw this is already working with a jdbc template based solution that I wrote. It's not me wanting to do the rewrite, it is the tec lead's idea. It is giving me a headache.
Lets not get into the discussion on why this is or isn't a good idea, but rather how to make it work.
in my repo
#Repository
interface Foo...extends JpaRepository
this works:
#Query(value = "SELECT t.td, em.ct, j.dsc AS bs, ct.dsc AS ct_name, "
+ "t.tn, et.dsc, f.f_id, f.f_life, f.fl_name, em.f_note "
+ "FROM ext_master em join tax t on em.td = t.td "
+ "join bs j on j.bs = t.bs "
+ "join ex_type et on em.ex_type = et.ex_type "
+ "join ct ct on ct.ct = em.ct "
+ "left join ex_f ef on em.td = ef.td and em.ct = ef.ct "
+ "left join f f on ef.f_id = f.f_id "
+ "WHERE em.ct in ( 'CC' ) "
+ "AND t.state IN ('FF','AK','AL','GA')"
+ "ORDER BY bs, t.tn", nativeQuery = true)
List<Object> getExtensionsByCustTypeAndState(#Param("custTypes") String custTypes, #Param("states") String states);
this doesn't returns empty result set
#Query(value = "SELECT t.td, em.ct, j.dsc AS bs, ct.dsc AS ct_name, "
+ "t.tn, et.dsc, f.f_id, f.f_life, f.fl_name, em.f_note "
+ "FROM extension_master em join tax t on em.td = t.td "
+ "join bs j on j.bs = t.bs "
+ "join exemption_type et on em.ex_type = et.ex_type "
+ "join ct ct on ct.ct = em.ct "
+ "left join ex_f ef on em.td = ef.td and em.ct = ef.ct "
+ "left join f f on ef.f_id = f.f_id "
+ "WHERE em.ct in ( :custTypes ) "
+ "AND t.state IN ('FF', :states)"
+ "ORDER BY bs, t.tn", nativeQuery = true)
List<Object> getExByCustTypeAndState(#Param("custTypes") String custTypes, #Param("states") String states);
both can have one or multiple values, I tried:
with both params like {:states} :#{#states}
with the value inside the param
surrounded by " or by '
passing the whole line in the parameter like "AND t.state IN ('FF','AK','AL','GA')"
passing the 'FF','AK','AL','GA' in the states param and subtituting the in the query with
+ "AND t.state IN ( :states)"
I did write a simple query to make sure I am getting the parameters passed in right. so that's working.
Can anyone write this right, so that the in statements work, or tell me why it is not possible.
Thank you.
You pass all the possible values in one parameter that's why you don't get the results you are expecting.
You have to change the where clause like this:
"WHERE em.ct in ( :custTypes1, :custTypes2, :custTypes3 .... :custTypesN) "
this ended up being the simplest solution
instead of :
"WHERE em.ct in ( :custTypes ) "
+ "AND t.state IN ('FF', :states)"
do:
"WHERE em.ct in :custTypes "
+ "AND t.state IN :states "
and add the FF to the states string on before I call the function.

Springboot Hibernate Query: filter by childrens attributes

Hi I have a problem generating a #query in my SpringBoot repo. Using hibernate.
My POJOs:
class Offer
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
#ManyToMany
private List<Attribute> attributes;
#ManyToMany
private List<Offer> offers;
and
class Attribute
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
It works this way to get the "offer.offers by attribute". But now I want to enhance my query to get "offer offer.offers.attribute" an I strugle with it.
What I want is to find all Offers which have "Offer.attribute == 1" AND "all Offer.offers.attribute == 2"
Ending with this query:
#query
"SELECT o FROM Offer o "
+ "JOIN o.attributes a "
+ "LEFT OUTER JOIN o.offers oo "
+ "LEFT OUTER JOIN o.offers.attributes ooa " <-- Problem issue
+ "WHERE o.status = 1 "
+ "AND oo.status = 1 "
+ "AND a.id = 100 "
+ "AND ooa.id = 101 "
+ "GROUP BY o.id "
But upon compilation I get this exception:
org.hibernate.QueryException: illegal attempt to dereference collection
[offer0_.id.offers] with element property reference [attributes]
Now I wonder how to do it correctly?
Hope some SQL/Hibernate hero can help me.
Thanks in advance!
Gregor
Well... Iam one step further:
this query works:
#query
"SELECT o FROM Offer o "
+ "JOIN o.attributes a "
+ "LEFT OUTER JOIN o.offers oo "
+ "LEFT OUTER JOIN oo.attributes ooa " <-- referencin JOIN
+ "WHERE o.status = 1 "
+ "AND oo.status = 1 "
+ "AND a.id = 100 "
+ "AND ooa.id = 101 "
+ "GROUP BY o.id "
But now I get all results with at least on "Offers.offer.attributes == 101"
But I need all offers.offer having that attribute.
Any hints how to continue?

native query with spring data jpa doesn't return result

Hello I have written the following method in spring data jpa repo:
#RestResource(exported = false)
#Query(
value = "select q.* " +
"from question q " +
"left join question_program qp " +
"on q.question_id = qp.question_id " +
"where " +
"q.difficulty_level_id = ?1 " +
"and " +
"q.paid = false " +
"and " +
"qp.program_id = ?2 " +
"order by random() " +
"limit ?3",
nativeQuery = true
)
List<Question> getFreeRandomQuestions(
#Param("df") Integer df,
#Param("programId") Integer programId,
#Param("qs") Integer qs);
It is returning an empty list whereas when I run the same query in pgadmin I get all the desired rows. It also works in spring data jpa if I remove the join with question_program. What am I doing wrong?
UPDATE:
I have tried select q ... and select * .... Both have the same problem
UPDATE:
I kind of solved this by implementing a service which fetches that data using sessionFactory and raw sql query!.

Syntax error on multiple join statement

Hello, I'm getting a syntax error on this sql statement, can anyone advise thanks
String sql = "Select tblStudent.*,tblSchool.*,tblAgents.* " +
"FROM tblStudent LEFT JOIN tblSchool " +
"ON (tblStudent.schoolID = tblSchool.schoolID) " +
"LEFT JOIN tblAgents " +
"ON (tblStudent.agentID = tblAgents.agentID) " +
"WHERE tblStudent.StudentID='" + studentID + "'";
I was hoping that I could do multiple joins
But I am getting a syntax error.
For access, parenthesis with multiple joins means the following. If you have three joins, there are two left parenthesis after the from. The last join does not have a right parenthesis.
String sql = "Select tblStudent.*,tblSchool.*,tblAgents.* " +
"FROM (tblStudent LEFT JOIN tblSchool " +
"ON (tblStudent.schoolID = tblSchool.schoolID)) " +
"LEFT JOIN tblAgents " +
"ON (tblStudent.agentID = tblAgents.agentID) " +
"WHERE tblStudent.StudentID='" + studentID + "'";
Access SQL injection has been covered in other threads .
String sql = "Select
tblStudent.StudentID,tblStudent.studentFirstName,"+
tblStudent.studentLastName,tblSchool.schoolName," +
tblAgents.agentFirstName,tblAgents.agentLastName " +
"FROM (tblStudent LEFT JOIN tblSchool " +
"ON (tblStudent.schoolID = tblSchool.schoolID)) " +
"LEFT JOIN tblAgents " +
"ON (tblStudent.agentID = tblAgents.agentID) " +
"WHERE tblStudent.StudentID=#studentID";
I believe your final SQL should look like this:
SELECT tblStudent.*
,tblSchool.*
,tblAgents.*
FROM tblSchool
RIGHT JOIN (
tblAgents RIGHT JOIN tblStudent ON tblAgents.agentID = tblStudent.agentID
) ON tblSchool.schoolID = tblStudent.schoolID
WHERE tblStudent.StudentID=111;
So, the VBA code for creating this SQL should be
Dim sql As String
sql = "SELECT tblStudent.* ,tblSchool.* ,tblAgents.* " & _
"FROM tblSchool RIGHT JOIN (" & _
"tblAgents RIGHT JOIN tblStudent ON tblAgents.agentID = tblStudent.agentID" & _
") ON tblSchool.schoolID = tblStudent.schoolID " & _
"WHERE tblStudent.StudentID=" & studentID
Here I assume that studentID is numeric field. Also I would recommend to do not use * for selecting the data from more than one table, otherwise column names may be unpredictable and as mentioned in comments, it will require additional resources, which won't be used.