Package com.righettod.jee6jpa.entity

Examples of com.righettod.jee6jpa.entity.Shop


            for (int i = 0; i < 4; i++) {
                Sample03.main(args);
            }

            //Load father object
            Shop shop = em.find(Shop.class, 4);
            //Remove it in a explicit transaction
            //--Begin a new transaction
            em.getTransaction().begin();
            try {
                //--Try to remove object
View Full Code Here


    public static void main(String[] args) {
        EntityManagerFactory emFactory = null;
        EntityManager em = null;
        TypedQuery<Shop> queryShop = null;
        List<Shop> shops = null;
        Shop shop = null;

        try {
            //Create a EntityManager instance using EntityManagerFactory
            emFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
            em = emFactory.createEntityManager();


            //Select all shops
            System.out.println("*** SELECT ALL SHOPS");
            queryShop = em.createQuery("SELECT s FROM Shop s", Shop.class);
            shops = queryShop.getResultList();
            for (Shop s : shops) {
                System.out.printf("[%s] : %s\n", s.getId(), s.getName());
            }


            //Select a shop using is id
            System.out.println("*** SELECT A SHOP USING IS ID");
            System.out.println("** NAMED PARAMETER VERSION");
            queryShop = em.createQuery("SELECT s FROM Shop s WHERE s.id = :id", Shop.class);
            try {
                shop = queryShop.setParameter("id", 2).getSingleResult();
                System.out.printf("[%s] : %s\n", shop.getId(), shop.getName());
            } catch (NoResultException nre) {
                //Exception throw by "getSingleResult()" if no result is returned by the query
                System.out.println("No shop found !");
            } catch (NonUniqueResultException nure) {
                //Exception throw by "getSingleResult()" if several record is found by the query
                System.out.println("More than one record exists for this ID !");
            }
            System.out.println("** ORDINAL PARAMETER VERSION");
            queryShop = em.createQuery("SELECT s FROM Shop s WHERE s.id = ?1", Shop.class);
            try {
                shop = queryShop.setParameter(1, 2).getSingleResult();
                System.out.printf("[%s] : %s\n", shop.getId(), shop.getName());
            } catch (NoResultException nre) {
                //Exception throw by "getSingleResult()" if no result is returned by the query
                System.out.println("No shop found !");
            } catch (NonUniqueResultException nure) {
                //Exception throw by "getSingleResult()" if several record is found by the query
                System.out.println("More than one record exists for this ID !");
            }


            //Select shops with a range restriction
            System.out.println("*** SELECT SHOPS WITH A RANGE RESTRICTION");
            queryShop = em.createQuery("SELECT s FROM Shop s", Shop.class);
            shops = queryShop.setFirstResult(1).setMaxResults(2).getResultList();
            for (Shop s : shops) {
                System.out.printf("[%s] : %s\n", s.getId(), s.getName());
            }


            //Select all shops using a predefined Named Query
            System.out.println("*** SELECT SHOPS USING A PREDEFINED NAMED QUERY");
            queryShop = em.createNamedQuery("Shop.findAll", Shop.class);
            shops = queryShop.getResultList();
            for (Shop s : shops) {
                System.out.printf("[%s] : %s\n", s.getId(), s.getName());
            }


            //Select a shop using a pessimistic lock
            System.out.println("*** SELECT A SHOPS USING PESSIMISTIC LOCK");
            try {
                shop = em.find(Shop.class, 2, LockModeType.PESSIMISTIC_READ);
                System.out.printf("[%s] : %s\n", shop.getId(), shop.getName());
            } catch (PessimisticLockException ple) {
                System.out.println("Unable to obtains lock on record !");
            }

        } catch (Exception e) {
View Full Code Here

            em = emFactory.createEntityManager();


            /*Shop table*/
            //Load a shop entity instance
            Shop shop = em.find(Shop.class, 1);
            //Try to update values
            try {
                em.getTransaction().begin();
                //This value will activate the check on max size
                shop.setName("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
                //In AUTO flush mode (default) the update on entity will be send at commit....
                em.getTransaction().commit();
            } catch (PersistenceException pe) {
                //No need to rollback because the exception have rollbacked and closed the transaction...
                if (pe.getCause() instanceof ConstraintViolationException) {
View Full Code Here

            //The lifetime of the EntityManager is restricted to the scope of the main method,
            //thus the attached (managed) entities managed by this EM are managed for the same lifetime...

            //Add a new shop
            Shop shop = em.find(Shop.class, 4);
            System.out.println("*** INSERT A NEW SHOP");
            //--Check that the shop do not exist
            if (shop == null) {
                //--Create a new object
                shop = new Shop();
                shop.setId(4);
                shop.setName("LIDLE");
                //--Begin a new transaction
                em.getTransaction().begin();
                try {
                    //--Try to persist object
                    em.persist(shop);
View Full Code Here

TOP

Related Classes of com.righettod.jee6jpa.entity.Shop

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.