Exemplo n.º 1
0
        private void DisplayAllEmployees()
        {
            EntityManager em = PersistenceUtils.getEm();

            grdEmployees.DataSource = em.CreateQuery <Employee>("from Employee", typeof(Employee)).GetResultList();
            grdEmployees.DataBind();
        }
Exemplo n.º 2
0
 private bool IsDuplicateOfExisting(Employee newEmployee)
 {
     try
     {
         EntityManager em = PersistenceUtils.getEm();
         Employee      duplicateEmployee = em.Find <Employee>(typeof(Employee), newEmployee.ID);
         return(duplicateEmployee != null);
     }
     catch (Exception e)
     {
         log.Info(e.Message);
         return(false);
     }
 }
Exemplo n.º 3
0
        private static void createAndSaveEntity()
        {
            EntityManager em  = PersistenceUtils.getEm();
            Employee      jon = new Employee();

            jon.FirstName = "jon";
            jon.LastName  = "do";

            em.GetTransaction().Begin();
            em.Persist(jon);
            em.GetTransaction().Commit();

            TypedQuery <Employee> query = em.CreateQuery <Employee>("from Employee as emp where emp.FirstName = ?", typeof(Employee));

            query.SetParameter(0, jon.FirstName);
            Employee emp = query.GetSingleResult();

            Console.WriteLine("Hello" + emp.FirstName);
        }
Exemplo n.º 4
0
        protected void btnAdd_OnClick(object sender, EventArgs e)
        {
            if (txtEmployeeID.Text.Trim().Length > 0)
            {
                Employee newEmployee = new Employee();
                newEmployee.FirstName = txtFirstName.Text;
                newEmployee.ID        = txtEmployeeID.Text;
                newEmployee.LastName  = txtLastName.Text;


                if (!IsDuplicateOfExisting(newEmployee))
                {
                    EntityManager em = PersistenceUtils.getEm();
                    em.GetTransaction().Begin();
                    try
                    {
                        em.Persist(newEmployee);
                        em.GetTransaction().Commit();
                        Response.Redirect("NPA_ExampleForm.aspx");
                    }
                    catch (Exception e1)
                    {
                        log.Info(e1.Message);
                        lblMessage.Text =
                            "<span style=\"color:red\">Problem during save</span><br />Please try again later.";
                    }
                }
                else
                {
                    lblMessage.Text =
                        "<span style=\"color:red\">The ID you provided is already in use.</span><br />Please change the ID and try again.";
                }
            }
            else
            {
                lblMessage.Text = "<span style=\"color:red\">The ID can't be empty</span>";
            }
        }