Below is my code inside a method named getUrlDetails() of WebsiteAvailability class
inside the method: getUrlDetails(String selUrl)
List<WebsiteAvailability>avaList=new ArrayList<WebsiteAvailability>
String selquery="select w.statusCode,w.updateTime,s.statusCodeValue from WebsiteAvailability w,StatusCodes s where w.statusCode=s.statusCodeNo and w.url=?";
avaList=session.createQuery(selquery).setString(0,selUrl).list();
Here this is hibernate java application.And StatusCode and WebsiteAvailability are POJO classes(generated by hibernate).So in HQL query I am using entity classes instead of database table names.I am using postgresql.
My problem is I am not getting the list of items which match where condition.In place of ?(placeholder) it is not taking supplied input.It is taking ? itself
Can someone please help me..
Use setString(1, ...)
EDIT: No, it is setString(0, ...)
You can try using a named parameter:
String selquery="select w.statusCode,w.updateTime,s.statusCodeValue from WebsiteAvailability w,StatusCodes s where w.statusCode=s.statusCodeNo and w.url=:selUrl";
avaList=session.createQuery(selquery).setString("selUrl",selUrl).list();
Related
Using Jooq, I am trying to fetch from a table by id first, if no matches found, then fetch by handle again.
And I want all fields of the returned rows, not just one.
Field<?> firstMatch = DSL.select(Tables.MY_TABLE.fields())
.from(Tables.MY_TABLE.fields())
.where(Tables.MY_TABLE.ID.eq(id))
.asfield(); // This is wrong, because it supports only one field, but above we selected Tables.MY_TABLE.fields(), which is plural.
Field<?> secondMatch = DSL.select(Tables.MY_TABLE.fields())
.from(Tables.MY_TABLE.fields())
.where(Tables.MY_TABLE.HANDLE.eq(handle))
.asfield(); // Same as above.
dslContext.select(DSL.coalesce(firstMatch, secondMatch))
.fetchInto(MyClass.class);
Due to the mistake mentioned above in the code, the following error occurs:
Can only use single-column ResultProviderQuery as a field
I am wondering how to make firstMatch and secondMatch two lists of fields, instead of two fields?
I tried
Field<?>[] secondMatch = DSL.select(Tables.MY_TABLE.fields())
.from(Tables.MY_TABLE.fields())
.where(Tables.MY_TABLE.HANDLE.eq(handle))
.fields();
but the following error occurred in the line containing DSL.coalesce
Type interface org.jooq.Field is not supported in dialect DEFAULT
Thanks in advance!
This sounds much more like something you'd do with a simple OR?
dslContext.selectFrom(MY_TABLE)
.where(MY_TABLE.ID.eq(id))
// The ne(id) part might not be required...
.or(MY_TABLE.ID.ne(id).and(MY_TABLE.HANDLE.eq(handle))
.fetchInto(MyClass.class);
If the two result sets should be completely exclusive, then you can do this:
dslContext.selectFrom(MY_TABLE)
.where(MY_TABLE.ID.eq(id))
.or(MY_TABLE.HANDLE.eq(handle).and(notExists(
selectFrom(MY_TABLE).where(MY_TABLE.ID.eq(id))
)))
.fetchInto(MyClass.class);
If on your database product, a query using OR doesn't perform well, you can write an equivalent query with UNION ALL, which might perform better.
I'm trying to make custom spring Data query using field from reference as query
parameter. When i performed this query via REST i had no results at all:
I enabled hibernate logs and copy it to H2 console and I got many results:
My Flight and Airport entities are very simple
So what am I doing wrong? I have other simpler methods and they work fine using browser. I tried the same using Query annotation(second method on second screen), but I got same results. (Copied generated sql query to H2 console and received many results but it didn't work via REST).
EDIT
I found the solution. I just removed quotation marks from URL and
it works well :D
Sorry for my inattention.
Regards,
Michal
I found the solution. It was very simple. Basically I just removed quotation marks from URL and it works well :D
Actual method in Repository looks like:
List findFlightsByFromCityAndToCity(#Param("from") String from, #Param("to") String to);
Regards,
Michal
I was originally doing a single variable query and I had to add in another variable to my statement and can't seem to get it to work correctly or find a straight answer on the expected syntax. I've already checked everything is mapped correctly and nothing null is going into the query.
Here's what I'm attempting to do which isn't working correctly
return getHibernateTemplate().find("from Pricing_Data where rate_class=?", rate_class + "where utility=?", utility);
Orginally I had it as this and that was working as expected
return getHibernateTemplate().find("from Pricing_Data where rate_class=?", rate_class);
Try this:
return getHibernateTemplate().find("from Pricing_Data where rate_class=? AND utility=?", new Object[]{rate_class, utility});
Hibernate Find method
Try to use query
from Pricing_Data where rate_class = ? and utility = ?
Then setParameter for rate_class and utility.
SQL/HQL queries can have second (third, etc) where clause only in subqueries.
I am using the webservice that will send the request for one of the column as Dran & Hyle , but i get the exception as a expected valid begining name character. due to the special character &
Below is the insert statement in my java .
public static final String PetInsert= insert into pet values(?,?,?);
I believe set define off will not work in java code , it is understood only by sql developer.
Any help is appreciated
From Java, it is recommended to use PreparedStatement when creating a statement to query the database. Read more in the documentation.
Not so sure, but can it be that in the XML of the web service the error is located? Then somewhere
"Dran & Hyle"
should go into the XML. Normally it is done automatically. So it would be the unlikely case of creating the XML oneself with Strings.
In that case use apache's StringEscapeUtils:
s = StringEscapeUtils.escaleXML(s);
P.S. I found set define off of #aUserHimself plausible.
I am working with a 3rd product called JPOS and it has an XMLPackager whereby I get a string from this packager that contains a record in an XML format such as:
<MACHINE><B000>STRING_VALUE</B000><B002>STRING_VALUE</B002><B003>STRING_VALUE</B003><B004>STRING_VALUE</B004><B007>STRING_VALUE</B007><B011>STRING_VALUE</B011><B012>STRING_VALUE</B012><B013>STRING_VALUE</B013><B015>STRING_VALUE</B015><B018>STRING_VALUE</B018><B028>STRING_VALUE</B028><B032>STRING_VALUE</B032><B035>STRING_VALUE</B035><B037>STRING_VALUE</B037><B039>STRING_VALUE</B039><B041>STRING_VALUE</B041><B043>STRING_VALUE</B043><B048>STRING_VALUE</B048><B049>STRING_VALUE</B049><B058>STRING_VALUE</B058><B061>STRING_VALUE</B061><B063>STRING_VALUE</B063><B127>STRING_VALUE</B127></MACHINE>
I have a SQL server table that contains a column for each of the listed. Not that it matters but I could potentially have thru defined with specific STRING_VALUEs. I'm not sure what is the best way to go about this in Java. My understanding is that SQL Server can take an XML string (not document) and do an insert. Is it best to parse each value and then put into a list that populate each value into? This is the first time I've used an XML file and therefore trying to get some help/direction.
Thanks.
Sorry, one of my colleagues was able to help and provide a quick answer. I'll try it from my Java code and it looks like it should work great. Thanks anyway.
Here is the SP that she created whereby I can pass in my XML string and bit value:
CREATE PROCEDURE [dbo].[sbssp_InsertArchivedMessages]
(
#doc varchar(max),
#fromTo bit
)
AS
BEGIN
DECLARE #idoc int, #lastId int
EXEC sp_xml_preparedocument #idoc OUTPUT, #doc
INSERT INTO [dbo].[tblArchivedMessages]
SELECT * FROM OPENXML(#idoc, '/MACHINE', 2) WITH [dbo].[tblArchivedMessages]
SET #lastId = (SELECT IDENT_CURRENT('tblArchivedMessages'))
UPDATE [dbo].[tblArchivedMessages]
SET FromToMach = #fromTo
WHERE ID = #lastId
END
GO
Regards.