I use Spring Data Neo4j 4.2.0.RELEASE and experience the following exception:
org.neo4j.ogm.exception.CypherException: Error executing Cypher "Neo.ClientError.Statement.ParameterMissing"; Code: Neo.ClientError.Statement.ParameterMissing; Description: Expected a parameter named 0
in the following place:
#Query("" +
"MATCH (u:User)-[:LOCATION]-(l:Location)-[r]-(p:PostalCode) " +
"WHERE id(u)={0} " +
"RETURN l, r, p"
)
Location findUserLocation(long userId);
in case if I use -parameters argument for my java compiler.
Do anyone know, why it may not work?
It's because compilation with -parameters allows the framework to get real parameter names from the source code.
In other words, it works the same way as : findUserLocation(#Param("userId") long userId)
Changing the query to use {userId} instead of {0} should work. If you need to be compatible with different compilation options, use #Param as stated above.
Related
I'm using OrientDB to represent large city maps and calculate the shortest traversal times between a pair of nodes when the need arises. I have the following method:
public void getShortestPath(int from, int to){
String query = "SELECT astar(?, ?, time, direction=OUT, customHeuristicFormula=EUCLIDEAN) FROM V";
OResultSet set = db.query(query, getNode(from).getProperty("#rid"), getNode(to).getProperty("#rid"));
}
The getNode(nodeID) method returns the OVertex object present in the database, hence we can identify the #rid. Clearly, this method serves no purpose as is.
My issue comes when trying to call the astar query on the database (i.e. line two of the method). I'm getting the following error: OCommandSQLParsingException: Error parsing query upon reaching the first ( (i.e. error encountering the open bracket). Removing the brackets entirely simply resulted in the same error occurring on the # in front of the first #rid value.
I can't seem to find any example of using this function in practice, and (at least I think) I'm using the function call as suggested by the documentation. Look forward to hearing your thoughts.
I'm using the most recent version of OrientDB: 3.2.3
Turns out the error had nothing to do with the bracket itself. Passing the "direction='OUT'" and "customHeuristicFormula='EUCLIDEAN'" parameters in as part of the string was the problem. The below block did the trick.
String sql = "SELECT ASTAR(" + getNode(from).getProperty("#rid") + ", " + getNode(to).getProperty("#rid") + ", time) FROM V";
try(OResultSet set = db.query(sql, "direction='OUT'", "customHeuristicFormula='EUCLIDEAN'")) {
// some code...
}
I got an enum (ClubRole) which got a method returning a collection of values from this enum. I trying to call this method from inside the query using SpEl.
#Query("select m from ClubMember m " +
"where m.student = :student " +
"and m.role in :#{#role.getParents()}"
)
List<ClubMember> findByRoleWithInheritance(#Param("student") Student student, #Param("role") ClubRole role);
This passes the build, and the application runs but when this method's called I got ``No parameter binding found for name role!; nested exception is java.lang.IllegalArgumentException: No parameter binding found for name role!
I tried different ways but none worked. I would like to know if it's possible to use SpEl in this situation, and if so how ?
Looks like this is an issue in spring-data-jpa. I could see people discussing same issue on spring-blog. Not sure whether there is an open issue for this.
You can try following as a workaround.
#Query("select m from ClubMember m " +
"where m.student = :#{#student}" +
"and m.role in :#{#role.getParents()}"
)
List<ClubMember> findByRoleWithInheritance(#Param("student") Student student, #Param("role") ClubRole role);
Or you can try with index access for second parameter like #{[1].getParents()}
this might help.
I am trying do something super simple but with Spring Repositories somethings is a bit hard. Basically I wanted to group by with DATE_FORMAT, example:
#Query("SELECT " +
" new users.bridge.models.dto.PerformanceDTO(sum(t.gl), sum(t.gl)) " +
"FROM " +
" Transaction t " +
"GROUP BY DATA_FORMATE(t.createdDate,'%Y-%m-%d')")
But it throws a syntax error. Is there a way to do that with spring repositories? I don't want to use nativeQuery=true flag, otherwise I can not use this syntax
new gara.users.bridge.models.dto.PerformanceDTO(sum(t.gl), sum(t.gl))
UPDATE:
The erros are:
all the java stack is quite big but:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: asc near line 1, column 180 [SELECT new gara.users.bridge.models.dto.PerformanceDTO(sum(t.gl), sum(t.gl),t.createdDate) FROM gara.model.db.Transaction t GROUP BY DATE_FORMAT(t.createdDate,'%Y-%m-%d') asc]
If you specify nativeQuery=false (the default) you need to use valid JPQL.
JPQL doesn't know the function DATE_FORMAT but you can use the generic FUNCTION function which allows you to call arbitrary SQL functions.
So a group by clause like this should work: GROUP BY FUNCTION('DATA_FORMAT', t.createdDate,'%Y-%m-%d')
Just be aware that such queries aren't portable between databases.
but with Spring Repositories somethings is a bit hard.
You can always fall back on custom method implementations which shouldn't be much harder than implementing your repository yourself in the first place.
I'm using the following JDBC driver (the one from Microsoft): http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx.
I want to retrieve the OUTPUT parameter (#cookie) of the stored procedure sp_setapprole. I can call the procedure fine like this, but I can't figure out how to retrieve the OUTPUT parameter.
statement = connection.createStatement();
statement.execute("EXEC sp_setapprole '" + applicationRoleName + "', '" + applicationRolePassword + "'");
I tried with a PreparedStatement and a CallableStatement and I always get the following exception: Stored procedure 'sys.sp_setapprole' can only be executed at the ad hoc level.. I found this post: https://stackoverflow.com/a/6944693/1362049, but I don't like the solution (use another JDBC driver).
So my question: how to get OUTPUT parameter from stored procedure sp_setapprole in SQLServer using a Statement.
I think this will help. I am not a big java program but lived with ODBC for years.
http://www.tutorialspoint.com/jdbc/jdbc-statements.htm
Look at the callable statement. You need to define the in/out, or inout parameters. Execute the SP and read the output.
In C#, it is just looking at the parameter such as below give you the value. But the idea of making a connection, binding parameters, making the call and reading the output are the same.
my_Cmd.Parameters["#PageCount"].Value.ToString();
Here is a article from MSDN in C++.
http://technet.microsoft.com/en-us/library/ms403283.aspx
Same idea, bind parameters, make the call, read the output using [SQLExecDirect]
Give me a holler if you do not get it.
I have this Java code (JPA):
String queryString = "SELECT b , sum(v.votedPoints) as votedPoint " +
" FROM Bookmarks b " +
" LEFT OUTER JOIN Votes v " +
" on (v.organizationId = b.organizationId) " +
"WHERE b.userId = 101 " +
"GROUP BY b.organizationId " +
"ORDER BY votedPoint ascending ";
EntityManager em = getEntityManager();
Query query = em.createQuery(queryString);
query.setFirstResult(start);
query.setMaxResults(numRecords);
List results = query.getResultList();
I don't know what is wrong with my query because it gives me this error:
java.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V
at org.hibernate.hql.antlr.HqlBaseParser.fromJoin(HqlBaseParser.java:1802)
at org.hibernate.hql.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1420)
at org.hibernate.hql.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1130)
at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:702)
at org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)
at org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:271)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:180)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)
at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650)
Thanks.
You definitely have an issue with the version of hibernate and ANTLR jars that you are using. The recover method wasn't present in the ANTLR Parser class until version 2.7.6? If you are using an earlier version of ANTLR, such as 2.7.2, then you will see this problem.
Using maven can cause this sort of situation, where you depend on Hibernate and its transitive dependencies, but something 'closer'; e.g. Struts; providers a different, earlier version of ANTLR and that earlier version gets resolved in your application.
If you can provide the version of jars involved, we would be able to help some more. Once you have fixed the issue with the jar versions, you should get a more revealing error message which shows what is wrong with your HQL expression.
Stab in the dark - Are you sure you have a consistent set of jars - perhaps you need to get the antlr jar that comes with the hibernate distribution you are using...
I've found the problem:
because this is a native query, java classes for this 2 tables must have some special attributes:
in Bookmarks.java class
#OneToMany(mappedBy = "bookmarkId")
private Collection votesCollection;
and in Votes.java class
#JoinColumn(name = "bookmark_id", referencedColumnName = "bookmark_id")
#ManyToOne
[private Bookmarks bookmarkId;
and i have also changed the query to work
tring queryString = "SELECT b, sum(v.votedPoints) " +
"FROM Bookmarks b " +
"LEFT OUTER JOIN b.votesCollection v " +
"WHERE b.userId = 101 " +
"GROUP BY b.organizationId " +
"ORDER BY sum(v.votedPoints) asc ";
thanks for the help
May be you have some double-quotes " missing or which should be doubled in your HQL.
Illustration here.
Or you miss some simple quotes as illustrated there
The query seems to be invalid unless it's an artifact of formatting.
I think you meant this:
Select b, ...
to be:
Select b.organizationId, ...
??
I have the consistent set of jars because simple queries like this one
"SELECT b FROM table_name b WHERE b.userId = 102 "
are working. I have verified all double quotes and everything is alright.
My database is: mysql, and I use jpa to connect to it. I don't know what is causing the problem. Maybe this type of join, i don't know
Er, isnt your query trying to select b which is a table alias and thats not allowed as far as I know.
I'd probably guess that something is going wrong with your query, because the method HqlBaseParser fails to lookup is called recover(RecognitionException, Bitset). Perhaps this query fails for some reason the other simpler queries don't (and the NoSuchMethod exception is thrown when attempting to recover from that error).
java.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V
Your query is still wrong. Maybe it works with your driver/db but it isn't standard SQL. You should be selecting b.* or b.organizationId.