Package javax.persistence

Examples of javax.persistence.EntityManager


    /**
     * Verifies if the commit works.
     */
    public void resourceLevelCommit() {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        // begins the trasnaction
        entityManager.getTransaction().begin();
        // inserts a book
        Book book = new Book();
        book.setId(ID);
        book.setName(ENTITY_NAME);
        entityManager.persist(book);
        // verifies if the transaction is marked as ACTIVE
        assertTrue(entityManager.getTransaction().isActive(),
                "The transaction is active, but the container returned false when the method isActive was called.");
        // verify if the rollbackonly is not set.
        assertFalse(entityManager.getTransaction().getRollbackOnly(),
                "The transaction is not marked as rollback, but the container returned true for the method getRollbackOnly()");
        // makes a commit
        entityManager.getTransaction().commit();
        entityManager.close();
        // verifies if the bean is persistent.
        Book bookResult = entityManager.find(Book.class, new Integer(ID));
        assertNotNull(bookResult, "The container did not make the rollback");

    }
View Full Code Here


     * @param ref Entity Manager Factory
     * @param name row name
     * @throws Exception if a problem occurs
     */
    public static void checkInstance(final EntityManagerFactory ref, final String name) throws Exception {
        EntityManager entityManager = ref.createEntityManager();
        logger.debug("Checking an entity manager factory reference.");

        if (entityManager == null) {
            logger.debug("The method createEntityManager() returned null.");
            throw new IllegalStateException("Error checking an entity manager factory reference.");
View Full Code Here

      {
         ut.begin();
         try
         {
            InitialContext ctx = new InitialContext();
            EntityManager em = (EntityManager) ctx.lookup("java:comp/env/persistence/em");
           
            String mode = req.getParameter("mode");
            if ("Write".equals(mode))
            {
               TestEntity test = new TestEntity("Hello", "World");
               em.persist(test);
            }
            else
            {
               TestEntity test = em.find(TestEntity.class, "Hello");
               resp.setContentType("text/plain");
               PrintWriter out = resp.getWriter();
               out.print(test.getDescription());
               out.close();
            }
View Full Code Here

  public void fill() throws JRException
  {
    long start = System.currentTimeMillis();
    // create entity manager factory for connection with database
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu1", new HashMap());
    EntityManager em = emf.createEntityManager();

    try
    {
      Map parameters = getParameters(em);
     
      JasperFillManager.fillReportToFile("build/reports/JRMDbReport.jasper", parameters);

      em.close();
     
      System.err.println("Filling time : " + (System.currentTimeMillis() - start));
    }
    finally
    {
      if (em.isOpen())
        em.close();
      if (emf.isOpen())
        emf.close();
    }
  }
View Full Code Here

            protected void testComponents()
                throws Exception
            {
                Product p = new Product();

                EntityManager em = (EntityManager) getValue("#{entityManager}");
                try {
                    em.persist(p);
                    fail("empty product persisted");
                } catch (PersistenceException e) {
                    // good
                }                
            }           
View Full Code Here

         p.setTitle("test");

         new FacesRequest() {
            protected void invokeApplication()
            {
                EntityManager em = (EntityManager) getValue("#{entityManager}");               
                em.persist(p);           
            }
           
          
         }.run();
        
         new FacesRequest() {
             protected void invokeApplication()
             {
                 EntityManager em = (EntityManager) getValue("#{entityManager}");
                 Product found = em.find(Product.class ,p.getProductId());
                 assertNotNull("find by id", found);
                 assertEquals("id", p.getProductId(), found.getProductId());
                 assertEquals("title", "test", found.getTitle());
        
                 em.remove(found);            
             }
          }.run();
        
          new FacesRequest() {
              protected void invokeApplication()
              {
                  EntityManager em = (EntityManager) getValue("#{entityManager}");
                  Product found = em.find(Product.class ,p.getProductId());

                  assertNull("deleted product", found);            
              }
           }.run();
         
View Full Code Here

         InitialContext ctx = new InitialContext();
        
         t = (UserTransaction) ctx.lookup("UserTransaction");
         t.begin();
     
         EntityManager em = (EntityManager) Component.getInstance("entityManager", true);
        
         List<Auction> auctions = em.createQuery("select a from Auction a").getResultList();
        
         Calendar cal = new GregorianCalendar();
        
         Random r = new Random(System.currentTimeMillis());
        
         for (Auction auction : auctions)
         {
            cal.setTime(auction.getEndDate());
            cal.add(Calendar.DATE, r.nextInt(7));
            cal.add(Calendar.MINUTE, 30 + r.nextInt(1410));
            auction.setEndDate(cal.getTime());
            auction.setStatus(Auction.STATUS_COMPLETED);
            em.merge(auction);

            AuctionEndAction auctionEnd = (AuctionEndAction) Component.getInstance(AuctionEndAction.class, true);
            auctionEnd.endAuction(auction.getAuctionId(), auction.getEndDate());
         }
        
View Full Code Here

    @Test
    public void directoryURL() throws Exception {
        new FacesRequest() {

            protected void invokeApplication() throws Exception {
                EntityManager em = (EntityManager) getInstance("restrictedEntityManager");
                WikiDirectory d = (WikiDirectory)
                        em.createQuery("select d from WikiDirectory d where d.id = :id")
                                .setParameter("id", 3l)
                                .getSingleResult();

                assert d.getPermURL(".lace").equals("3.lace");
                assert d.getWikiURL().equals("CCC");

                d = (WikiDirectory)
                        em.createQuery("select d from WikiDirectory d where d.id = :id")
                                .setParameter("id", 4l)
                                .getSingleResult();

                assert d.getPermURL(".lace").equals("4.lace");
                assert d.getWikiURL().equals("CCC/DDD");

                d = (WikiDirectory)
                        em.createQuery("select d from WikiDirectory d where d.id = :id")
                                .setParameter("id", 1l)
                                .getSingleResult();

                assert d.getPermURL(".lace").equals("1.lace");
                assert d.getWikiURL().equals("");
View Full Code Here

    @Test
    public void commentURL() throws Exception {
        new FacesRequest() {

            protected void invokeApplication() throws Exception {
                EntityManager em = (EntityManager) getInstance("restrictedEntityManager");
                WikiComment c = (WikiComment)
                        em.createQuery("select c from WikiComment c where c.id = :id")
                                .setParameter("id", 10l)
                                .getSingleResult();

                assert c.getPermURL(".lace").equals("6.lace#comment10");
                assert c.getWikiURL().equals("CCC/One#comment10");
View Full Code Here

    @Test
    public void uploadURL() throws Exception {
        new FacesRequest() {

            protected void invokeApplication() throws Exception {
                EntityManager em = (EntityManager) getInstance("restrictedEntityManager");
                WikiUpload u = (WikiUpload)
                        em.createQuery("select u from WikiUpload u where u.id = :id")
                                .setParameter("id", 30l)
                                .getSingleResult();

                assert u.getPermURL(".lace").equals("service/File/30");
                assert u.getWikiURL().equals("service/File/30");
View Full Code Here

TOP

Related Classes of javax.persistence.EntityManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.