6

I've been working within the Spring MVC and have absolutely no clue what I'm doing. I'm trying to retrieve a list of objects (or records) from a database using entityManager. I have my method that doesn't seem to do anything:

     @Override
public List<Module> sortStatus(String status) {
    String queryString = "SELECT id, title, description, credit, minimumScore, daysToComplete, status, deleted FROM Module where status='"
            + status + "'";

    Query query = entityManager.createQuery(queryString);

    return (List<Module>) query.getResultList();
}

I'm trying to return a list of 'Modules' based on that query string but it doesn't appear to be executing. The documentation and solutions I've found regarding this problem are very complicated for my beginner understanding. Any simple explanations as to why nothing happens would be much appreciated.

[edit]: Persistence file:

      <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence          http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
<persistence-unit name="trainingDatabase">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.oreillyauto.javawebtraining.domain.Module</class>
    <class>com.oreillyauto.javawebtraining.domain.TrainingEntry</class>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.DB2400Dialect" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
        <property name="hibernate.jdbc.batch_size" value="30" />
        <property name="hibernate.max_fetch_depth" value="30" />
    </properties>
</persistence-unit>
</persistence>
  • can you post your META-INF/persistence.xml file? – StefanHeimberg Feb 05 '15 at 21:28
  • You seem to be confused between SQL and JPQL. You also have a concatenated query - injection anyone? I would say, first do some reading then try again. – Boris the Spider Feb 05 '15 at 21:31
  • I've edited the post. and Boris thanks for the incredibly insightful comment. –  Feb 05 '15 at 21:33
  • sorry, I'm just saying what everyone else here will say. First, there is not enough information here to fix anything - how do you call the method? How do you know it does nothing? Second, the code you have posted is so far from correct that it will be impossible to explain in a single answer. You are going to have to learn JPA. – Boris the Spider Feb 05 '15 at 21:40
  • Thank you for useful information. I'm not even sure what to include in this post as the problem seems to be, after debugging, solely within that method that I puked up. I figure you guys could point me in the right direction as you have. I will look into JPA and see if that will help the situation. –  Feb 05 '15 at 21:50
  • That would probably have worked if you changed to use `createNativeQuery()` which takes native SQL. – starcorn Mar 10 '16 at 09:17

1 Answers1

14

JPQL is not SQL:

em.createQuery("select e from Module e where e.status = :status",
    Module.class).setParameter("status", status).getResultList();
atamanroman
  • 11,607
  • 7
  • 57
  • 81
  • Thanks. I now realize that after yours and Boris' comment. I'm getting a "Parameter value [inactive] did not match expected type" error. I haven't looked into it just yet though. –  Feb 05 '15 at 22:01