使用PostgreSQL数据库无法使用JPQL获取数据Spring MVC

使用PostgreSQL数据库无法使用JPQL获取数据Spring MVC

问题描述:

我想开发SpringMVC框架,一切工作正常,但是当我运行我的代码时,数据不会从使用JPQL的Postgres数据库中获取。使用PostgreSQL数据库无法使用JPQL获取数据Spring MVC

域模型类

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 

repository类

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为 作为FIRST_NA3_0_的DESCRIPT2_0_,contact0_.FIRST_NAME, contact0_.LAST_NAME为LAST_NAM4_0_,contact0_.VERSION作为VERSION5_0_ 从联系contact0_

2016年1月24日下午二时十九分14秒org.apache.jasper.compiler.TldLocationsCache tldScanJar信息:至少有一个JAR被扫描*域名尚未包含 无*域名。为此记录器启用调试日志记录,以获取 JAR的完整列表,这些JAR在未找到TLD的情况下进行扫描。跳过JAR扫描 可以提高启动时间和JSP编译时间。

输出

enter image description here

Postgres数据库数据 enter image description here

请纠正我,我错过了什么。 非常感谢。

+0

插入数据后,是否在posgres DB中提交事务?还请尝试控制器中的联系人列表的sysout,看看是否有任何数据? –

+1

随着日志显示,查询被执行。在存储库,服务,控制器等中使用调试器或甚至简单的System.out.println()调用来查看返回列表在每个级别包含的内容。 –

+0

大家好,我已经尝试了所有的建议,但无法获取数据。请在下面找到答案。非常感谢您的支持。 – soewin

最后我意识到我错过了什么。

我忘了为模型类声明空构造函数。

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