1

I am building a jcr query and receive data from repository. Here is my code:

    String queryString = "SELECT * FROM public:hours";

try {
  // get session
  Session session = requestContext.getSession();

  // create query from queryString constructed
  Query q = session.getWorkspace().getQueryManager().createQuery(queryString, Query.JCR_SQL2); 

  // execute query and retrieve result
  QueryResult result = q.execute();  


  // debug line
  log.error("query is", q.getStatement());
....

But this can not execute successfully. It gives me an error that

Repositorty Failed: 
[INFO] [talledLocalContainer] javax.jcr.query.InvalidQueryException: Query:
[INFO] [talledLocalContainer] SELECT * FROM public:(*)hours; expected: <end>

In the jcr-shell, it works if I type in query sql "select * from public:hours" and will give me proper results.
I searched many references but almost every example is the same as mine. so I am not sure where the problem is. Anyone have experience with this please help.

Allan Jiang
  • 11,063
  • 27
  • 104
  • 165

1 Answers1

4

If you're using the JCR-SQL2 query language, then you should quote the selector name with square brackets:

SELECT * FROM [public:hours]

For details, see Section 6.7.4 of the JSR-283 (aka, JCR 2.0) specification, which is also available online. Note that the square bracket quote characters are not required if the name were to be a valid SQL92 identifier. The node type names containing namespace prefixes always need to be quoted, since the ':' character is not allowed in SQL92 identifiers.

Of course, this assumes that you have a node type named "public:hours", where "public" is the namespace prefix.

Randall Hauch
  • 7,069
  • 31
  • 28
  • Hi @Randall Thank you for your answer! It get rid of the error, but I cannot get anything still.(NOTE, I can get results from shell using `query sql "select * from public:hours"`)
    Please see the last line in my code, which is `log.error("query is", q.getStatement());` it gives me an result of `query statement is` with nothing follows. So I am wondering I still didnt build the query correctly? Thank you
    – Allan Jiang Feb 21 '12 at 02:19