Package com.righettod.jee6jpa.entity

Examples of com.righettod.jee6jpa.entity.Product


                }
            }

            /*Product table*/
            //Load a product entity instance
            Product product = em.find(Product.class, 1);
            //Try to update values
            try {
                em.getTransaction().begin();
                //This value will activate the check on digits
                product.setPrice(BigDecimal.valueOf(235.345));
                //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


            //Update the added shop, add a new product to it
            System.out.println("*** UPDATE THE ADDED SHOP, ADD A NEW PRODUCT TO IT");
            //--Begin a transaction
            em.getTransaction().begin();
            //--Create new product (Product ID is generated)
            Product p = new Product();
            p.setName("GameGear number " + System.currentTimeMillis());
            p.setDescription("Console de jeu portable");
            p.setPrice(BigDecimal.valueOf(20.45));
            p.setShop(shop);
            try {
                //--Try to persist object
                em.persist(p);
                //--Commit current active transaction
                em.getTransaction().commit();
View Full Code Here

     * @param args Cmd line arguments
     */
    public static void main(String[] args) {
        EntityManagerFactory emFactory = null;
        EntityManager em = null;
        Product prod = null;
        CriteriaBuilder criteriaBuilder = null;
        CriteriaQuery<Product> criteriaQuery = null;
        Root<Product> product = null;
        List<Product> products = null;

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


            //Select Products using a parameter on name
            System.out.println("*** SELECT PRODUCTS USING A PARAMETER ON NAME");
            criteriaQuery = criteriaBuilder.createQuery(Product.class);
            product = criteriaQuery.from(Product.class);
            criteriaQuery.select(product).where(criteriaBuilder.equal(product.get("name"), "PS3"));
            try {
                prod = em.createQuery(criteriaQuery).getSingleResult();
                System.out.printf("[%s] : %s, %s\n", prod.getId(), prod.getName(), prod.getDescription());
            } catch (NoResultException nre) {
                //Exception throw by "getSingleResult()" if no result is returned by the query
                System.out.println("No product found !");
            } catch (NonUniqueResultException nure) {
                //Exception throw by "getSingleResult()" if several record is found by the query
View Full Code Here

TOP

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

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.