Advertisement
Guest User

Untitled

a guest
May 10th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.60 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <persistence-unit name="JPA_ProductosWebPU" transaction-type="RESOURCE_LOCAL">
  4. <provider>org.hibernate.ejb.HibernatePersistence</provider>
  5. <class>com.empresa.proyecto.entity.Pedidos</class>
  6. <class>com.empresa.proyecto.entity.Productos</class>
  7. <class>com.empresa.proyecto.entity.Pedidosxproducto</class>
  8. <exclude-unlisted-classes>false</exclude-unlisted-classes>
  9. <properties>
  10. <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
  11. <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/dbproductos?zeroDateTimeBehavior=convertToNull"/>
  12. <property name="javax.persistence.jdbc.user" value="root"/>
  13. <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  14. <property name="javax.persistence.jdbc.password" value=""/>
  15. </properties>
  16. </persistence-unit>
  17. </persistence>
  18.  
  19. package com.empresa.proyecto.entity;
  20.  
  21. import java.io.Serializable;
  22. import java.util.List;
  23. import javax.persistence.Basic;
  24. import javax.persistence.CascadeType;
  25. import javax.persistence.Column;
  26. import javax.persistence.Entity;
  27. import javax.persistence.FetchType;
  28. import javax.persistence.GeneratedValue;
  29. import javax.persistence.GenerationType;
  30. import javax.persistence.Id;
  31. import javax.persistence.NamedQueries;
  32. import javax.persistence.NamedQuery;
  33. import javax.persistence.OneToMany;
  34. import javax.persistence.Table;
  35. import javax.xml.bind.annotation.XmlRootElement;
  36. import javax.xml.bind.annotation.XmlTransient;
  37.  
  38. @Entity
  39. @Table(name = "productos")
  40. @XmlRootElement
  41. @NamedQueries({
  42. @NamedQuery(name = "Productos.findAll", query = "SELECT p FROM Productos p"),
  43. @NamedQuery(name = "Productos.findById", query = "SELECT p FROM Productos p WHERE p.id = :id"),
  44. @NamedQuery(name = "Productos.findByNombre", query = "SELECT p FROM Productos p WHERE p.nombre = :nombre"),
  45. @NamedQuery(name = "Productos.findByPreuni", query = "SELECT p FROM Productos p WHERE p.preuni = :preuni")})
  46. public class Productos implements Serializable {
  47.  
  48. private static final long serialVersionUID = 1L;
  49. @Id
  50. @GeneratedValue(strategy = GenerationType.IDENTITY)
  51. @Basic(optional = false)
  52. @Column(name = "ID")
  53. private Integer id;
  54. @Basic(optional = false)
  55. @Column(name = "NOMBRE")
  56. private String nombre;
  57. @Basic(optional = false)
  58. @Column(name = "PREUNI")
  59. private double preuni;
  60. @OneToMany(cascade = CascadeType.ALL, mappedBy = "productos", fetch = FetchType.LAZY)
  61. private List<Pedidosxproducto> pedidosxproductoList;
  62.  
  63. public Productos() {
  64. }
  65.  
  66. public Productos(Integer id) {
  67. this.id = id;
  68. }
  69.  
  70. public Productos(Integer id, String nombre, double preuni) {
  71. this.id = id;
  72. this.nombre = nombre;
  73. this.preuni = preuni;
  74. }
  75.  
  76. public Integer getId() {
  77. return id;
  78. }
  79.  
  80. public void setId(Integer id) {
  81. this.id = id;
  82. }
  83.  
  84. public String getNombre() {
  85. return nombre;
  86. }
  87.  
  88. public void setNombre(String nombre) {
  89. this.nombre = nombre;
  90. }
  91.  
  92. public double getPreuni() {
  93. return preuni;
  94. }
  95.  
  96. public void setPreuni(double preuni) {
  97. this.preuni = preuni;
  98. }
  99.  
  100. @XmlTransient
  101. public List<Pedidosxproducto> getPedidosxproductoList() {
  102. return pedidosxproductoList;
  103. }
  104.  
  105. public void setPedidosxproductoList(List<Pedidosxproducto> pedidosxproductoList) {
  106. this.pedidosxproductoList = pedidosxproductoList;
  107. }
  108.  
  109. @Override
  110. public int hashCode() {
  111. int hash = 0;
  112. hash += (id != null ? id.hashCode() : 0);
  113. return hash;
  114. }
  115.  
  116. @Override
  117. public boolean equals(Object object) {
  118. // TODO: Warning - this method won't work in the case the id fields are not set
  119. if (!(object instanceof Productos)) {
  120. return false;
  121. }
  122. Productos other = (Productos) object;
  123. if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
  124. return false;
  125. }
  126. return true;
  127. }
  128.  
  129. @Override
  130. public String toString() {
  131. return "com.empresa.proyecto.entity.Productos[ id=" + id + " ]";
  132. }
  133.  
  134. }
  135.  
  136. package dao;
  137.  
  138. import com.empresa.proyecto.entity.Productos;
  139. import java.util.List;
  140. import javax.persistence.EntityManager;
  141. import javax.persistence.EntityManagerFactory;
  142. import javax.persistence.Persistence;
  143. import javax.persistence.Query;
  144.  
  145. public class ProductoDAO {
  146.  
  147. private EntityManagerFactory emf;
  148. private EntityManager em;
  149.  
  150. public ProductoDAO() {
  151. emf = Persistence.createEntityManagerFactory("JPA_ProductosWebPU");
  152. em = emf.createEntityManager();
  153. }
  154.  
  155. public List<Productos> productoQRY() {
  156. Query q = em.createQuery("select productos from Productos productos ");
  157. return q.getResultList();
  158. }
  159.  
  160. public void productoINS(Productos productos) {
  161. em.getTransaction().begin();
  162. em.persist(productos);
  163. em.getTransaction().commit();
  164. }
  165.  
  166. public void productoUPD(Productos productos) {
  167. em.getTransaction().begin();
  168. em.merge(productos);
  169. em.getTransaction().commit();
  170. }
  171.  
  172. public void productoDEL(Productos productos) {
  173. em.getTransaction().begin();
  174. em.remove(productos);
  175. em.getTransaction().commit();
  176. }
  177.  
  178. public Productos buscarProductoByID(int id) {
  179. return em.find(Productos.class, id);
  180. }
  181.  
  182. }
  183.  
  184. package web.servlet;
  185.  
  186. import com.empresa.proyecto.entity.Productos;
  187. import dao.ProductoDAO;
  188. import java.io.IOException;
  189. import java.util.List;
  190. import javax.servlet.ServletException;
  191. import javax.servlet.annotation.WebServlet;
  192. import javax.servlet.http.HttpServlet;
  193. import javax.servlet.http.HttpServletRequest;
  194. import javax.servlet.http.HttpServletResponse;
  195.  
  196. @WebServlet(name = "ServletProductos", urlPatterns = {"/Productos"})
  197. public class ServletProductos extends HttpServlet {
  198.  
  199. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  200. throws ServletException, IOException {
  201. response.setCharacterEncoding("UTF-8");
  202.  
  203. String accion = request.getParameter("accion");
  204.  
  205. if (accion.equals("QRY")) {
  206.  
  207. ProductoDAO productoDAO = new ProductoDAO();
  208. List<Productos> list = productoDAO.productoQRY();
  209. request.setAttribute("list", list);
  210. request.getRequestDispatcher("view/productos/").forward(request, response);
  211.  
  212. } else if (accion.equals("INS")) {
  213.  
  214. String nom = request.getParameter("nom");
  215. double pu = Double.parseDouble(request.getParameter("pu"));
  216.  
  217. Productos productos = new Productos();
  218. productos.setNombre(nom);
  219. productos.setPreuni(pu);
  220.  
  221. ProductoDAO productoDAO = new ProductoDAO();
  222. productoDAO.productoINS(productos);
  223.  
  224. List<Productos> list = productoDAO.productoQRY();
  225. request.setAttribute("list", list);
  226. request.getRequestDispatcher("view/productos/").forward(request, response);
  227.  
  228. } else if (accion.equals("GET")) {
  229.  
  230. int id = Integer.parseInt(request.getParameter("id"));
  231. ProductoDAO productoDAO = new ProductoDAO();
  232. Productos productos = productoDAO.buscarProductoByID(id);
  233.  
  234. request.setAttribute("productos", productos);
  235.  
  236. request.getRequestDispatcher("./view/productos/productoUpd.jsp").forward(request, response);
  237.  
  238. } else if (accion.equals("UPD")) {
  239.  
  240. int id = Integer.parseInt(request.getParameter("id"));
  241. String nom = request.getParameter("nom");
  242. double pu = Double.parseDouble(request.getParameter("pu"));
  243.  
  244. Productos productos = new Productos(id, nom, pu);
  245.  
  246. ProductoDAO productoDAO = new ProductoDAO();
  247. productoDAO.productoUPD(productos);
  248.  
  249. List<Productos> list = productoDAO.productoQRY();
  250. request.setAttribute("list", list);
  251. request.getRequestDispatcher("view/productos/").forward(request, response);
  252.  
  253. } else if (accion.equals("DEL")) {
  254.  
  255. int id = Integer.parseInt(request.getParameter("id"));
  256. ProductoDAO productoDAO = new ProductoDAO();
  257. Productos productos = productoDAO.buscarProductoByID(id);
  258.  
  259. productoDAO.productoDEL(productos);
  260.  
  261. List<Productos> list = productoDAO.productoQRY();
  262. request.setAttribute("list", list);
  263. request.getRequestDispatcher("view/productos/").forward(request, response);
  264.  
  265. }
  266.  
  267. }
  268.  
  269. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  270. /**
  271. * Handles the HTTP <code>GET</code> method.
  272. *
  273. * @param request servlet request
  274. * @param response servlet response
  275. * @throws ServletException if a servlet-specific error occurs
  276. * @throws IOException if an I/O error occurs
  277. */
  278. @Override
  279. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  280. throws ServletException, IOException {
  281. processRequest(request, response);
  282. }
  283.  
  284. /**
  285. * Handles the HTTP <code>POST</code> method.
  286. *
  287. * @param request servlet request
  288. * @param response servlet response
  289. * @throws ServletException if a servlet-specific error occurs
  290. * @throws IOException if an I/O error occurs
  291. */
  292. @Override
  293. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  294. throws ServletException, IOException {
  295. processRequest(request, response);
  296. }
  297.  
  298. /**
  299. * Returns a short description of the servlet.
  300. *
  301. * @return a String containing servlet description
  302. */
  303. @Override
  304. public String getServletInfo() {
  305. return "Short description";
  306. }// </editor-fold>
  307.  
  308. }
  309.  
  310. < %@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  311. < %@page contentType="text/html" pageEncoding="UTF-8"%>
  312. < !DOCTYPE html>
  313. < html>
  314. < head>
  315. < meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  316. < title>JPA 2.0< /title>
  317. < /head>
  318. < body>
  319. < div>
  320. < table border="1">
  321. < h1>Lista de Productos</h1>
  322. < thead>
  323. < tr>
  324. < td>Código</td>
  325. < td>Nombre</td>
  326. < td>Precio Unitario</td>
  327. < td colspan="2" style="width: 40px">&nbsp;</td>
  328. < /tr>
  329. < /thead>
  330.  
  331. < tfoot>
  332. < tr>
  333. < th colspan="9">JPA 2.0</th>
  334. < /tr>
  335. < /tfoot>
  336.  
  337. < tbody>
  338. < c:forEach items="${requestScope.list}" var="producto" >
  339. < tr>
  340. < td>${producto.id}</td>
  341. < td>${producto.nombre}</td>
  342. < td>${producto.preuni}</td>
  343. < th><a href="./Productos?accion=GET&id=${producto.id}" >Modificar< /a>< /th>
  344. < th>< a href="./Productos?accion=DEL&id=${producto.id}" >Eliminar< /a></th>
  345. </tr >
  346. </c:forEach >
  347. </tbody >
  348. </table >
  349.  
  350. < p>< a class="simple" href="view/productos/productoIns.jsp">Agregar Producto</a>< /p>
  351. < p>< a class="simple" href="index.jsp">Home</a></p>
  352. < /div>
  353. < /body>
  354. < /html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement