【发布时间】:2016-04-30 15:26:42
【问题描述】:
我正在尝试开发 SpringMVC 框架,一切正常,但是当我运行我的代码时,没有使用 JPQL 从 Postgres 数据库中获取数据。
领域模型类
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Version;
@Entity
@Table(name = "Contact")
@NamedQueries({
@NamedQuery(name="Contact.findAll", query="select c from Contact c")
})
public class Contact implements Serializable {
private Long id;
private int version;
private String firstName;
private String lastName;
private String description;
getter and setter defined
存储库类
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.apress.prospring3.ch17.domain.Contact;
@Repository
@Transactional
public class ContactRepository implements CrudRepository {
@PersistenceContext
private EntityManager manager;
public List<Contact> findAll() {
List<Contact> contact = manager.createNamedQuery("Contact.findAll",
Contact.class).getResultList();
return contact;
}
}
控制器类
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.apress.prospring3.ch17.domain.Contact;
import com.apress.prospring3.ch17.service.ContactService;
@RequestMapping("/contact")
@Controller
public class ContactController {
@Autowired
ContactService contactService;
@RequestMapping(method = RequestMethod.GET)
public String list(Model uiModel) {
List<Contact> contacts = contactService.findAll();
uiModel.addAttribute("contacts", contacts);
return "contacts/list";
}
}
前视图 (list.jspx)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:joda="http://www.joda.org/joda/time/tags" version="2.0">
<jsp:directive.page contentType="text/html;charset=UTF-8" />
<jsp:output omit-xml-declaration="yes" />
<h1>Contact Listing</h1>
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Birth Date</th>
</tr>
</thead>
<tbody>
<c:forEach items="${contacts}" var="contact">
<tr>
<td>${contact.firstName}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
服务器日志
休眠:选择contact0_.ID作为ID1_0_,contact0_.DESCRIPTION作为 DESCRIPT2_0_,contact0_.FIRST_NAME 为 FIRST_NA3_0_, contact0_.LAST_NAME 作为 LAST_NAM4_0_,contact0_.VERSION 作为 VERSION5_0_ 来自联系人contact0_
2016 年 1 月 24 日下午 2:19:14 org.apache.jasper.compiler.TldLocationsCache tldScanJar 信息:已扫描至少一个 JAR 以查找尚未包含的 TLD 没有顶级域名。启用此记录器的调试日志记录以获得完整列表 在未找到 TLD 的情况下扫描的 JAR。跳过 JAR 扫描 可以提高启动时间和JSP编译时间。
输出
请纠正我的遗漏。 非常感谢。
【问题讨论】:
-
插入数据后是否在 posgres DB 中提交了事务?也可以在控制器中尝试 sysout 的联系人列表,看看是否有任何数据?
-
查询被执行,如日志所示。使用您的调试器,甚至在存储库、服务、控制器等中使用简单的 System.out.println() 调用来查看返回的列表在每个级别包含的内容。
-
大家好,我已经尝试了您的所有建议,但无法获取数据。请在下面找到答案。非常感谢您的支持。
标签: spring hibernate postgresql spring-mvc jpql