Can not Fetch Data with JPQL using Postgres Database Spring MVC

By : soewin
Source: Stackoverflow.com
Question!

I am trying to develop SpringMVC framework, everything working fine but when I run my codes, data are not fetched from Postgres database using JPQL.

domain model class

    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 Class

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;
    }
}

Controller Class

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";

    }
}

Front View (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>

Server Logs

Hibernate: select contact0_.ID as ID1_0_, contact0_.DESCRIPTION as DESCRIPT2_0_, contact0_.FIRST_NAME as FIRST_NA3_0_, contact0_.LAST_NAME as LAST_NAM4_0_, contact0_.VERSION as VERSION5_0_ from Contact contact0_

Jan 24, 2016 2:19:14 PM org.apache.jasper.compiler.TldLocationsCache tldScanJar INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned where no TLDs were found. Skipping JAR scanning can improve startup time and JSP compilation time.

Output

enter image description here

Postgres DataBase data enter image description here

Please correct me what I am missing. Thank you so much.

By : soewin


Answers

Finally I realized what I have missed.

I forget to declare empty constructor for my model class.

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
By : soewin


This video can help you solving your question :)
By: admin