Créons un exemple de programme JPA avec Eclipse. Modifiez le programme MVC et utilisez JPA pour obtenir les données de la base de données SQL Postgre. Créez une table Emplyee à l'avance. (Voir: Installer Postgre SQL)
Windows 10 Pro 1709(16299.192) Eclipse pleiades-4.7.3 java 1.8.0_162 PostgreSQL 10.4 Released!
Copier dans WEB-INF \ lib du projet
Convertir en projet JPA
Modifications du code source
Contrôle de fonctionnement
URL
https://jdbc.postgresql.org/download.html
Changer avant
@Entity
@NamedQuery(name="Employee.findAll", query="SELECT e FROM Employee e")
public class Employee implements Serializable {
Après le changement
@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 {
Après le changement
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();
//Passer les données du modèle à la vue
request.setAttribute("employeeList", employeeList);
//Afficher la vue
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();
//Passer les données du modèle à la vue
request.setAttribute("employeeList", employeeList);
//Afficher la vue
this.getServletContext()
.getRequestDispatcher("/employeeList.jsp")
.forward(request, response);
}
}
Changer avant
<?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>
Après modification (informations de connexion à la base de données ajoutées)
<?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>
[Reportez-vous à la vérification du fonctionnement de «MVC avec Eclipse».](Https://qiita.com/0ashina0/items/7c378823905949b301af#4-%E5%8B%95%E4%BD%9C%E7%A2%BA%E8% AA% 8D)
--L'annotation @PersistenceContext peut être ajoutée à EntityManager uniquement lorsque la gestion des transactions (gestion des conteneurs) est effectuée. Sinon, @PersistenceContext est ignoré.
Recommended Posts