I have requirement where I need to lock rows of table for reading by other Transaction.
Means,
If Transaction 1 reads few records for table 1. These rows should not be read by Transaction2
I am having following environment
1. MySQL
2. Jboss 5.1
3. JPA 1.0
Please let me know your suggestions
I am using native query select for update
Query createNativeQuery = entityManager.createNativeQuery("select id from cscache where id = ? for update ");
But it is giving following error
2014-12-12 09:35:42,326 ERROR [org.hibernate.util.JDBCExceptionReporter ] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'limit 2' at line 1 (http-0.0.0.0-8543-5:)
You can use like that.
LOCK TABLES tbl_name READ;
For more details . please click here
SELECT FOR UPDATE should do the trick.
YourClass c = em.find(YourClass.class, yourId,
LockModeType.PESSIMISTIC_WRITE);
or
TypedQuery<Table> q = em.createQuery("select t from Table t where ...", Table.class);
q.setLockMode(LockModeType.PESSIMISTIC_WRITE);
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 am new to JPA.
I am trying to create a nativequery with 3 joins on 3 tables.
I have written a nativequery which is something like the below:
Query query=entityManager.createNativeQuery("select p.value,m.value,t.value,t.value from ping as p,ming as m,ting as t where p.id=m.vid and m.id=t.vid");
List<Object[]> list = (List<Object[]>) query.getResultList();
I have 3 tables ping,ming,ting in my database.
I have got syntax error during execution.
check the manual that corresponds to your MySQL server version for the right syntax to use near 'ping as p,ming as m ,ting as t';
Would be helpful if some one can point me the error and What would be the better solution to join different tables over nativequery in JPA.
EDIT:I have successfully run the above query on mysql.
'ping as p,ming as m ,ting as t'
Try to put dot instead coma.
Maybe your syntax is incorrect
Some how alias on the column is causing the problem.
I have tried running the program with out alias on the columns and everything is fine.
Found the similar question here.
hibernate native SQL query error
My following native query is not working:
Query createNativeQuery = entityManager.createNativeQuery(
"select id from cscache where id=? for update ");
Environment Details:
Mysql 5.6
Jboss 5.1
JPA 1.0
Error:
2014-12-12 10:20:14,581 WARN [org.hibernate.util.JDBCExceptionReporter]
SQL Error: 1064, SQLState: 42000 (http-0.0.0.0-8543-3:)
2014-12-12 10:20:14,581 ERROR [org.hibernate.util.JDBCExceptionReporter]
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'limit 2' at line 1 (http-0.0.0.0-8543-3:)
For Update basically puts a lock on rows, to achieve the same using JPA you need to use Lock Modes. To set the lock, you can use EntityManager or TypeQuery and use LockModeType.PESSIMISTIC_WRITE.
Refer this article
I haven't actually done a for update in hibernate, but I believe you can (and should?) do it on the Session or query object. Why not let hibernate do it instead of executing a native query?
According to the documentation on locking (Chapter 5: Locking, you can set the lock mode on either the session or the query.
Cscache cscache = (Cscache )session.get( Cscache.class, id, LockOptions.UPGRADE );
Specifying the LockOptions should result in a SELECT ... FOR UPDATE being executed.
Not familiar with JPA specifically, but it appears you're not telling it what the value of ? is. Check out setParameter.
How to get specific row count in Java from Object DB ?
I need to get result for query like :
SELECT COUNT(id) FROM Users WHERE banned=true
Try using * as field:
SELECT COUNT(*) as count FROM Users WHERE banned = false
OrientDB does support SQL like queries and also supports the count(<field>|*) function according to the documentation: http://code.google.com/p/orient/wiki/SQLWhere#Functions
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.