how to remove node itself in NOT logical cypher query? - java

I have this code in my method:
"match (n:" + query.getLabel() + " {" + propertiesGenerator(query.getNodeProperties()) + "}) ,(r:" + query.getLabel() + ")" +
"where NOT (n)-[:" + query.getRelation() + "{" + propertiesGenerator(query.getRelationProperties()) + "}]->(r)" +
"return r"
that it is equal to this cypher query in neo4j:
MATCH (a:Person {name:"salar"}),(r:Person)
WHERE NOT (a)-[:PARENT_OF {age:"20"}]->(r)
RETURN r
when i excute it , it return "a" node itself. in other words "a" dont have relation with itself.I dont want to return itself. how can i remove that?

Related

Getting Exception With DB2 Auto Increment

I have created the following table:
"CREATE TABLE ParsonCollection "
+ "(id integer not null GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"
+ "name varchar(20),"
+ "eye varchar(20),"
+ "hair varchar(20),"
+ "height varchar(20),"
+ "weight varchar(20),"
+ "PRIMARY KEY (id))";
I am then trying to insert into the table and this is where I am running into issues. When I try to alter the "id" column, I get an error saying "java.sql.SQLSyntaxErrorException: Attempt to modify an identity column 'ID'. " Here is what the insert statement looks like:
"insert into ParsonCollection values(" + q_surround(Name) + ","
+ q_surround(Eye) + "," + q_surround(Hair) + "," + q_surround(Height) + "," + q_surround(Weight) + ",1" + ")";
However, when I take away the field that is inserting into "id", I get the following error: "java.sql.SQLSyntaxErrorException: The number of values assigned is not the same as the number of specified or implied columns." Here is what this insert statement looks like:
"insert into ParsonCollection values(" + q_surround(Name) + ","
+ q_surround(Eye) + "," + q_surround(Hair) + "," + q_surround(Height) + "," + q_surround(Weight) + ")";
How do I get past this? It seems that when I solve one exception, the other one pops up and vice versa. Thanks.
You can't assign to an identity column. Since you cannot pass all values for insert, you need to enumerate the columns (omitting the identity column):
"insert into ParsonCollection (
name,
eye,
hair,
height
weight
) values("
+ q_surround(Name)
+ "," + q_surround(Eye)
+ "," + q_surround(Hair)
+ "," + q_surround(Height)
+ "," + q_surround(Weight)
+ ")";
Side note: your code is opened to SQL injection. You should seriously consider using prepared statements and bind parameters instead of concatenating the query string.
Out of interest, another solution would be to make the identity column IMPLICITLY HIDDEN

Issue when adding NULLS LAST

When adding "NULLS LAST" on the following query I'm getting the exception below:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The value is not set for the parameter number 11.
If I remove the NULLS LAST everything works fine
If I remove the CASE WHEN code and order by just one specific column it works with the NULLS LAST, but I need all the columns on the CASE WHEN.
#Query("SELECT c FROM ClassSpecificationTableEntity c"
+ " LEFT JOIN c.owner o "
+ " LEFT JOIN c.domain d "
+ "WHERE ISNULL(c.classStructure.classification.description, '') LIKE :SEARCH "
+ " OR ISNULL(c.classStructure.description, '') LIKE :SEARCH "
+ " OR ISNULL(d.description, '') LIKE :SEARCH "
+ " OR ISNULL(o.description, '') LIKE :SEARCH "
+ " OR ISNULL(c.measurementUnit, '') LIKE :SEARCH "
+ " OR ISNULL(c.defaultValue, '') LIKE :SEARCH "
+ " OR ISNULL(c.dataType, '') LIKE :SEARCH "
+ " OR ISNULL(c.tooltip, '') LIKE :SEARCH "
+ " ORDER BY "
+ " CASE :ORDERBY" +
" WHEN 0 THEN c.classStructure.classification.description " +
" WHEN 1 THEN c.assetAttribId " +
" WHEN 2 THEN c.dataType " +
" WHEN 3 THEN c.measurementUnit " +
" WHEN 4 THEN c.domain.description " +
" WHEN 5 THEN c.owner.description " +
" WHEN 6 THEN c.defaultValue " +
" WHEN 7 THEN c.tooltip " +
" END DESC NULLS LAST"
)
Page<ClassSpecificationTableEntity> findByLikeSearchDESC(#Param(value="SEARCH") final String searchCrit, final Pageable pageable, #Param(value="ORDERBY") final String orderBy);
There is no NULLS LAST syntax is T-SQL.
If you want to order NULL values last the common way is to use a CASE expression or IIF:
--CASE Expression
CASE WHEN {expression} IS NULL THEN 1 ELSE 0 END
--IIF funciton (which is actually a shorthand CASE expression)
IIF({Expression} IS NULL, 1, 0)
If you have a complicated expression, that you want to order the NULL values last for, and then the expression, and that expression does not appear in the SELECT (so cannot be referenced by its Alias), then you can move the expression to the FROM, to avoid typing the expression multiple times:
FROM ...
JOIN ...
LEFT JOIN ...
...
CROSS APPLY (VALUES({Expression}))V(Alias)
WHERE ...
ORDER BY IIF(V.Alias IS NULL,1,0),
V.Alias

how to pass positional parameters to createnativequery jpa java

I have the following SQL query in my j2ee web app that I am unable to get to work, as-is. The named parameters sourceSystem and sourceClientId do not appear to get passed to the query, and therefore it does not return any records. I added a watch to the Query object querySingleView and it maintained a value of null as the debugger ran through the code. I also inserted a System.out.println statement just under the method declaration and confirmed that the correct values sourceSystem and sourceClientId are being passed to the method signature. I am using NetBeans 8.0, JPA 2.1, running on a JBoss EAP 6.21 server. I have multiple Entities mapped to several tables in an Oracle database.
Here is the query (I should note that the items in the query follow a schema.table.column format - not sure if that is part of the problem, but it does work in one test, see my comment and sample below the main query below):
public List<String> searchSingleView (String sourceSystem, String sourceClientId) {
//Change the underscore character in the source system value to a dash, and make it uppercase. This is what single view accepts
if (!sourceSystem.equals("")) {
sourceSystemSingleView = sourceSystem.replace('_', '-').toUpperCase();
}
String sqlSingleViewQuery = "SELECT DISTINCT " +
"MDMCUST_ORS.C_BO_CONTRACT.LAST_ROWID_SYSTEM AS POLICY_SYSTEM, " +
"MDMCUST_ORS.C_BO_CONTRACT.SRC_POLICY_ID AS POLICY_ID, " +
"MDMCUST_ORS.C_BO_CONTRACT.ISSUE_DT, " +
"MDMCUST_ORS.C_BO_PARTY_XREF.SRC_CLIENT_ID AS SRC_CLIENT_ID, " +
"MDMCUST_ORS.C_BO_PARTY_XREF.PERS_FULL_NAME_TXT, " +
"MDMCUST_ORS.C_BO_PARTY_XREF.ORG_LEGAL_NAME_TXT, " +
"MDMCUST_ORS.C_BO_PARTY_XREF.ORG_LEGAL_SFX_TXT, " +
"MDMCUST_ORS.C_BO_PARTY_XREF.ORG_NAME_TXT, " +
"MDMCUST_ORS.C_BO_PARTY_XREF.SIN_BIN_TEXT, " +
"MDMCUST_ORS.C_BO_PARTY_XREF.PERS_BIRTH_DT, " +
"MDMCUST_ORS.C_LU_CODES.CODE_DESCR_EN AS ADDRESS_PURPOSE_CD, " +
"MDMCUST_ORS.C_BO_PARTY_POSTAL_ADDR.COMPLETE_ADDRESS_TXT, " +
"MDMCUST_ORS.C_BO_PARTY_POSTAL_ADDR.CITY_NAME, " +
"MDMCUST_ORS.C_BO_PARTY_POSTAL_ADDR.COUNTRY_NAME, " +
"MDMCUST_ORS.C_BO_PARTY_POSTAL_ADDR.POSTAL_CD, " +
"MDMCUST_ORS.C_BO_PARTY_POSTAL_ADDR.STATE_PROVINCE_NAME " +
"FROM MDMCUST_ORS.C_BO_PARTY_XREF " +
"LEFT JOIN MDMCUST_ORS.C_BO_PARTY_POSTAL_ADDR ON MDMCUST_ORS.C_BO_PARTY_POSTAL_ADDR.PARTY_ROWID = MDMCUST_ORS.C_BO_PARTY_XREF.ROWID_OBJECT " +
"LEFT JOIN MDMCUST_ORS.C_LU_CODES ON MDMCUST_ORS.C_BO_PARTY_POSTAL_ADDR.POSTAL_ADDR_PURPOSE_CD = MDMCUST_ORS.C_LU_CODES.CODE " +
"LEFT JOIN MDMCUST_ORS.C_BO_PARTY_REL ON MDMCUST_ORS.C_BO_PARTY_XREF.ROWID_OBJECT = MDMCUST_ORS.C_BO_PARTY_REL.FROM_PARTY_ROWID " +
"LEFT JOIN MDMCUST_ORS.C_BO_CONTRACT ON MDMCUST_ORS.C_BO_PARTY_REL.CONTRACT_ROWID = MDMCUST_ORS.C_BO_CONTRACT.ROWID_OBJECT " +
"WHERE MDMCUST_ORS.C_BO_CONTRACT.LAST_ROWID_SYSTEM = :sourceSystemSingleView " +
"AND MDMCUST_ORS.C_BO_PARTY_XREF.SRC_CLIENT_ID = :sourceClientId " +
"AND MDMCUST_ORS.C_BO_PARTY_POSTAL_ADDR.POSTAL_ADDR_PURPOSE_CD = '56|07' " +
"AND MDMCUST_ORS.C_BO_PARTY_POSTAL_ADDR.LAST_ROWID_SYSTEM = MDMCUST_ORS.C_BO_CONTRACT.LAST_ROWID_SYSTEM " +
"ORDER BY MDMCUST_ORS.C_BO_CONTRACT.LAST_ROWID_SYSTEM";
querySingleView = emSingleView.createQuery(sqlSingleViewQuery);
querySingleView.setParameter("sourceSystemSingleView", sourceSystemSingleView);
querySingleView.setParameter("sourceClientId", sourceClientId);
querySingleViewResult = (List<String>) querySingleView.getResultList();
return querySingleViewResult;
}
}
However, if I put literal values in the SQL query in place of the positional parameters it works fine (without using the setParameter method).
WHERE MDMCUST_ORS.C_BO_CONTRACT.LAST_ROWID_SYSTEM = 'ADMIN' " +
"AND MDMCUST_ORS.C_BO_PARTY_XREF.SRC_CLIENT_ID = '0000001234' " +
I have looked online but haven't as yet found anything that seems to address this specific question. Any help would be greatly appreciated. Thank you!

Could not locate named parameter

I am not able to understand why the hibernate is not finding the parameter "setor" of the query below.
hql.append("select top :limite * from MA3OCORT oco,MA4DETOT ocodA " +
" where MA4IDOCO=ma3idoco " +
" and ocodA.MA4IDODE in (select max(ocodB.MA4IDODE) from MA4DETOT ocodB where ocodA.MA4IDOCO=ocodB.MA4IDOCO and ocodB.MA4IDSIT=:situacao)" +
" and (oco.MA3DSSOL like :solicitante or :solicitante is null)" +
" and (ocodA.MA4DTDET between :datai and :dataf or :dataf is null)" +
" and ocodA.MA4IDSET = :setor" +
"order by ocodA.MA4DTDET desc");
return em.createNativeQuery(hql.toString(), OcorrenciaDetalhe.class)
.setParameter("situacao", situacao)
.setParameter("solicitante", "%" + solicitanteFiltro + "%")
.setParameter("datai", dataRespostaFiltro1)
.setParameter("dataf", dataRespostaFiltro2)
.setParameter("setor", usuarioLogado.getSetor().getId())
.setParameter("limite", limit).getResultList();
Because there is a syntax error in the select
this
" and ocodA.MA4IDSET = :setor" +
"order by ocodA.MA4DTDET desc");
becomes
' and ocodA.MA4IDSET = :setororder by ocodA.MA4DTDET desc'
You need to add a blank character after :setor or before order.
You have to make a space between sector and order by:
like that :
hql.append("select top :limite * from MA3OCORT oco,MA4DETOT ocodA " +
" where MA4IDOCO=ma3idoco " +
" and ocodA.MA4IDODE in (select max(ocodB.MA4IDODE) from MA4DETOT ocodB where ocodA.MA4IDOCO=ocodB.MA4IDOCO and ocodB.MA4IDSIT=:situacao)" +
" and (oco.MA3DSSOL like :solicitante or :solicitante is null)" +
" and (ocodA.MA4DTDET between :datai and :dataf or :dataf is null)" +
" and ocodA.MA4IDSET = :setor" +
" order by ocodA.MA4DTDET desc");
return em.createNativeQuery(hql.toString(), OcorrenciaDetalhe.class)
.setParameter("situacao", situacao)
.setParameter("solicitante", "%" + solicitanteFiltro + "%")
.setParameter("datai", dataRespostaFiltro1)
.setParameter("dataf", dataRespostaFiltro2)
.setParameter("setor", usuarioLogado.getSetor().getId())
.setParameter("limite", limit).getResultList();

JPA could not locate named parameter

I keep getting the following error: "could not locate named parameter [articleCommentId]" but it doesn't make sense to me because to me the named parameter is very much in place.
public ArticleCommentForDisplay getCommentByArticleCommentId(BigInteger articleCommentId) {
String queryString = "select c.article_comment_id, "
+ " c.article_id, "
+ " c.parent_comment_id, "
+ " p.nickname, "
+ " c.title, "
+ " c.comment, "
+ " c.person_id, "
+ " c.confirmed_user, "
+ " c.comment_depth, "
+ " c.moderation_rank, "
+ " c.moderation_reason, "
+ " c.hide, "
+ " c.hide_reason, "
+ " c.session_id, "
+ " c.confirmation_uuid, "
+ " c.created_timestamp, "
+ " c.created_by_id, "
+ " c.updated_timestamp, "
+ " c.updated_by_id, "
+ " c.update_action, "
+ " null as comment_path "
+ "from article_comment c "
+ " join person p "
+ " on p.person_id = c.person_id "
+ "where c.article_comment_id = :articleCommentId; ";
Query query = em.createNativeQuery(queryString, "ArticleCommentMap");
query.setParameter("articleCommentId", articleCommentId);
List <ArticleCommentForDisplay> articleComments = new ArrayList<>();
articleComments = query.getResultList();
ArticleCommentForDisplay theComment = articleComments.get(0);
return (theComment);
}
Here is an extract of the stack trace with the relevant error:
Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: could not locate named parameter [articleCommentId]
at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:379)
at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:72)
at com.extremelatitudesoftware.content.ArticleCommentFacade.getCommentByArticleCommentId(ArticleCommentFacade.java:293)
I bet it is due to the extra ; in your query string.
SQL/HQL does not need to be terminated by semicolon
The named parameters is not defined for native queries in JPA Specification.
Replace
where c.article_comment_id = :articleCommentId;
with
where c.article_comment_id = ?1;
....
query.setParameter(1, articleCommentId)
Mine was an extra ' in the sql query. Oh my gosh, kept looking until my eyes nearly pooooopped out `-)
So, ensure that you don't have anything "extra" in your query, make sure that your (, ", ' etc...have matching pairs, because the error message in that case is not relevant and has nothing to do with your named parameter! JPA is right as it could not locate it, but that's because something else in your query is messing up...
You can also use it like this
where c.article_comment_id = ?,
and c.any_other_field = ?;
....
query.setParameter(1, articleCommentId)
query.setParameter(2, anyOtherValue)
it will take it by sequence.
And you can also give numbers like
where c.article_comment_id = ?1,
and c.any_other_field = ?2;
....
query.setParameter(1, articleCommentId)
query.setParameter(2, anyOtherValue)
If you are using named parameter at end of your query the remove the ; from your query
In my case, I didn't add the extra space after the named parameter.
example:
+ "WHERE\n" + " s.something = 'SOME'\n" + "START WITH\n"
+ " s.country_type = :countryName" + "CONNECT BY\n"
changed to (notice the space after named parameter :countryName
+ "WHERE\n" + " s.something = 'SOME'\n" + "START WITH\n"
+ " s.country_type = :countryName " + "CONNECT BY\n"

Categories