我正在尝试开发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>
服务器日志
Hibernate:选择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编译时间。
输出
请纠正我错过的内容。 非常感谢你。
答案 0 :(得分:0)
最后我意识到我错过了什么。
我忘了为我的模型类声明空构造函数。
public class Contact implements Serializable {
private Long id;
private int version;
private String firstName;
private String lastName;
private String description;
public Contact(){} // This invoke to model with Entity class and interact with Database
getter and setter defined