I am trying to write a hibernate query which selects a number of records from a table based on some criteria.
I have two relevant tables in my database
tbleventattendees which has the following fields - eventAttendeeRecord tblevent, tblmembers, and memberComments;
tblevent which has the following fields relevant field eventID which is the tblevent foreign key in tbleventattendees
What I am trying to do is write a HQL query which shows the attendees the attendees at specific event e.g. something like this SQL query select * from tbleventattendees where tblevent = 1
However, whilst this works as an SQL query in MySQL workbench when I try from tbleventattendees where tblevent = 1 in Netbeans run HQL Query I get the following error
org.hibernate.QueryException: Incorrect query syntax [ FROM
Society.Tbleventattendees as attendees where Tblevent =1
]
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:259)
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:209)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1796)
Caused by: java.lang.NullPointerException
at org.hibernate.sql.QuerySelect.appendTokens(QuerySelect.java:185)
at org.hibernate.sql.QuerySelect.setWhereTokens(QuerySelect.java:103)
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.renderSQL(QueryTranslatorImpl.java:625)
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:243)
... 9 more
I think this is because I am using the value in the database in the query rather than the object reference as when I remove the =1 criteria I get the following value in that column
Society.Tblevent#6a2437ae
Can someone please help, I have read through lots of posts on here but struggling to work out the answer
You can write somthing like this:
Query query = session.createQuery("from Tbleventattendees attendees where attendees.tblevent.eventID = :tbleventId ");
query.setParameter("tbleventId", "1");
List list = query.list();
Related
I have a script, which I run successfully in postgres db, however it was failed when I run in hsqldb.
Can someone help me change this sql to make it work for both HSQLDB and Postgres DB?
Below is my script:
UPDATE tableA af
SET columnA2 = b.columnB2
from
( select columnB1, columnB2 from.....) as b
Where af.columnA1 = b.columnB1;
This throws the following exception when I run it in hsqldb:
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or
object not found: b.columnB2 Caused by: org.hsqldb.HsqlException: user
lacks privilege or object not found: b.columnB2
Thanks.
Updated:
I created another view to make my question more clear.
table_A has 2 columns: company_code, company_number
and view_B has 2 columns: company_code, company_number_correct_answer
table_A has 10000 rows, and view_B has only 2 rows.
What I want is updating 2 record in table_A, with company_code existing in view_B and set table_A.company_number = view_B.company_number_correct_answer
In standard SQL, the FROM clause (or JOIN or similar things) is not valid for the UPDATE statement.
If you want an UPDATE statement that works across multiple database products, you will have to use a co-related sub-query:
update table_a
set columna2 = (select columnb2
from table_b
where table_b.columnb1 = table_a.columna1)
where exists (select *
from table_b
where table_b.columnb1 = table_a.columna1);
Note that this requires that table_b.columnb1 is a unique or primary key, otherwise the sub-query would return more than one row which will lead to an error).
You can also use MERGE in HSQLDB
MERGE INTO tableA af
USING (select columnB1, columnB2 from.....) as b
ON af.columnA1 = b.columnB1
WHEN MATCHED THEN
UPDATE SET af.columnA2 = b.columnB2
I'm retrieving data from my database using hibernate but getting an error.
My code for retrieving data is:
I got the answer. Actually, I have to pass the name of class i.e. mapped to required table.
Query query= session.createQuery("from Register");
instead of
Query query = session.createQuery("from bank_employee") ;
I tried in Different ways, but could not fixed this Query.
I have 2 tables MerchantOffers & MerchantStore both are mapped with many-to-many. Now i need to update the total count of active offer in every MerchantStore .
some queries i tried
"UPDATE MerchantStore ms
SET ms.offers_count=
(
SELECT count(mo.offer_id)
from MerchantOffers mo
JOIN
mo.stores ms
where mo.offer_status=1
)"
for this i am getting following exception
Caused by: java.sql.SQLException: You can't specify target table 'MerchantStore' for update in FROM clause
When I attempt the following:
"UPDATE MerchantStore ms
JOIN ms.offers mo
SET ms.offers_count=count(*)
where mo.offer_status=1)"
I get the error message:
org.hibernate.hql.ast.QuerySyntaxException: expecting "set", found 'JOIN'
in this way i am failed to write Query.Could any one help.
Thanks in Advance,
raju.
I want to use spring-batch for retrieving and processing data from a postgres db.
I have a working SQL statement that would give me the full result set (about 400k entries):
private static final String QUERY = "SELECT * FROM MyDataTable ";
Now I want to use the JpaPagingItemReader so that the data is fetched (and written elsewhere) in chunks:
JpaPagingItemReader<MyEntity> reader = new JpaPagingItemReader<>();
reader.setEntityManagerFactory(emf);
reader.setQueryString(QUERY);
But it does not work:
[] 2014-09-17 16:31:58,234 ERROR : QuerySyntaxException: unexpected token: * near line 1, column 8 [SELECT * FROM my_data_table]
I also tried SELECT FROM MyDataTable and SELECT m FROM MyDataTable m without the star. Same result.
So, how can I execute that hql query with spring-batch?
By the way: the query works fine in a sql editor like pgAdmin.
SELECT m FROM MyDataTable m is almost correct (it is valid JPQL query as long as you have entity calles MyDataTable). So, it seems that you don't have entity class named MyDataTable.
As JpaPagingItemReader#setQueryString(String) accepts JPQL queries you should make sure that you have entity class for this table and then you should use its name instead MyDataTable.
By the way - for HQL queries there's HibernatePagingItemReader.
I am facing problem of executing the following query in java using hibernate for postgres tables.
The query is made up to retrive the data from 3 tables using Inner Joins.
Query :
QryJourney = "SELECT journey.id , journey.operatingday, journey.linename, journey.scheduledeparture, journey.scheduledeparturestopname, journeydetail.stopname , journeydetail.latitude, journeydetail.longitude FROM journey left join journey_journeydetail ON journey.id = journey_journeydetail.journey_id left JOIN journeydetail ON journey_journeydetail.journeydetails_id = journeydetail.id WHERE journey.id = '155815228' ORDER BY journeydetail.schedulearrival";
as soon as it executes, following exception occured.
Exception :
Exception in thread "main" org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ON near line 1, column 268 [SELECT journey.id , journey.operatingday, journey.linename, journey.scheduledeparture, journey.scheduledeparturestopname, journeydetail.stopname , journeydetail.latitude, journeydetail.longitude FROM de.db.journeyTracker.model.journey left join journey_journeydetail ON journey.id = journey_journeydetail.journey_id left JOIN journeydetail ON journey_journeydetail.journeydetails_id = journeydetail.id WHERE journey.id = '155815228' ORDER BY journeydetail.schedulearrival]
Tis query works 100% fine at postgres while executing on its SQL Pane.
Anybody having any idea?
Regards
Usman
Hibernate queries are written in Hibernate Query Language (HQL) not in native SQL. Rephrase your query in HQL or use a native query to use SQL with Hibernate.
Hibernate is an object-relational mapper. It won't just give you a result set. If you want that, use JDBC directly, using PgJDBC.
If you want native domain objects as query results, use Hibernate with HQL or via a native query mapping. Native queries are fiddlier becuse you have to explicitly tell Hibernate how all the result columns map to your result objects.