java 使用 entityManager 从数据库中获取列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28354302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using entityManager to get list from database
提问by atamanroman
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:
我一直在 Spring MVC 中工作,完全不知道我在做什么。我正在尝试使用 entityManager 从数据库中检索对象(或记录)列表。我的方法似乎没有任何作用:
@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>
回答by atamanroman
JPQL is not SQL:
JPQL 不是 SQL:
em.createQuery("select e from Module e where e.status = :status",
Module.class).setParameter("status", status).getResultList();