0

I have a FILE table within my application (using postgreSQL) which stores off the full path of a file I have on my OS.

When I try and do a 'like' query, I keep getting zero results when I know there is data saved.

Here's the query in question:

public List<MyFile> getDirectoryFiles(Path path) {
        Query query = em.createQuery("from MyFile f WHERE f.path LIKE :path");
        query.setParameter("path", "////data////myData////.settings% ESCAPE '//'");  // hard coded the path for testing
        return query.getResultList();
}


@Override
public void deleteDirectoryFiles(Path path) {
    for(MyFile file : this.getDirectoryFiles(path)){
        this.delete(file);
    }
}


// in some other method I am deleting the files from a directory
Foo.deleteDirectoryFiles(this);  // points to '/data/myData/.settings'

Not sure why this is happening. When I take what hibernate is producing and plugin the absolute path, I do see the returned tuples within my database.

Has anyone ever dealt with this issue? If so, how did you solve the problem? I've tried hardcoding my ':path' variable to be "'/data/myData/.settings%'", "%/data/myData/.settings%", "//data//myData//.settings%" and still nothing is being returned. There are no errors, but I cannot figure out why there are no results.

Dan
  • 979
  • 1
  • 8
  • 29
  • 3
    A query that returns no result doesn't throw an exception. It returns an empty list. You shouldn't catch any exception in your method. Catching the exception only hides the bug. Let the exception bubble, and paste its stack trace to understand the cause of the exception, and fix the code. – JB Nizet Mar 14 '14 at 20:43
  • It's meaningless for you, but maybe not for us. Why do you ask a question if you think nobody can understand what you don't? – JB Nizet Mar 14 '14 at 21:10
  • 2
    getSingleResult() throws a NoResultException. getResultList() returns an empty list. Read the api doc. Your catch block is useless. And returning null from a methods returning a List is a bad practice. – JB Nizet Mar 15 '14 at 07:12
  • I'll fix. Any suggestions on what might be causing the query to return empty when I know the data is there? – Dan Mar 15 '14 at 10:15

1 Answers1

1

You have wrong syntax in you query, it looks more like HQL query than JPQL. The correct query should be like this:

em.createQuery("SELECT f FROM MyFile f WHERE f.path LIKE :path");
Michal
  • 212
  • 2
  • 10
  • 1
    Under the hood I am using Hibernate, so I know the statement works – Dan Mar 14 '14 at 20:55
  • Changed the query just to be sure it wasn't the syntax and it still didn't return any results. – Dan Mar 14 '14 at 21:12
  • I've updated the query to the point where the log files are producing SQL that runs correctly when I plug it in to pgAdmin, but I do not get any results through JPA. – Dan Mar 17 '14 at 13:17