I am running a query but it returns no such column. I copy and pasted the column into sqlite viewer and it is running with no errors.
String SQL_QUERY = "SELECT m.* FROM '21_cars_models' as m INNER JOIN ( '41_cars_models_galleries' as mg
INNER JOIN ( '24_galleries' as g
INNER JOIN ( '26_galleries_pictures' as gp INNER JOIN '25_pictures' as p ON gp.picture_id=p._id )
ON g._id=gp.gallery_id ) ON mg.gallery_id=g._id )
ON m._id=mg.model_id WHERE m.brand_id LIKE '" + markaID + "'";
02-12 13:58:09.881: E/MYAPP(10962): android.database.sqlite.SQLiteException: no such column: gp.gallery_id (code 1): , while compiling: SELECT m.* FROM '21_cars_models' as m INNER JOIN ( '41_cars_models_galleries' as mg INNER JOIN ( '24_galleries' as g INNER JOIN ( '26_galleries_pictures' as gp INNER JOIN '25_pictures' as p ON gp.picture_id=p._id ) ON g._id=gp.gallery_id ) ON mg.gallery_id=g._id ) ON m._id=mg.model_id WHERE m.brand_id LIKE '339'
EDIT:
If i remove all INNER JOIN and run it is everything fine. I can't understand why with INNER JOIN things is blowing. Again to remind that if i run this directly to database is everyting fine....
The problem is that you cannot make inner join with parenthesis.... i turned my query like
From a INNER JOIN b ON a.id=b.id INNER JOIN c ON c.id=b.id INNER JOIN d ON d.id=c.id
And it worked perfectly...
Thanks everyone for your time....
Related
i have a simple stored procedure. i use this to show data from my db. i want to optimize this query because i think this is slow. 31 seconds for 220.000 rows. so this is my query
BEGIN
sql_base :=
'SELECT
p.id
,p.cis
,p.account_no
,p.name
,p.address
,p.village_name
,p.postal_code
,p.subdistrict_name
,p.regency_name
,p.province_name
,p.country_name
,m.group_id
,i.name
,m.id
,m.name
,m.address
,m.village_name
,m.postal_code
,m.subdistrict_name
,m.regency_name
,m.province_name
,m.country_name
,listagg (mMcc.name, '','') within group (order by mMcc.name) merchantMccName
,o.mid
,o.name
,o.mcc_id outletMccId
,REPLACE(REPLACE(o.jenis_outlet,''ecomm'',''e-commerce''),''retail'',''toko'') jenisOutlet
,o.MODIFIEDDATE
,o.CREATEDDATE
,o.CC_testing_ACCOUNTNO outletCcAccountNo
,o.DEBIT_testing_ACCNO outletDebitAccountNo
,MCC.name outletMccName
,o.MCC_CODE outletMccCode
,m.code merchantCode
,p.jenisn abah jenisn abah
,p.segmenn abah segmenn abah
,p.IDENTITY_NO identityNo
,p.IDENTITY_TYPE identityType
,o.status status
,NVL(o.ISOUTLET, 0) isOutlet
,NVL(p.IS_ACCOUNT_testing, 0) isAccounttesting
,p.ACCOUNT_OTHERBANK_NO accountOtherbankNo
,p.ACCOUNT_OTHERBANK_BANKNAME accountOtherbankBankname
,p.ACCOUNT_OTHERBANK_NO pemilikOtherBankAccountNo
,o.ADDRESS1 outletAddress1
,o.ADDRESS2 outletAddress2
,o.VILLAGE_NAME outletVillageName
,o.POSTAL_CODE outletPostalCode
,o.SUBDISTRICT_NAME outletSubdistrictName
,o.REGENCY_NAME outletRegencyName
,o.PROVINCE_NAME outletProvinceName
,C E WHEN NVL(o.IS_CC_TESTING,0)=1 THEN ''TESTING'' ELSE TO_CHAR(o.CC_OTHERBANK_NAME) END outletCcBankName
,C E WHEN NVL(o.IS_DEBIT_TESTING,0)=1 THEN ''TESTING'' ELSE TO_CHAR(o.CC_OTHERBANK_NAME) END outletDebitBankName
,NVL(o.IS_CC_OTHERBANK,0) isCcOtherBank
,NVL(o.IS_CC_TESTING,0) is_cc_testing
,NVL(o.IS_DEBIT_testing,0) is_debit_testing
,o.CC_OTHERBANK_ACCNO ccOtherBankAccNo
,o.QRIS_STATIS_NMID nmid
,o.AGENT_BANK_CODE outletAgentBank
FROM PEMILIK p
LEFT JOIN mytable m ON p.ID = m.PEM_ID
LEFT JOIN mytable2 i ON m.GROUP_ID = i.ID
INNER JOIN mytable3 o ON m.ID = o.MERCH_ID
LEFT JOIN mytable4 mMcc ON m.ID = mMcc.MERCH_ID
LEFT JOIN mytable5 mcc ON MCC.ID = o.MCC_ID
GROUP BY
*SAME LIKE SELECT*;
final_result:='SELECT rec.*, count (*) over ()CountData FROM ('
|| sql_base || ') rec '
||WHERE_Q
||ORDERBY_Q||' '||PAGING_Q ;
OPEN O_RESULT_REC FOR
final_result;
i already tried to change the query but still i think my query still not good. any tips to improve my query? should i change the connection in java? or this is just query problem?
The query is:
WITH part_list AS (
SELECT pd.id AS part_data_id, b.billname, pd.refpn, pd.originalpn, pd.catalogpn, pd.originalmfg, mfg.market, pd.contentid, b.accountid
FROM price_data pd
JOIN bills_price_data bpd ON pd.id = bpd.price_data_id
JOIN bill b ON bpd.bill_id = b.id
LEFT JOIN markets mfg ON pd.marketid = mfg.id
WHERE b.id = 35699799 -- ?#{#billId}
)
SELECT COUNT(*) FROM (
SELECT d.part_data_id, d.billname, d.refpn, d.originalpn, d.catalogpn,
d.originalmfg, d.market, cd.declaration_type,
'Yes' AS rate_stock_contained,
s.id AS stock_id, s.stock, s.cas
FROM part_list d
INNER JOIN content c ON d.contentid = c.id
INNER JOIN content_declaration_type cd ON c.declarationtypeid = cd.id
INNER JOIN content_cards cc ON c.id = cc.content_id
INNER JOIN card_materials cm ON cc.id = cm.content_cards_id
INNER JOIN material_stocks ms ON cm.id = ms.card_materials_id
INNER JOIN stocks s ON ms.stocks_id = s.id
WHERE c.declarationtypeid IN (2,3)
AND s.cas ~ ',?(10099-76-0|11120-22-2|117-81-7|12036-10-1|129915-35-1|1317-36-8|1317-38-0|65997-18-4|7439-92-1|7631-86-9),?'
UNION
SELECT d.part_data_id, d.billname, d.refpn, d.originalpn, d.catalogpn, d.originalmfg, d.market
, cd.declaration_type
, 'No' AS rate_stock_contained
, 0 AS stock_id, '' AS stock, '' AS cas
FROM part_list d
INNER JOIN content c ON d.contentid = c.id
INNER JOIN content_declaration_type cd ON c.declarationtypeid = cd.id
WHERE c.declarationtypeid IN (2,3) -- Full/Partial
AND NOT EXISTS (
SELECT *
FROM part_list d2
JOIN content c2 ON d2.contentid = c2.id
JOIN content_declaration_type cd2 ON c2.declarationtypeid = cd2.id
JOIN content_cards cc2 ON c2.id = cc2.content_id
JOIN card_materials cm2 ON cc2.id = cm2.content_cards_id
JOIN material_stocks ms2 ON cm2.id = ms2.card_materials_id
JOIN stocks s2 ON ms2.stocks_id = s2.id
WHERE c2.declarationtypeid IN (2,3)
AND s2.cas ~ ',?(10099-76-0|11120-22-2|117-81-7|12036-10-1|129915-35-1|1317-36-8|1317-38-0|65997-18-4|7439-92-1|7631-86-9),?'
AND d2.part_data_id = d.part_data_id
)
UNION
SELECT d.part_data_id, d.billname, d.refpn, d.originalpn, d.catalogpn, d.originalmfg, d.market
, cd.declaration_type
, 'Yes' AS rate_stock_contained , s.id AS stock_id, s.stock, s.cas
FROM part_list d
INNER JOIN content c ON d.contentid = c.id
INNER JOIN content_declaration_type cd ON c.declarationtypeid = cd.id
INNER JOIN content_restricted_stocks crs ON c.id = crs.content_id
INNER JOIN stocks s ON crs.stock_id = s.id
WHERE c.declarationtypeid NOT IN (2,3) -- Full/Partial
AND s.cas ~ ',?(10099-76-0|11120-22-2|117-81-7|12036-10-1|129915-35-1|1317-36-8|1317-38-0|65997-18-4|7439-92-1|7631-86-9),?'
UNION
SELECT d.part_data_id, d.billname, d.refpn, d.originalpn, d.catalogpn, d.originalmfg, d.market
, cd.declaration_type
, CASE c.has_no_restricted_stocks WHEN true THEN 'No' ELSE 'Unknown' END AS rate_stock_contained
, 0 AS stock_id, '' AS stock, '' AS cas
FROM part_list d
JOIN content c ON d.contentid = c.id
JOIN content_declaration_type cd ON c.declarationtypeid = cd.id
WHERE c.declarationtypeid NOT IN (2,3) -- Full/Partial
AND NOT EXISTS (
SELECT *
FROM part_list d2
JOIN content c2 ON d2.contentid = c2.id
JOIN content_restricted_stocks crs2 ON c2.id = crs2.content_id
WHERE d2.part_data_id = d.part_data_id
)
) z
As a query, it runs correctly and it returns results. Inside Java, this error is returned:
Mar 14, 2021 9:22:13 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 42601
Mar 14, 2021 9:22:13 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ERROR: syntax error at end of input
Position: 3288
Is there a syntax issue I am not seeing? And why would it run manually but not inside my Java api ?
You're missing a closing parenthesis which I guess PostgreSQL is simply ignoring, but Java isn't. The Parenthesis seems also to be at around Position 3288. So I guess after the z.
Here's my Repository method that I'm trying to call.
#Query(value = "select ise from InquirySession ise " +
"inner join ise.inquiryRequest.inquiryRequestFacilities irf " +
"left outer join ise.inquiryResponses ire on ire.facilityId = ?2 " +
"left outer join ire.responseActions ira " +
"left outer join ire.acceptDates irad " +
"left outer join ire.responseReasons irr " +
"where ise.carePathItem.id = ?1 and irf.facilityId = ?2")
Set<InquirySession> getAllByCarePathItemIdAndProviderId(final Long carePathItemId, final Long providerId);
My thought process was that when this method is executed, ise.inquiryResponses would be joined to InquirySession using the additional 'on' clause defined in the JPQL. However, I think this JPQL only selects InquirySession and does separate calls to join all other entities (defined in InquirySession) as I see separate sql statements after this one joining each entity using the primary/foreign key relationship.
Here's how InquiryResponse looks inside InquirySession
#OneToMany(mappedBy = "inquirySessionId")
#OrderBy(value = "createDate")
private Set<InquiryResponse> inquiryResponses;
and vice versa,
#Column(name = "inquirySessionDatabaseKey")
private Long inquirySessionId;
Is it possible to join InquiryResponse with the additional on clause ire.facilityId = ?2 when hydrating InquirySession entity using JPQL?
Edit
JPQL equivalent SQL
select inquiryses0_.inquirySessionId as inquirySessionId1_36_, inquiryses0_.carepathitemid as CarePat12_36_, inquiryses0_.createDate as createDa2_36_, inquiryses0_.description as descript3_36_,
inquiryses0_.fileSize as fileSize4_36_, inquiryses0_.documentLink as inquiryS5_36_, inquiryses0_.numberOfPages as numberOf6_36_, inquiryses0_.patientId as patientId7_36_,
inquiryses0_.patientEncounterId as patientV8_36_, inquiryses0_.patientplannerid as patientplannerid13_36_, inquiryses0_.purgedOn as purgedOn9_36_, inquiryses0_.amazonbucketname as s10_36_,
inquiryses0_.type as type11_36_
from InquirySession inquiryses0_
inner join InquiryRequest inquiryreq1_ on inquiryses0_.inquirySessionId=inquiryreq1_.inquirySessionId
inner join InquiryRequest_Facility inquiryreq2_ on inquiryreq1_.inquiryrequestid=inquiryreq2_.inquiryrequestid
left outer join InquiryResponse inquiryres3_ on inquiryses0_.inquirySessionId=inquiryres3_.inquirySessionId and (inquiryres3_.providerId=?)
left outer join InquiryResponse_Action_Link inquiryres3_1_ on inquiryres3_.inquiryresponseid=inquiryres3_1_.inquiryresponseid
left outer join InquiryResponseActions inquiryres4_ on inquiryres3_1_.inquiryresponseactionid=inquiryres4_.inquiryresponseactionid
left outer join InquiryResponse_AcceptDate_Link acceptdate5_ on inquiryres3_.inquiryresponseid=acceptdate5_.inquiryresponseid
left outer join InquiryResponse_Reason_Link responsere6_ on inquiryres3_.inquiryresponseid=responsere6_.inquiryresponseid
left outer join InquiryResponseReasons inquiryres7_ on responsere6_.responsereasonid=inquiryres7_.responsereasonid
where inquiryses0_.carepathitemid=? and inquiryreq2_.providerId=?
SQL that does not honor the 'on' clause and joins just on InquirySession.inquirySessionId
select inquiryres0_.inquirySessionId as inquirySessionId5_30_0_, inquiryres0_.inquiryresponseid as inquiryresponseid1_30_0_, inquiryres0_.inquiryresponseid as inquiryresponseid1_30_1_, inquiryres0_.createDate as createDa2_30_1_,
inquiryres0_.providerId as providerId3_30_1_, inquiryres0_.alsoCallMe as alsoCall4_30_1_, inquiryres0_.inquirySessionId as inquirySessionId5_30_1_, inquiryres0_.phoneNumber as phoneNum6_30_1_,
inquiryres0_.respondedBy as responde7_30_1_, inquiryres0_.responseText as response8_30_1_, inquiryres0_.responseTypeID as response9_30_1_,
inquiryres0_1_.inquiryresponseactionid as RSPNS_AC1_32_1_, inquiryres1_.inquiryresponseactionid as RSPNS_AC1_34_2_, inquiryres1_.inquiryresponseactiondescription as RSPNS_AC2_34_2_
from InquiryResponse inquiryres0_
left outer join InquiryResponse_Action_Link inquiryres0_1_ on inquiryres0_.inquiryresponseid=inquiryres0_1_.inquiryresponseid
inner join InquiryResponseActions inquiryres1_ on inquiryres0_1_.inquiryresponseactionid=inquiryres1_.inquiryresponseactionid
where inquiryres0_.inquirySessionId=? order by inquiryres0_.createDate
Looking at the first SQL, it's quite evident that the JPQL does not really care about joining other entities in InquirySession as all of the select statements are for InquirySession table. It does a separate SELECT statement to join InquiryResponse and thus, no longer has the expected additional ON clause.
While running Hibernate Criteria, HQL query is not created in given order
and shows Exception
ERROR SqlExceptionHelper:131 - Unknown column 'stsup4_.SUPPLIER_ID' in 'on clause'
The Generated HQL is not created in expected order.
When I run the same generated HQL in mysql with some changes, I get output.
Java Code is given Below
Session session = connector.getSession();
Criteria cr = session.createCriteria(ST_PRODUCTMASTER.class, "ST_PRODUCT");
cr.createCriteria("ST_PRODUCT.ST_PRODUCT_MANUFACTURER_PARENT", "STPRODMAN", JoinType.LEFT_OUTER_JOIN, Restrictions.eq("PROD_MAN_STATUS", "ACTIVE"));
cr.createCriteria("STPRODMAN.ST_MANUFACTURER", "STMAN", JoinType.LEFT_OUTER_JOIN);
cr.createCriteria("ST_PRODUCT.ST_PRODUCT_SUPPLIER_PARENT", "STPRODSUP", JoinType.LEFT_OUTER_JOIN, Restrictions.eq("PROD_SUPPLIER_STATUS", "ACTIVE"));
cr.createCriteria("STPRODSUP.ST_SUPPLIER", "STSUP", JoinType.LEFT_OUTER_JOIN);
cr.createCriteria("ST_PRODUCT.ST_PRODUCT_RATES", "ZSTPR", JoinType.LEFT_OUTER_JOIN, Restrictions.eqProperty("ST_SUPPLIER.SUPPLIER_ID", "STSUP.SUPPLIER_ID"));
cr.createCriteria("ST_PRODUCT.ST_MEDICINE_CATEGORY", "MEDI_CAT", JoinType.LEFT_OUTER_JOIN);
cr.createCriteria("ST_PRODUCT.ST_TAX_MASTER", "TAX", JoinType.INNER_JOIN);
cr.createCriteria("ST_PRODUCT.ST_UNIT", "UNIT", JoinType.INNER_JOIN);
cr.add(Restrictions.eq("ST_PRODUCT.PRODUCT_STATUS", "ACTIVE"));
ProjectionList p1 = Projections.projectionList();
p1.add(Projections.property("ST_PRODUCT.PRODUCT_ID"));
p1.add(Projections.property("ST_PRODUCT.PRODUCT_NAME"));
p1.add(Projections.property("UNIT.UNIT_DISPLAY_UNIT"));
p1.add(Projections.property("TAX.TAX_PURCHASE"));
p1.add(Projections.property("ST_PRODUCT.PRODUCT2_DISCOUNT"));
p1.add(Projections.property("ST_PRODUCT.PRODUCT2_DIS_AMOUNT"));
p1.add(Projections.property("ST_MEDICINE_CATEGORY.CAT_CATEGORY_ID"));
p1.add(Projections.property("MEDI_CAT.CAT_SHORT_NAME"));
p1.add(Projections.property("UNIT.UNIT_ID"));
p1.add(Projections.property("TAX.TAX_ID"));
//MANUFACTURER
p1.add(Projections.property("STMAN.MAN_ID"));
p1.add(Projections.property("STPRODMAN.BASE_LEVEL"));
p1.add(Projections.property("STPRODMAN.FREE_QTY"));
//SUPPLIER
p1.add(Projections.property("STSUP.SUPPLIER_ID"));
p1.add(Projections.property("STPRODSUP.BASE_LEVEL"));
p1.add(Projections.property("STPRODSUP.FREE_QTY"));
cr.setProjection(p1);
List l = cr.list();
System.out.println("Size items::" + l.size());
ERROR SqlExceptionHelper:131 - Unknown column 'stsup4_.SUPPLIER_ID' in 'on clause'
This it the SQL generated by Hibernate
select
this_.PRODUCT_ID as y0_,
this_.PRODUCT_NAME as y1_,
unit8_.UNIT_DISPLAY_UNIT as y2_,
tax7_.TAX_PURCHASE as y3_,
this_.PRODUCT2_DISCOUNT as y4_,
this_.PRODUCT2_DIS_AMOUNT as y5_,
this_.CAT_CATEGORY_ID as y6_,
medi_cat6_.CAT_SHORT_NAME as y7_,
unit8_.UNIT_ID as y8_,
tax7_.TAX_ID as y9_,
stman2_.MAN_ID as y10_,
stprodman1_.BASE_LEVEL as y11_,
stprodman1_.FREE_QTY as y12_,
stsup4_.SUPPLIER_ID as y13_,
stprodsup3_.BASE_LEVEL as y14_,
stprodsup3_.FREE_QTY as y15_
from
ST_PRODUCTMASTER this_
left outer join
ST_MEDICINE_CATEGORY medi_cat6_
on this_.CAT_CATEGORY_ID=medi_cat6_.CAT_CATEGORY_ID
left outer join
ST_PRODUCT_MANUFACTURER stprodman1_
on this_.PRODUCT_ID=stprodman1_.PRODUCT_ID
and (
stprodman1_.PROD_MAN_STATUS=?
)
left outer join
ST_MANUFACTURER stman2_
on stprodman1_.MAN_ID=stman2_.MAN_ID
left outer join
ST_PRODUCT_RATES zstpr5_
on this_.PRODUCT_ID=zstpr5_.PRODUCT_ID
and (
zstpr5_.SUPPLIER_ID=stsup4_.SUPPLIER_ID
)
left outer join
ST_PRODUCT_SUPPLIER stprodsup3_
on this_.PRODUCT_ID=stprodsup3_.PRODUCT_ID
and (
stprodsup3_.PROD_SUPPLIER_STATUS=?
)
left outer join
ST_SUPPLIER stsup4_
on stprodsup3_.SUPPLIER_ID=stsup4_.SUPPLIER_ID
inner join
ST_TAX_MASTER tax7_
on this_.TAX_ID=tax7_.TAX_ID
inner join
ST_UNIT unit8_
on this_.UNIT_ID=unit8_.UNIT_ID
where
(
this_.PRODUCT_DELETED <> 'DELETED'
)
and this_.PRODUCT_STATUS=?
zstpr5_ is generated before stsup4_
and thus zstpr5_.SUPPLIER_ID=stsup4_.SUPPLIER_ID
is not accessible
left outer join
ST_PRODUCT_RATES zstpr5_
on this_.PRODUCT_ID=zstpr5_.PRODUCT_ID
and (
zstpr5_.SUPPLIER_ID=stsup4_.SUPPLIER_ID
)
left outer join
ST_PRODUCT_SUPPLIER stprodsup3_
on this_.PRODUCT_ID=stprodsup3_.PRODUCT_ID
and (
stprodsup3_.PROD_SUPPLIER_STATUS=?
)
left outer join
ST_SUPPLIER stsup4_
on stprodsup3_.SUPPLIER_ID=stsup4_.SUPPLIER_ID
ACTUAL EXPECTED ORDER IS
left outer join
ST_PRODUCT_SUPPLIER stprodsup3_
on this_.PRODUCT_ID=stprodsup3_.PRODUCT_ID
and (
stprodsup3_.PROD_SUPPLIER_STATUS=?
)
left outer join
ST_SUPPLIER stsup4_
on stprodsup3_.SUPPLIER_ID=stsup4_.SUPPLIER_ID
left outer join
ST_PRODUCT_RATES zstpr5_
on this_.PRODUCT_ID=zstpr5_.PRODUCT_ID
and (
zstpr5_.SUPPLIER_ID=stsup4_.SUPPLIER_ID
)
Hibernate is generating a number with table name,
1_, 2_ , 3_
even though while creating SQL this order is not maintained,
it is not even created in the order we give.
Since you haven't posted your mapping file, I'll explain how Hibernate orders the joins.
Hibernate uses the mapping xml file to figure out the order of joins, so if you have a relation in it (one-to-many or any other tag) for that table, just move it above the other table's tag.
You can read more about it here.
I'm learning JPQL and I wonder how to translate this query into JPQL ?
I have entities Astre for astres table and Planetefor planetes table. generate_series() is PostgreSQL function.
SELECT t1.galaxie, t1.ss, t1.position FROM
(
SELECT galaxie, ss, position FROM
generate_series(1, 9) galaxie
CROSS JOIN generate_series(1, 500) ss
CROSS JOIN generate_series(1, 15) position
) t1
LEFT JOIN
(
SELECT a.galaxie, a.ss FROM astres a
INNER JOIN planetes p
ON a.id=p.astre_id
WHERE p.planete_mere=TRUE
GROUP BY a.galaxie, a.ss
HAVING COUNT(*)=3
) t2
ON t1.galaxie = t2.galaxie
AND t1.ss = t2.ss
LEFT JOIN
(
SELECT a.galaxie, a.ss, a.position FROM astres a
INNER JOIN planetes p
ON a.id=p.astre_id
) t3
ON t1.galaxie=t3.galaxie
AND t1.ss=t3.ss
AND t1.position=t3.position
WHERE t2.galaxie IS NULL
AND t3.galaxie IS NULL
ORDER BY t1.galaxie, t1.ss, t1.position
I can supply entities code if needed.
Thanks for help.