UPDATE with LEFT JOIN not working with UCanAccess - java

I am trying to update the table row in MS Access from NetBeans. But I am getting the error -
net.ucanaccess.jdbc.UcanaccessSQLException: unexpected token: LEFT required: SET
I had tested the query directly in MS-Access, which is working perfectly. But when I use that same query in NetBeans it throwing the error.
Two tables are connected each other - category and Product tables.
Category ID (Primary Key, AutoIncrement) will be the foreign key in product table.
Now I want to update the details of product in Product table.
My Update Query :
ps_ins_new_prod = con.prepareStatement("UPDATE Inv_Category LEFT JOIN Inv_Product ON Inv_Category.Category_ID = Inv_Product.Category_ID SET Inv_Product.[Size] = ?, Inv_Product.Quantity = ?, Inv_Product.Item_No = ?, Inv_Product.Purchase_Price = ?, Inv_Product.Selling_Price = ?, Inv_Product.Category_ID = ? WHERE (((Inv_Product.Product_Name)=?) AND ((Inv_Product.Size)=?) AND ((Inv_Category.Category_Name)=?) AND ((Inv_Product.Quantity)=?) AND ((Inv_Product.Item_No)=?) AND ((Inv_Product.Purchase_Price)=?) AND ((Inv_Product.Selling_Price)=?) AND ((Inv_Product.Category_ID)=?))");

Internally Ucanaccess runs your query against a HSQLDB database which does not support LEFT OUTER JOINS in an UPDATE.
See this thread for a discussion and a possible workaround using the MERGE command.

Related

Syntax error with upsert while using latest version

I am trying to execute the following query:
INSERT INTO `xguilds_relations` ( `id1`, `id2`, `dominance` ) VALUES ( ?, ?, ? )
ON CONFLICT DO UPDATE SET `dominance` = VALUES(`dominance`);
However, this results in giving me the following syntax error:
SQL error or missing database (near "UPDATE": syntax error)
I have been looking through Google and Stackoverflow for a while and all I found was that UPSERT is only supported since SQLite 3.24.0. However, I am using 3.30.1 and it's still not working.
What I would like to achieve is:
- Insert a new row into xguilds_relations with the provided id's (table contains a CHECK (id1 > id2) or something similar) and the provided dominance
- If a row with the provided id's already exists, update that row with the new dominance value
I don't think SQLite supports VALUES. Does this work?
INSERT INTO xguilds_relations ( id1, id2, dominance )
VALUES ( ?, ?, ? )
ON CONFLICT DO UPDATE SET dominance = excluded.dominance;
You may want to also include the columns that you conflict refers to.
UPSERT works when there is a violation of a UNIQUE constraint and not a CHECK constraint.
Is there a UNIQUE constraint for the combination of id1 and id2? I believe not.
So create it:
CREATE UNIQUE INDEX IF NOT EXISTS un_id1_id2 ON `xguilds_relations` (`id1`, `id2`);
Now write the statement like this:
INSERT INTO `xguilds_relations` ( `id1`, `id2`, `dominance` ) VALUES ( ?, ?, ? )
ON CONFLICT(id1, id2) DO UPDATE SET `dominance` = <value>;
See a simplified demo.

How do I Join two tables together in SQLite that can be used in JAVA Sprint Boot?

I am using a Movie Database that will link to the front-end of my website. i am using Java Spring Boot to link the Database. My question is, How can i join a Movies table and Reviews table>]?
try {
String getAllActorsbyMoviesidQuery = "SELECT * FROM tblMovies JOIN tblReviews ON tblReviews.actor_id = tblMovies.actor_id";
statement = dbConnection.createStatement();
ResultSet resultSet = statement.executeQuery(getAllReviewsbyMoviesidQuery);
Joining the 2 tables on actor_id is not probably the suitable way.
I guess there exists inside tblReviews a column like movie_id that refers to the id column of the table tblMovies:
SELECT * FROM tblMovies JOIN tblReviews ON tblReviews.movie_id = tblMovies.id
change the column names to the actual ones.
If you want the reviews for a particular movie, then add a WHERE clause, like:
WHERE tblMovies.id = ?
or
WHERE tblMovies.title = ?

Native insert JPA Spring

I have a many-to-many relationship between two entities:
Entity A
ID
NAME
Entity B
ID
NAME
Join Table A_B
A_ID
B_ID
I'm trying to do an insert based off of the secondary unique attributes (names). I want to avoid fetching each entity by their name and then saving (2 queries & 1 insert vs 1 insert). Effectively, I'm looking to do something like this:
#Query(nativeQuery = true, value = "INSERT INTO A_B(A_ID, B_ID) VALUES ((SELECT ID FROM A WHERE NAME = ?), (SELECT ID FROM B WHERE NAME = ?))")
void addToJoinTable(String nameA, String nameB);
I saw this post, but I'm getting exceptions because an insert doesn't return a result set. The post mentions setting nativeQuery to true would resolve the issue, but I've had no such luck... Is there another & better way to do this?
Disclaimer: I am new to JPA, so this might be a stupid question...

How to map native query to one model class?

Hi I´m using Eclipselink and I did a native query to select some fields of 2 tables. I mapped my table Logins in a model class. I would not like to map my table "B" because I need only 2 fields of this table on my sql result.. can I map this 2 fields in my Logins table to my sql result ?
My sql is this:
select l.login_id, s.lugarcerto,s.vrum, l.username, l.first_name, l.last_name, l.phone, l.fax_number, l.address, l.zip,
l.address2 as 'birth_date', l.city as 'cpf_cnpj'
from Logins l
join (select se.login_id, lugarcerto = min(case when se.service = 'IM' then '1' end), vrum = min(case when se.service = 'VE' then '1' end)
from (select distinct ad.login_id, substring(ap.Rate_code,(CHARINDEX('-', ap.Rate_code)+1),2) as 'service'
from Ad_Data.dbo.ad ad
join Ad_Data.dbo.ad_pub ap on (ad.ad_id = ap.ad_id)
where ap.ad_type =1) se
group by se.login_id) s on (s.login_id = l.login_id)
I did map Logins table and I want to map s.lugarcerto and s.vrum to my SQL query result.
There´s anyway to just add it to my Logins model ?
Not without having mappings for the attributes you want those values put into, and not without causing problems with them being cached in the entity.
Why not just return the values beside the entity, much like you would with a JPQL query such as: "Select l, subquery1, subquery2 from Logins l" ie:
Query q = em.createNativeQuery(yourQueryString, "resultMappingName");
And in the entity, include the annotation:
#SqlResultSetMapping(name="resultMappingName",
entities={#EntityResult(entityClass=com.acme.Logins.class, )},
columns={#ColumnResult(name="LUGARCERTO"), #ColumnResult(name="VRUM")}
)
Best Regards,
Chris

Mixing Parameterized Query and Sub-query on Insert

I have a colleague who wants to attempt the following query:
INSERT INTO table (ColumnA, ColumnB, ColumnC)
VALUES (?, (SELECT Id FROM ColumnD WHERE x=y), ?)
Sybase complains about this as it does not seem to allow subqueries in the VALUES portion of the query. Does anyone know of a way around this problem?
How about:
INSERT INTO table (ColumnA, ColumnB, ColumnC)
SELECT
?,
Id,
?
FROM
TableD
WHERE
x = y
(Or similar)

Categories

Resources