Let's make a JPA sample program in Eclipse. Modify MVC program and use JPA to get data from Postgre SQL DB. Create an Emplyee table in advance. (See: Install Postgre SQL)
Windows 10 Pro 1709(16299.192) Eclipse pleiades-4.7.3 java 1.8.0_162 PostgreSQL 10.4 Released!
Copy to WEB-INF \ lib of project
Convert to JPA project
Source code changes
Operation check
URL
https://jdbc.postgresql.org/download.html
Change before
@Entity
@NamedQuery(name="Employee.findAll", query="SELECT e FROM Employee e")
public class Employee implements Serializable {
After change
@Entity
@NamedQueries({
@NamedQuery(name="Employee.findAll", query="SELECT e FROM Employee e"),
@NamedQuery(name = "Employee.findById", query = "SELECT e from Employee e where e.id = :id")
})
public class Employee implements Serializable {
After change
package emp;
import java.io.IOException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Employee;
/**
* Servlet implementation class EmployeeServlet
*/
@WebServlet(name = "EmpList", urlPatterns = { "/EmpList" })
public class EmployeeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public EmployeeServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("SampleMVC2");
EntityManager entityManager = emf.createEntityManager();
List<Employee> employeeList = entityManager
.createNamedQuery("Employee.findAll", Employee.class)
.getResultList();
entityManager.close();
emf.close();
//Pass Model data to View
request.setAttribute("employeeList", employeeList);
//Show View
this.getServletContext()
.getRequestDispatcher("/employeeList.jsp")
.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("SampleMVC2");
EntityManager entityManager = emf.createEntityManager();
List<Employee> employeeList;
String id = request.getParameter("id");
if (id.isEmpty()) {
employeeList = entityManager
.createNamedQuery("Employee.findAll", Employee.class)
.getResultList();
}
else {
employeeList = entityManager
.createNamedQuery("Employee.findById", Employee.class)
.setParameter("id", id)
.getResultList();
}
entityManager.close();
emf.close();
//Pass Model data to View
request.setAttribute("employeeList", employeeList);
//Show View
this.getServletContext()
.getRequestDispatcher("/employeeList.jsp")
.forward(request, response);
}
}
Change before
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="SampleMVC">
<class>model.Employee</class>
</persistence-unit>
</persistence>
After change (DB connection information added)
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="SampleMVC2">
<class>model.Employee</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" /> <!-- DB Driver -->
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/postgres" /> <!-- BD Mane -->
<property name="javax.persistence.jdbc.user" value="postgres" /> <!-- DB User -->
<property name="javax.persistence.jdbc.password" value="dev" /> <!-- DB Password -->
</properties>
</persistence-unit>
</persistence>
[Refer to "MVC with Eclipse." Operation check](https://qiita.com/0ashina0/items/7c378823905949b301af#4-%E5%8B%95%E4%BD%9C%E7%A2%BA%E8% AA% 8D)
--The annotation @PersistenceContext can be added to EntityManager only when transaction management (container management) is performed. Otherwise @PersistenceContext is ignored.
Recommended Posts