簡體   English   中英

無法使用Postgres數據庫Spring MVC通過JPQL提取數據

[英]Can not Fetch Data with JPQL using Postgres Database Spring MVC

我正在嘗試開發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_中選擇contact0_.ID作為ID1_0_,contact0_.DESCRIPTION作為DESCRIPT2_0_,contact0_.FIRST_NAME作為FIRST_NA3_0_,contact0_.LAST_NAME作為LAST_NAM4_0_,contact0_.VERSION作為VERSION5_0_。

2016年1月24日,下午2:19:14 org.apache.jasper.compiler.TldLocationsCache tldScanJar信息:至少對一個JAR進行了TLD掃描,但不包含TLD。 為此記錄器啟用調試日志記錄,以獲取在沒有找到TLD的情況下掃描的JAR的完整列表。 跳過JAR掃描可以縮短啟動時間和JSP編譯時間。

輸出量

在此處輸入圖片說明

Postgres數據庫數據 在此處輸入圖片說明

請糾正我所缺少的內容。 非常感謝。

終於我意識到了我所錯過的。

我忘了為模型類聲明空的構造函數。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM