/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jpa.demo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
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 javax.transaction.NotSupportedException;
import javax.transaction.SystemException;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "EntityOPServlet", urlPatterns = {"/EntityOPServlet"})
public class EntityOPServlet extends HttpServlet {

    @PersistenceContext(unitName = "MyJPADemoPU")
    private EntityManager em;
    @Resource
    private javax.transaction.UserTransaction utx;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        try {
            Customer c = new Customer();
            c.setName("zx");
            c.setEmail("zx@163.com");
            c.setPhoneNumber("www");
            Address a = new Address();
            a.setCity("beijing");
            a.setState("bj");
            a.setStreet("changan street 2");
            c.setAddress(a);

            utx.begin();

            em.persist(c);

            utx.commit();

            /*  Address ad = em.getReference(Address.class, 2L);
             Customer c2 = new Customer();
             c2.setName("Peter");
             c2.setAddress(ad);
             utx.begin();
             em.persist(c2);
             em.persist(ad);
             utx.commit();*/

            /*   Customer c = em.find(Customer.class, 1L);

             LOG.info("Customer c Name:" + c.getName());
             c.setName("tom");
             utx.begin();
             em.merge(c);
             //  em.persist(c);
             em.flush();
             Customer c2 = em.find(Customer.class, 1L);
             LOG.info("Customer c2 Name:" + c2.getName());
             // em.persist(ad);
             utx.commit();*/
            /* utx.begin();
             Customer c = em.find(Customer.class, 1L);
             LOG.info("Customer c Name:" + c.getName());
             c.setName("hyl");
             Customer c2 = em.find(Customer.class, 1L);
             LOG.info("Customer c2 Name:" + c2.getName());
             LOG.info("Customer c Name:" + c.getName());
             utx.commit();*/
            /* utx.begin();
             Customer c = em.find(Customer.class, 1L);
             em.remove(c);
             utx.commit();
             LOG.info("Customer c Name:" + c.getName());*/
            // em.persist(ad);

        } catch (Exception ex) {
            Logger.getLogger(EntityOPServlet.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
        }
    }
    private static final Logger LOG = Logger.getLogger(EntityOPServlet.class.getName());

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
