Package relationships

Examples of relationships.Book


        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);

            book.setBookCoverImage(new BookCoverImage());
            book.getBookCoverImage().setType("image/jpg");

            List<BookReview> bookReviews = book.getBookReviews();
            BookReview bookReview = new BookReview();
            bookReview.setRating(5);
            bookReviews.add(bookReview);
            bookReview = new BookReview();
            bookReview.setRating(4);
            bookReviews.add(bookReview);

            EntityTransaction txn = em.getTransaction();
            txn.begin();
            try {
                // When the Book is made persistent, the "PERSIST"
                // action cascades to the BookCoverImage object and
                // all BookReview objects.
                em.persist(book);
                txn.commit();
            } finally {
                if (txn.isActive()) {
                    txn.rollback();
                }
            }

        } finally {
            em.close();
        }

        try {
            em = emf.createEntityManager();

            Book book = em.find(Book.class, "978-0141185064");
            if (book != null) {
                out.println("<p>Found <i>" + book.getTitle() + "</i></p>");

                // Automatically fetch the BookCoverImage entity and access a field.
                out.println("<p>Book cover image type: " + book.getBookCoverImage().getType() + "</p>");

                out.println("<p>Ratings: ");
                for (BookReview bookReview : book.getBookReviews()) {
                    out.println("[" + bookReview.getRating() + "] ");
                }
                out.println("</p>");

            } else {
View Full Code Here

TOP

Related Classes of relationships.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.