Package queries

Examples of queries.Book


        EntityManagerFactory emf = EMF.get();
        EntityManager em = null;

        try {
            em = emf.createEntityManager();
            Book book = new Book("978-0141185064");
            book.setTitle("The Grapes of Wrath");
            book.setAuthor("John Steinbeck");
            book.setCopyrightYear(1939);
            Date authorBirthdate =
                new GregorianCalendar(1902, Calendar.FEBRUARY, 27).getTime();
            book.setAuthorBirthdate(authorBirthdate);

            em.persist(book);
        } finally {
            em.close();
        }

        try {
            em = emf.createEntityManager();
            Book book = new Book("978-0141185101");
            book.setTitle("Of Mice and Men");
            book.setAuthor("John Steinbeck");
            book.setCopyrightYear(1937);
            Date authorBirthdate =
                new GregorianCalendar(1902, Calendar.FEBRUARY, 27).getTime();
            book.setAuthorBirthdate(authorBirthdate);

            em.persist(book);
        } finally {
            em.close();
        }

        try {
            em = emf.createEntityManager();
            Book book = new Book("978-0684801469");
            book.setTitle("A Farewell to Arms");
            book.setAuthor("Ernest Hemmingway");
            book.setCopyrightYear(1929);
            Date authorBirthdate =
                new GregorianCalendar(1899, Calendar.JULY, 21).getTime();
            book.setAuthorBirthdate(authorBirthdate);

            em.persist(book);
        } finally {
            em.close();
        }

        try {
            em = emf.createEntityManager();
            Book book = new Book("978-0684830483");
            book.setTitle("For Whom the Bell Tolls");
            book.setAuthor("Ernest Hemmingway");
            book.setCopyrightYear(1940);
            Date authorBirthdate =
                new GregorianCalendar(1899, Calendar.JULY, 21).getTime();
            book.setAuthorBirthdate(authorBirthdate);

            em.persist(book);
        } finally {
            em.close();
        }

        try {
            em = emf.createEntityManager();
            Query query = null;
            List<Book> results = null;

            // Query for all entities of a kind
            query = em.createQuery("SELECT b FROM Book b");
            out.println("<p>Every book:</p><ul>");
            results = (List<Book>) query.getResultList();
            for (Book b : results) {
                out.println("<li><i>" + b.getTitle() + "</i>, " +
                            b.getAuthor() + ", " +
                            b.getCopyrightYear() + "</li>");
            }
            out.println("</ul>");

            // Query with a property filter
            query = em.createQuery(
                "SELECT b FROM Book b WHERE copyrightYear >= :earliestYear");
            query.setParameter("earliestYear", 1937);
            out.println("<p>Every book published in or after 1937:</p><ul>");
            results = (List<Book>) query.getResultList();
            for (Book b : results) {
                out.println("<li><i>" + b.getTitle() + "</i>, " +
                            b.getAuthor() + ", " +
                            b.getCopyrightYear() + "</li>");
            }
            out.println("</ul>");

            // Getting just the first result of a query
            query = em.createQuery(
                "SELECT b FROM Book b WHERE title = \"A Farewell to Arms\"");
            Book singleResult = (Book) query.getSingleResult();
            if (singleResult != null) {
                out.println("<p>Found: <i>" + singleResult.getTitle() +
                            "</i>, " + singleResult.getAuthor() + "</p>");
            } else {
                out.println("<p>Could not find that book I was looking for...</p>");
            }

            // Getting specific results in the result list
View Full Code Here

TOP

Related Classes of queries.Book

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.